116 lines
2.1 KiB
PHP
116 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace davigil\sources;
|
|
|
|
require_once('helpers/string.php');
|
|
require_once('helpers/cache.php');
|
|
require_once('helpers/pit.php');
|
|
require_once('helpers/ics.php');
|
|
|
|
|
|
/**
|
|
*/
|
|
class class_source_ics_feed
|
|
implements interface_source
|
|
{
|
|
|
|
/**
|
|
*/
|
|
private string $url;
|
|
|
|
|
|
/**
|
|
*/
|
|
public function __construct(
|
|
string $url
|
|
)
|
|
{
|
|
$this->url = $url;
|
|
$this->cache_file = new \davigil\helpers\cache\class_cache_encoded(
|
|
new \davigil\helpers\cache\class_cache_file('data'),
|
|
fn($value) => \json_encode($value),
|
|
fn($value_encoded) => \json_decode($value_encoded, true)
|
|
);
|
|
$this->cache_memory = new \davigil\helpers\cache\class_cache_memory();
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
private function retrieve(
|
|
) : array
|
|
{
|
|
$client = new \Sabre\HTTP\Client();
|
|
$request = new \Sabre\HTTP\Request(
|
|
'GET',
|
|
$this->url,
|
|
[],
|
|
null
|
|
);
|
|
$response = $client->send($request);
|
|
$status_code = $response->getStatus();
|
|
switch ($status_code)
|
|
{
|
|
case 200:
|
|
{
|
|
$ics = $response->getBody();
|
|
$vcalendar = \davigil\helpers\ics\vcalendar_decode($ics);
|
|
$data = \array_map(
|
|
fn($vevent) => [
|
|
'id' => $vevent->uid,
|
|
'title' => $vevent->summary,
|
|
'begin' => \davigil\helpers\ics\datetime_to_unix_timestamp($vevent->dtstart->value),
|
|
'end' => (
|
|
($vevent->dtend === null)
|
|
?
|
|
null
|
|
:
|
|
\davigil\helpers\ics\datetime_to_unix_timestamp($vevent->dtend->value)
|
|
),
|
|
'location' => $vevent->location,
|
|
'description' => $vevent->description,
|
|
'tags' => $vevent->categories,
|
|
],
|
|
$vcalendar->events
|
|
);
|
|
return $data;
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
throw (new \Exception(\sprintf('unhandled response status code: %u', $status_code)));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* [implementation]
|
|
*/
|
|
public function get(
|
|
array $parameters
|
|
) : array
|
|
{
|
|
$key = $this->url;
|
|
return \davigil\helpers\cache\get(
|
|
$this->cache_memory,
|
|
$key,
|
|
fn() => \davigil\helpers\cache\get(
|
|
$this->cache_file,
|
|
$key,
|
|
fn() => $this->retrieve(),
|
|
[
|
|
'ttl' => (60 * 5),
|
|
]
|
|
),
|
|
[
|
|
'ttl' => null,
|
|
]
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
?>
|