Passed
Push — develop ( aa4acd...adb2d3 )
by Jens
17:37
created

CachableBaseComponent::isLoggedIn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * Created by jensk on 25-10-2017.
4
 */
5
6
namespace CloudControl\Cms\components;
7
8
9
use CloudControl\Cms\cc\Request;
10
use CloudControl\Cms\components\cms\CmsConstants;
11
use CloudControl\Cms\storage\Cache;
12
13
class CachableBaseComponent extends BaseComponent
14
{
15
    const DEFAULT_MAXAGE = 'P7D';
16
17
    const PARAMETER_CACHABLE = 'cachable';
18
    const PARAMETER_MAXAGE = 'maxage';
19
20
    protected $cachable = true;
21
    protected $maxAge = self::DEFAULT_MAXAGE;
22
    protected $cacheValidity;
23
    protected $cache;
24
25
    /**
26
     * CachableBaseComponent constructor.
27
     * @param string $template
28
     * @param Request $request
29
     * @param array $parameters
30
     * @param $matchedSitemapItem
31
     */
32
    public function __construct($template = '', Request $request, $parameters = array(), $matchedSitemapItem)
33
    {
34
        parent::__construct($template, $request, $parameters, $matchedSitemapItem);
35
36
        if (isset($this->parameters[self::PARAMETER_CACHABLE]) && $this->parameters[self::PARAMETER_CACHABLE] === 'false') {
37
            $this->cachable = false;
38
        }
39
40
        if (isset($this->parameters[self::PARAMETER_MAXAGE])) {
41
            $this->maxAge = $this->parameters[self::PARAMETER_MAXAGE];
42
        }
43
44
        $this->setCacheValidity();
45
    }
46
47
    public function get()
48
    {
49
        if ($this->isCachable() && $this->isCacheValid()) {
50
            return $this->cache->contents;
51
        } else if ($this->isCachable() && !$this->isCacheValid()) {
52
            $this->createCache($this->renderedContent);
53
        }
54
        return $this->renderedContent;
55
    }
56
57
58
    /**
59
     * @return bool
60
     */
61
    public function isCachable()
62
    {
63
        return $this->cachable;
64
    }
65
66
    /**
67
     * @return bool
68
     */
69
    public function isCacheValid()
70
    {
71
        if ($this->isLoggedIn()) {
72
            return false;
73
        }
74
        if ($this->cacheValidity === null) {
75
            return $this->setCacheValidity();
76
        }
77
        return $this->cacheValidity;
78
    }
79
80
    private function isLoggedIn()
81
    {
82
        return isset($_SESSION[CmsConstants::SESSION_PARAMETER_CLOUD_CONTROL]);
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getMaxAge()
89
    {
90
        return $this->maxAge;
91
    }
92
93
    protected function setCacheValidity()
94
    {
95
        if ($this->cacheValidity === null) {
96
            $this->cache = Cache::getInstance()->getCacheForPath(Request::$requestUri);
97
            $cacheExists = $this->cache !== false;
98
            $cacheExpired = false;
99
            if ($cacheExists) {
100
                $cacheCreationStamp = (int) $this->cache->creationStamp;
101
                $currentTime = time();
102
                $cacheAge = $currentTime - $cacheCreationStamp;
103
                $maxAgeInterval = new \DateInterval($this->maxAge);
104
                $maxAgeSeconds = date_create('@0')->add($maxAgeInterval)->getTimestamp();
105
                $cacheExpired = $cacheAge > $maxAgeSeconds;
106
            }
107
            $this->cacheValidity = $cacheExists && !$cacheExpired;
108
        }
109
        return $this->cacheValidity;
110
    }
111
112
    private function createCache($renderedContent)
113
    {
114
        Cache::getInstance()->setCacheForPath(Request::$requestUri, $renderedContent);
115
    }
116
117
}