Completed
Push — master ( 2700b4...4ccb05 )
by Torben
04:03 queued 02:37
created

EventCacheService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 3
dl 0
loc 76
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addCacheTagsByEventRecords() 0 11 3
A addPageCacheTagsByEventDemandObject() 0 13 4
A flushEventCache() 0 16 4
A getTypoScriptFrontendController() 0 4 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
    /**
64
     * Flushes the page cache by event tags for the given event uid and pid
65
     *
66
     * @param int $eventUid
67
     * @param int $eventPid
68
     * @return void
69
     */
70
    public function flushEventCache(int $eventUid = 0, int $eventPid = 0)
71
    {
72
        $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
73
        $cacheTagsToFlush = [];
74
75
        if ($eventUid > 0) {
76
            $cacheTagsToFlush[] = 'tx_sfeventmgt_uid_' . $eventUid;
77
        }
78
        if ($eventPid > 0) {
79
            $cacheTagsToFlush[] = 'tx_sfeventmgt_pid_' . $eventPid;
80
        }
81
82
        foreach ($cacheTagsToFlush as $cacheTagToFlush) {
83
            $cacheManager->flushCachesInGroupByTag('pages', $cacheTagToFlush);
84
        }
85
    }
86
87
    /**
88
     * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
89
     */
90
    protected function getTypoScriptFrontendController()
91
    {
92
        return $GLOBALS['TSFE'] ?: null;
93
    }
94
}
95