Completed
Push — master ( 465cbd...e0d13e )
by Marco
06:44 queued 11s
created

ServerCache::dump()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
ccs 0
cts 13
cp 0
rs 8.439
cc 6
eloc 12
nc 2
nop 3
crap 42
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";
34
35 1
    public function read(
36
        Request $request,
37
        Response $response
38
    ) {
39
40 1
        $name = self::getCacheName($request);
41
42 1
        $cache_object = $this->getCache()->setNamespace(self::$cache_namespace)->get($name);
43
44 1
        if (is_null($cache_object)) return false;
45
46
        $response->import($cache_object);
47
48
        return true;
49
50
    }
51
52
    public function dump(
53
        Request $request,
54
        Response $response,
55
        Route $route
56
    ) {
57
58
        $cache = strtoupper($route->getParameter('cache'));
59
60
        $ttl = $route->getParameter('ttl');
61
62
        $name = self::getCacheName($request);
63
64
        $method = (string)$request->getMethod();
65
66
        $status = $response->getStatus()->get();
67
68
        if (
69
            ($cache == 'SERVER' || $cache == 'BOTH') &&
70
            in_array($method, self::$cachable_methods) &&
71
            in_array($status, self::$cachable_statuses)
72
        ) {
73
74
            $this->getCache()
75
                ->setNamespace(self::$cache_namespace)
76
                ->set($name, $response->export(), $ttl === null ? self::DEFAULTTTL : intval($ttl));
77
78
        }
79
80
    }
81
82 1
    private static function getCacheName(Request $request) {
83
84 1
        return md5((string)$request->getMethod().(string)$request->getUri());
85
86
    }
87
88
}
89