Completed
Push — development ( c1e1bc...13d7a7 )
by Torben
01:23
created

EventCacheService::flushEventCache()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 4
nc 8
nop 2
1
<?php
2
namespace DERHANSEN\SfEventMgt\Service;
3
4
/*
5
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.txt file that was distributed with this source code.
9
 */
10
11
use TYPO3\CMS\Core\Cache\CacheManager;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
14
/**
15
 * Class CacheService
16
 *
17
 * @author Torben Hansen <[email protected]>
18
 */
19
class EventCacheService
20
{
21
    /**
22
     * Adds cache tags to page cache by event records.
23
     *
24
     * Following cache tags will be added to tsfe:
25
     * "tx_sfeventmgt_uid_[event:uid]"
26
     *
27
     * @param array $eventRecords array with event records
28
     * @return void
29
     */
30
    public function addCacheTagsByEventRecords(array $eventRecords)
31
    {
32
        $cacheTags = [];
33
        foreach ($eventRecords as $event) {
34
            // cache tag for each event record
35
            $cacheTags[] = 'tx_sfeventmgt_uid_' . $event->getUid();
36
        }
37
        if (count($cacheTags) > 0) {
38
            self::getTypoScriptFrontendController()->addCacheTags($cacheTags);
39
        }
40
    }
41
42
    /**
43
     * Adds page cache tags by used storagePages.
44
     * This adds tags with the scheme tx_sfeventmgt_pid_[event:pid]
45
     *
46
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $demand
47
     * @return void
48
     */
49
    public function addPageCacheTagsByEventDemandObject(\DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $demand)
50
    {
51
        $cacheTags = [];
52
        if ($demand->getStoragePage()) {
53
            // Add cache tags for each storage page
54
            foreach (GeneralUtility::trimExplode(',', $demand->getStoragePage()) as $pageId) {
55
                $cacheTags[] = 'tx_sfeventmgt_pid_' . $pageId;
56
            }
57
        }
58
        if (count($cacheTags) > 0) {
59
            self::getTypoScriptFrontendController()->addCacheTags($cacheTags);
60
        }
61
    }
62
63
    public function flushEventCache(int $eventUid = 0, int $eventPid = 0)
64
    {
65
        $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
66
        $cacheTagsToFlush = [];
67
68
        if ($eventUid > 0) {
69
            $cacheTagsToFlush[] = 'tx_sfeventmgt_uid_' . $eventUid;
70
        }
71
        if ($eventPid > 0) {
72
            $cacheTagsToFlush[] = 'tx_sfeventmgt_pid_' . $eventPid;
73
        }
74
75
        foreach ($cacheTagsToFlush as $cacheTagToFlush) {
76
            $cacheManager->flushCachesInGroupByTag('pages', $cacheTagToFlush);
77
        }
78
    }
79
80
    /**
81
     * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
82
     */
83
    protected function getTypoScriptFrontendController()
84
    {
85
        return $GLOBALS['TSFE'] ?: null;
86
    }
87
}
88