Passed
Push — main ( 9da72c...4c09d2 )
by Felix
20:30 queued 11:53
created

Cache::buildIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
namespace Aoe\Restler\System\TYPO3;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2016 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use TYPO3\CMS\Core\Cache\CacheManager;
29
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
30
use TYPO3\CMS\Core\SingletonInterface;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Extbase\Object\ObjectManager;
33
34
/**
35
 * @package Restler
36
 */
37
class Cache implements SingletonInterface
38
{
39
    /**
40
     * This is the phpdoc-tag, which defines, that the response of API-method is cacheable via TYPO3-caching-framework
41
     *
42
     * Syntax:
43
     * The PHPdoc-comment must look like this:
44
     * @restler_typo3cache_expires [expires-in-seconds]
45
     *
46
     * Examples:
47
     * When API-method should be cacheable in TYPO3 for 10 minutes, than the PHPdoc-comment must look like this:
48
     * @restler_typo3cache_expires 600
49
     * When API-method should be cacheable in TYPO3 for endless time, than the PHPdoc-comment must look like this:
50
     * @restler_typo3cache_expires 0
51
     *
52
     * @var string
53
     */
54
    const API_METHOD_TYPO3CACHE_EXPIRES = 'restler_typo3cache_expires';
55
56
    /**
57
     * This is the phpdoc-tag, which defines, that the cached response of API-method should be tagged with given tags
58
     *
59
     * Syntax:
60
     * The PHPdoc-comment must look like this:
61
     * @restler_typo3cache_tags [comma-separated-list-of-tags]
62
     *
63
     * Example:
64
     * When response of API-method should be tagged with 'tag_a' and 'tag_b', than the PHPdoc-comment must look like this:
65
     * @restler_typo3cache_tags tag_a,tag_b
66
     *
67
     * @var string
68
     */
69
    const API_METHOD_TYPO3CACHE_TAGS = 'restler_typo3cache_tags';
70
71
    /**
72
     * @var FrontendInterface
73
     */
74
    private $cache;
75
76
    /**
77
     * @param CacheManager $cacheManager
78
     */
79 8
    public function __construct(CacheManager $cacheManager = null)
80
    {
81 8
        $cacheManager = $cacheManager ?? GeneralUtility::makeInstance(ObjectManager::class)->get(CacheManager::class);
82 8
        $this->cache = $cacheManager->getCache('cache_restler');
83 8
    }
84
85
    /**
86
     * @param string $requestMethod
87
     * @param array $apiMethodInfoMetadata
88
     * @return boolean
89
     */
90 3
    public function isResponseCacheableByTypo3Cache($requestMethod, array $apiMethodInfoMetadata)
91
    {
92 3
        if ($requestMethod === 'GET' &&
93 3
            array_key_exists(self::API_METHOD_TYPO3CACHE_EXPIRES, $apiMethodInfoMetadata) &&
94 3
            array_key_exists(self::API_METHOD_TYPO3CACHE_TAGS, $apiMethodInfoMetadata)) {
95 1
            return true;
96
        }
97 2
        return false;
98
    }
99
100
    /**
101
     * cache response
102
     *
103
     * @param int $responseCode
104
     * @param string $requestUri
105
     * @param array $requestGetData
106
     * @param array $apiMethodInfoMetadata
107
     * @param string $responseData
108
     * @param $responseFormatClass
109
     * @param array $responseHeaders
110
     */
111 1
    public function cacheResponseByTypo3Cache(
112
        $responseCode,
113
        $requestUri,
114
        array $requestGetData,
115
        array $apiMethodInfoMetadata,
116
        $responseData,
117
        $responseFormatClass,
118
        array $responseHeaders
119
    ) {
120 1
        $identifier = $this->buildIdentifier($requestUri, $requestGetData);
121 1
        $frontendCacheExpires = (integer) $apiMethodInfoMetadata['expires'];
122 1
        $typo3CacheExpires = (integer) $apiMethodInfoMetadata[self::API_METHOD_TYPO3CACHE_EXPIRES];
123 1
        $typo3CacheTags = explode(',', $apiMethodInfoMetadata[self::API_METHOD_TYPO3CACHE_TAGS]);
124
125 1
        $cacheData = array();
126 1
        $cacheData['responseCode'] = $responseCode;
127 1
        $cacheData['requestUri'] = $requestUri;
128 1
        $cacheData['requestGetData'] = $requestGetData;
129 1
        $cacheData['responseData'] = $responseData;
130 1
        $cacheData['responseFormatClass'] = $responseFormatClass;
131 1
        $cacheData['responseHeaders'] = $responseHeaders;
132 1
        $cacheData['frontendCacheExpires'] = $frontendCacheExpires;
133
134 1
        $this->cache->set($identifier, $cacheData, $typo3CacheTags, $typo3CacheExpires);
135 1
    }
136
137
    /**
138
     * @param string $requestUri
139
     * @param array $getData
140
     * @return array
141
     */
142 1
    public function getCacheEntry($requestUri, array $getData)
143
    {
144 1
        $identifier = $this->buildIdentifier($requestUri, $getData);
145 1
        return $this->cache->get($identifier);
146
    }
147
148
    /**
149
     * @param string $requestUri
150
     * @param array $getData
151
     * @return boolean
152
     */
153 2
    public function hasCacheEntry($requestUri, array $getData)
154
    {
155 2
        $identifier = $this->buildIdentifier($requestUri, $getData);
156 2
        return $this->cache->has($identifier);
157
    }
158
159
    /**
160
     * @param string $tag
161
     */
162 1
    public function flushByTag($tag)
163
    {
164 1
        $this->cache->flushByTag($tag);
165 1
    }
166
167
    /**
168
     * @param string $requestUri
169
     * @param array $getData
170
     * @return string
171
     */
172 4
    private function buildIdentifier($requestUri, array $getData)
173
    {
174 4
        return md5($requestUri . serialize($getData));
175
    }
176
}
177