Completed
Push — master ( 486efe...95fe8f )
by Tim
09:28
created

IcsReaderService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 9 2
A getCachedUrlFile() 0 8 3
A getCheckedCacheFolder() 0 9 2
1
<?php
2
3
/**
4
 * ICS Service.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\Service;
9
10
use HDNET\Calendarize\Utility\DateTimeUtility;
11
use JMBTechnologyLimited\ICalDissect\ICalParser;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
14
/**
15
 * ICS Service.
16
 */
17
class IcsReaderService extends AbstractService
18
{
19
    /**
20
     * Get the ICS events in an array.
21
     *
22
     * @param string $paramUrl
23
     *
24
     * @return array
25
     */
26
    public function toArray($paramUrl)
27
    {
28
        $tempFileName = $this->getCachedUrlFile($paramUrl);
29
        $backend = new ICalParser();
30
        if ($backend->parseFromFile($tempFileName)) {
31
            return $backend->getEvents();
32
        }
33
        return [];
34
    }
35
36
    /**
37
     * Get cached URL file
38
     *
39
     * @param string $url
40
     * @return string
41
     */
42
    protected function getCachedUrlFile(string $url):string {
43
        $tempFileName = $this->getCheckedCacheFolder() . \md5($url);
44
        if (!\is_file($tempFileName) || \filemtime($tempFileName) < (\time() - DateTimeUtility::SECONDS_HOUR)) {
45
            $icsFile = GeneralUtility::getUrl($url);
46
            GeneralUtility::writeFile($tempFileName, $icsFile);
47
        }
48
        return $tempFileName;
49
    }
50
51
    /**
52
     * Return the cache folder and check if the folder exists.
53
     *
54
     * @return string
55
     */
56
    protected function getCheckedCacheFolder():string
57
    {
58
        $cacheFolder = GeneralUtility::getFileAbsFileName('typo3temp/var/transient/calendarize/');
59
        if (!\is_dir($cacheFolder)) {
60
            GeneralUtility::mkdir_deep($cacheFolder);
61
        }
62
63
        return $cacheFolder;
64
    }
65
}
66