Skip to content

Commit 5fe0df7

Browse files
committed
centraliize config and update docs
Signed-off-by: Andreas Bräu <[email protected]>
1 parent f3d66c6 commit 5fe0df7

File tree

7 files changed

+1574
-153
lines changed

7 files changed

+1574
-153
lines changed

ics-collector/CalendarAPI.php

Lines changed: 26 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
require_once 'lib/IcsValidator.php';
1515
require_once 'lib/SabreVObjectCalendarHandler.php';
16+
require_once 'lib/CalendarConfig.php';
1617

1718
use ICal\IcsValidator;
1819
use ICal\SabreVObjectCalendarHandler;
20+
use ICal\CalendarConfig;
1921

2022
/**
2123
* CalendarAPI class for handling iCal data and generating responses
@@ -32,54 +34,34 @@ class CalendarAPI {
3234
protected string $icsMergedFile;
3335

3436
/**
35-
* Supported HTTP methods
37+
* Supported HTTP methods (from central configuration)
3638
* @var array<string>
3739
*/
38-
protected array $supportedMethods = ['GET'];
40+
protected array $supportedMethods;
3941

4042
/**
41-
* Mandatory parameters fields
43+
* Mandatory parameters fields (from central configuration)
4244
* @var array<string>
4345
*/
44-
protected array $mandatory = ['source'];
46+
protected array $mandatory;
4547

4648
/**
47-
* If not specified, the parameters will take these default values
49+
* If not specified, the parameters will take these default values (from central configuration)
4850
* @var array<string, string>
4951
*/
50-
protected array $defaultValues = [
51-
'format' => 'ics'
52-
];
52+
protected array $defaultValues;
5353

5454
/**
55-
* Supported sets of values for some parameters
55+
* Supported sets of values for some parameters (from central configuration)
5656
* @var array<string, array<string>>
5757
*/
58-
protected array $supportedValues = [
59-
'format' => ['ics'] // Removed JSON support - deprecated
60-
];
58+
protected array $supportedValues;
6159

6260
/**
63-
* Supported formats (in regexp) for some parameters
61+
* Supported formats (in regexp) for some parameters (from central configuration)
6462
* @var array<string, array<string>>
6563
*/
66-
protected array $supportedFormat = [
67-
'from' => [
68-
"/^now$/",
69-
"/^\+\d+ weeks$/",
70-
"/^[1-2][0-9]{3}-[0-1][0-9]-[0-3][0-9]$/", // date format, e.g. 1997-12-31
71-
"/^[1-2][0-9]{3}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-6][0-9]:[0-6][0-9]$/" // datetime format, e.g. 2015-06-10T10:09:59
72-
],
73-
'to' => [
74-
"/^now$/",
75-
"/^\+\d+ weeks$/",
76-
"/^[1-2][0-9]{3}-[0-1][0-9]-[0-3][0-9]$/",
77-
"/^[1-2][0-9]{3}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-6][0-9]:[0-6][0-9]$/"
78-
],
79-
'limit' => [
80-
"/^[0-9]*$/"
81-
]
82-
];
64+
protected array $supportedFormat;
8365

8466
/**
8567
* Parameters passed to the API
@@ -111,14 +93,21 @@ class CalendarAPI {
11193
* @param string|null $icsMergedFile Optional Pfad zur Merged-ICS-Datei (für Tests)
11294
*/
11395
public function __construct(?string $icsMergedFile = null) {
96+
// Initialize configuration from central config
97+
$this->supportedMethods = CalendarConfig::getSupportedHttpMethods();
98+
$this->mandatory = CalendarConfig::getMandatoryParameters();
99+
$this->defaultValues = CalendarConfig::getDefaultApiParameters();
100+
$this->supportedValues = CalendarConfig::getSupportedParameterValues();
101+
$this->supportedFormat = CalendarConfig::getSupportedParameterFormats();
102+
114103
// Initialize validator
115104
$this->validator = new IcsValidator();
116105

117106
// Set merged ICS file path
118107
$this->icsMergedFile = $icsMergedFile ?? self::DEFAULT_ICS_MERGED_FILE;
119108

120-
// Initialize Sabre handler (always enabled now)
121-
$this->sabreHandler = new SabreVObjectCalendarHandler('Europe/Berlin');
109+
// Initialize Sabre handler with default timezone from central config
110+
$this->sabreHandler = new SabreVObjectCalendarHandler();
122111
}
123112

124113
/**
@@ -286,7 +275,7 @@ protected function processCalendar(): void {
286275
// Generate cache key based on request parameters
287276
$cacheKey = md5(json_encode($this->parameters));
288277
$cacheFile = sys_get_temp_dir() . '/ff_calendar_' . $cacheKey . '.cache';
289-
$cacheLifetime = 3600; // 1 hour cache lifetime (in seconds)
278+
$cacheLifetime = CalendarConfig::getCacheLifetime();
290279

291280
// Check if a valid cache exists
292281
if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < $cacheLifetime)) {
@@ -339,35 +328,15 @@ protected function processCalendarData(): array {
339328
*/
340329
protected function processCalendarDataWithSabre(): array {
341330
try {
342-
// Prepare parameters for Sabre handler
343-
$parameters = [];
344-
345-
// Map 'from' parameter
346-
if (array_key_exists('from', $this->parameters)) {
347-
$parameters['from'] = $this->parameters['from'];
348-
}
331+
// Prepare parameters for Sabre handler - pass through all parameters
332+
// The Sabre handler will handle defaults via central configuration
333+
$parameters = $this->parameters;
349334

350-
// Map 'to' parameter
351-
if (array_key_exists('to', $this->parameters)) {
352-
$parameters['to'] = $this->parameters['to'];
353-
} else {
354-
// Default: 6 months into the future
355-
$parameters['to'] = '+6 months';
356-
}
357-
358-
// Map 'source' parameter
335+
// Map 'source' parameter from sources array
359336
if (!empty($this->sources) && !in_array('all', $this->sources, true)) {
360337
$parameters['source'] = implode(',', $this->sources);
361338
}
362339

363-
// Map 'limit' parameter
364-
if (array_key_exists('limit', $this->parameters)) {
365-
$parameters['limit'] = $this->parameters['limit'];
366-
}
367-
368-
// Map 'format' parameter
369-
$parameters['format'] = $this->parameters['format'] ?? 'ics';
370-
371340
// Use Sabre handler to process the request
372341
$result = $this->sabreHandler->processCalendarRequest($this->icsMergedFile, $parameters);
373342

0 commit comments

Comments
 (0)