url = $url; $this->lifetime = $lifetime; $this->combine = $combine; $this->cache_file = \davigil\helpers\call\convey( new \davigil\helpers\cache\class_cache_file('data'), [ fn($x) => new \davigil\helpers\cache\class_cache_encoded( $x, fn($value) => \json_encode($value), fn($value_encoded) => \json_decode($value_encoded, true) ), ] ); $this->cache_memory = \davigil\helpers\call\convey( (new \davigil\helpers\cache\class_cache_memory()), [ ] ); } /** */ private function vcalendar_to_calendar( \davigil\helpers\ics\struct_vcalendar $vcalendar ) : \davigil\model\struct_calendar { return ( new \davigil\model\struct_calendar( \array_map( fn($vevent) => (new \davigil\model\struct_event( // id $vevent->uid, // title ( $this->combine ? \sprintf( '%s%s', $vevent->summary, \implode( '', \array_map( fn($category) => \sprintf(' (%s)', $category), $vevent->categories ) ) ) : $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 ( $this->combine ? ['combined'] : $vevent->categories ) )), $vcalendar->events ) ) ); } /** */ private function retrieve( ) { $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); $calendar = $this->vcalendar_to_calendar($vcalendar); $calendar_raw = \davigil\model\calendar_to_raw($calendar); return $calendar_raw; break; } default: { throw ( new \Exception( \sprintf( 'unhandled response status code: %u', $status_code ) ) ); break; } } } /** * [implementation] */ public function get( array $parameters ) : \davigil\model\struct_calendar { $key = $this->url; $f1 = fn() => $this->retrieve(); $f2 = fn() => \davigil\helpers\cache\get( $this->cache_file, $key, $f1, [ 'ttl' => $this->lifetime, ] ); $f3 = fn() => \davigil\helpers\cache\get( $this->cache_memory, $key, $f2, [ 'ttl' => null, ] ); return \davigil\model\calendar_from_raw( ($f3)() ); } } ?>