Completed
Push — 4.0 ( 354070...1bb9b8 )
by Marco
05:08
created

ServerCache::read()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 16
c 0
b 0
f 0
ccs 0
cts 7
cp 0
rs 10
cc 3
nc 3
nop 2
crap 12
1
<?php namespace Comodojo\Dispatcher\Cache;
2
3
use \Comodojo\Dispatcher\Request\Model as Request;
4
use \Comodojo\Dispatcher\Router\Route;
5
use \Comodojo\Dispatcher\Response\Model as Response;
6
7
/**
8
 * @package     Comodojo Dispatcher
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @author      Marco Castiello <[email protected]>
11
 * @license     MIT
12
 *
13
 * LICENSE:
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 */
23
24
class ServerCache extends AbstractCache {
25
26
    // @NOTE: Server cache will not consider cacheable POST or PUT requests
27
    //        because of dispatcher internal structure: if post request is cached
28
    //        subsequent requests will never reach the service.
29
    protected static $cachable_methods = ['GET', 'HEAD'];
30
31
    protected static $cachable_statuses = [200, 203, 300, 301, 302, 404, 410];
32
33
    protected static $cache_namespace = "DISPATCHERSERVICES";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal DISPATCHERSERVICES does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
34
35
    protected $bypass = false;
36
37
    public function read(
38
        Request $request,
39
        Response $response
40
    ) {
41
42
        if ( $this->bypass === true ) return false;
43
44
        $name = self::getCacheName($request);
45
46
        $cache_object = $this->getCache()->setNamespace(self::$cache_namespace)->get($name);
47
48
        if (is_null($cache_object)) return false;
49
50
        $response->import($cache_object);
51
52
        return true;
53
54
    }
55
56
    public function dump(
57
        Request $request,
58
        Response $response,
59
        Route $route
60
    ) {
61
62
        if ( $this->bypass === true ) return;
63
64
        $cache = strtoupper($route->getParameter('cache'));
65
66
        $ttl = $route->getParameter('ttl');
67
68
        $name = self::getCacheName($request);
69
70
        $method = (string)$request->getMethod();
71
72
        $status = $response->getStatus()->get();
73
74
        if (
75
            ($cache == 'SERVER' || $cache == 'BOTH') &&
0 ignored issues
show
Coding Style introduced by
Operator == prohibited; use === instead
Loading history...
76
            in_array($method, self::$cachable_methods) &&
77
            in_array($status, self::$cachable_statuses)
78
        ) {
79
80
            $this->getCache()
81
                ->setNamespace(self::$cache_namespace)
82
                ->set($name, $response->export(), $ttl === null ? self::DEFAULTTTL : intval($ttl));
83
84
        }
85
86
    }
87
88
    public function bypassCache() {
89
90
        $this->bypass = true;
91
        return $this;
92
93
    }
94
95
    private static function getCacheName(Request $request) {
96
97
        return md5((string)$request->getMethod().(string)$request->getUri());
98
99
    }
100
101
}
102