Completed
Push — master ( 5d76d8...23accf )
by
unknown
04:14
created

Cache::getCacheEntry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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
32
/**
33
 * @package Restler
34
 */
35
class Cache implements SingletonInterface
36
{
37
    /**
38
     * This is the phpdoc-tag, which defines, that the response of API-method is cacheable via TYPO3-caching-framework
39
     *
40
     * Syntax:
41
     * The PHPdoc-comment must look like this:
42
     * @restler_typo3cache_expires [expires-in-seconds]
43
     *
44
     * Examples:
45
     * When API-method should be cacheable in TYPO3 for 10 minutes, than the PHPdoc-comment must look like this:
46
     * @restler_typo3cache_expires 600
47
     * When API-method should be cacheable in TYPO3 for endless time, than the PHPdoc-comment must look like this:
48
     * @restler_typo3cache_expires 0
49
     *
50
     * @var string
51
     */
52
    const API_METHOD_TYPO3CACHE_EXPIRES = 'restler_typo3cache_expires';
53
54
    /**
55
     * This is the phpdoc-tag, which defines, that the cached response of API-method should be tagged with given tags
56
     *
57
     * Syntax:
58
     * The PHPdoc-comment must look like this:
59
     * @restler_typo3cache_tags [comma-separated-list-of-tags]
60
     *
61
     * Example:
62
     * When response of API-method should be tagged with 'tag_a' and 'tag_b', than the PHPdoc-comment must look like this:
63
     * @restler_typo3cache_tags tag_a,tag_b
64
     *
65
     * @var string
66
     */
67
    const API_METHOD_TYPO3CACHE_TAGS = 'restler_typo3cache_tags';
68
69
    /**
70
     * @var FrontendInterface
71
     */
72
    private $cache;
73
74
    /**
75
     * @param CacheManager $cacheManager
76
     */
77 8
    public function __construct(CacheManager $cacheManager)
78
    {
79 8
        $this->cache = $cacheManager->getCache('cache_restler');
80 8
    }
81
82
    /**
83
     * @param string $requestMethod
84
     * @param array $apiMethodInfoMetadata
85
     * @return boolean
86
     */
87 3
    public function isResponseCacheableByTypo3Cache($requestMethod, array $apiMethodInfoMetadata)
88
    {
89 3
        if ($requestMethod === 'GET' &&
90 3
            array_key_exists(self::API_METHOD_TYPO3CACHE_EXPIRES, $apiMethodInfoMetadata) &&
91 3
            array_key_exists(self::API_METHOD_TYPO3CACHE_TAGS, $apiMethodInfoMetadata)) {
92 1
            return true;
93
        }
94 2
        return false;
95
    }
96
97
    /**
98
     * cache response
99
     *
100
     * @param string $requestUri
101
     * @param array $requestGetData
102
     * @param array $apiMethodInfoMetadata
103
     * @param string $responseData
104
     * @param $responseFormatClass
105
     * @param array $responseHeaders
106
     */
107 1
    public function cacheResponseByTypo3Cache(
108
        $requestUri, array $requestGetData,
109
        array $apiMethodInfoMetadata, $responseData, $responseFormatClass, array $responseHeaders)
110
    {
111 1
        $identifier = $this->buildIdentifier($requestUri, $requestGetData);
112 1
        $frontendCacheExpires = (integer) $apiMethodInfoMetadata['expires'];
113 1
        $typo3CacheExpires = (integer) $apiMethodInfoMetadata[self::API_METHOD_TYPO3CACHE_EXPIRES];
114 1
        $typo3CacheTags = explode(',', $apiMethodInfoMetadata[self::API_METHOD_TYPO3CACHE_TAGS]);
115
116 1
        $cacheData = array();
117 1
        $cacheData['requestUri'] = $requestUri;
118 1
        $cacheData['requestGetData'] = $requestGetData;
119 1
        $cacheData['responseData'] = $responseData;
120 1
        $cacheData['responseFormatClass'] = $responseFormatClass;
121 1
        $cacheData['responseHeaders'] = $responseHeaders;
122 1
        $cacheData['frontendCacheExpires'] = $frontendCacheExpires;
123
124 1
        $this->cache->set($identifier, $cacheData, $typo3CacheTags, $typo3CacheExpires);
125 1
    }
126
127
    /**
128
     * @param string $requestUri
129
     * @param array $getData
130
     * @return array
131
     */
132 1
    public function getCacheEntry($requestUri, array $getData)
133
    {
134 1
        $identifier = $this->buildIdentifier($requestUri, $getData);
135 1
        return $this->cache->get($identifier);
136
    }
137
138
    /**
139
     * @param string $requestUri
140
     * @param array $getData
141
     * @return boolean
142
     */
143 2
    public function hasCacheEntry($requestUri, array $getData)
144
    {
145 2
        $identifier = $this->buildIdentifier($requestUri, $getData);
146 2
        return $this->cache->has($identifier);
147
    }
148
149
    /**
150
     * @param string $tag
151
     */
152 1
    public function flushByTag($tag)
153
    {
154 1
        $this->cache->flushByTag($tag);
155 1
    }
156
157
    /**
158
     * @param string $requestUri
159
     * @param array $getData
160
     * @return string
161
     */
162 4
    private function buildIdentifier($requestUri, array $getData)
163
    {
164 4
        return md5($requestUri . serialize($getData));
165
    }
166
}
167