Passed
Push — master ( 1a2f46...8e46a2 )
by Jens
04:11
created

handleSitemapComponentCaching()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 9
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 12
rs 8.2222
1
<?php
2
/**
3
 * Created by: Jens
4
 * Date: 12-10-2017
5
 */
6
7
namespace CloudControl\Cms\cc\application;
8
9
10
use CloudControl\Cms\cc\Application;
11
use CloudControl\Cms\cc\Request;
12
use CloudControl\Cms\components\CachableBaseComponent;
13
use CloudControl\Cms\components\CmsComponent;
14
use CloudControl\Cms\storage\Storage;
15
16
class ApplicationRenderer
17
{
18
    protected $storage;
19
    protected $request;
20
    protected $application;
21
    const HEADER_POWERED_BY = 'Cloud Control - https://getcloudcontrol.org';
22
23
    /**
24
     * ApplicationRenderer constructor.
25
     * @param Application $application
26
     * @param Storage $storage
27
     * @param Request $request
28
     */
29
    public function __construct(Application $application, Storage $storage, Request $request)
30
    {
31
        $this->storage = $storage;
32
        $this->request = $request;
33
        $this->application = $application;
34
    }
35
36
    /**
37
     * Loop through all application components and render them
38
     * @param array $applicationComponents
39
     */
40
    public function renderApplicationComponents($applicationComponents)
41
    {
42
        foreach ($applicationComponents as $applicationComponent) {
43
            $applicationComponent->{'object'}->render();
44
        }
45
    }
46
47
    /**
48
     * Loop through all (matched) sitemap components and render them
49
     * @param array $matchedSitemapItems
50
     * @throws \Exception
51
     */
52
    public function renderSitemapComponents($matchedSitemapItems)
53
    {
54
        if (count($matchedSitemapItems) < 1) {
55
            return;
56
        }
57
58
        $sitemapItem = current($matchedSitemapItems);
59
        $this->renderSitemapComponent($sitemapItem);
60
    }
61
62
    /**
63
     * Set the default caching of pages
64
     * @param string $intervalString
65
     */
66
    public function setCachingHeaders($intervalString = CachableBaseComponent::DEFAULT_MAXAGE)
67
    {
68
        $expires = new \DateTime();
69
        $interval = new \DateInterval($intervalString);
70
        $maxAge = date_create('@0')->add($interval)->getTimestamp();
71
        $expires = $expires->add($interval);
72
        header('X-Powered-By: ' . self::HEADER_POWERED_BY);
73
        header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', $expires->getTimestamp()));
74
        header('Cache-Control: max-age=' . $maxAge);
75
        header('Pragma: cache');
76
    }
77
78
    /**
79
     * Set non caching
80
     * @throws \Exception
81
     */
82
    public function setNotCachingHeaders()
83
    {
84
        header('X-Powered-By: ' . self::HEADER_POWERED_BY);
85
        header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
86
        header("Cache-Control: post-check=0, pre-check=0", false);
87
        header("Pragma: no-cache");
88
    }
89
90
    /**
91
     * @param $sitemapItem
92
     * @throws \Exception
93
     */
94
    private function renderSitemapComponent($sitemapItem)
95
    {
96
        $isCachable = ($sitemapItem->object instanceof CachableBaseComponent) && !CmsComponent::isCmsLoggedIn();
97
98
        $this->handleSitemapComponentCaching($sitemapItem, $isCachable);
99
100
        echo $sitemapItem->object->get();
101
        ob_end_flush();
102
        exit;
103
    }
104
105
    /**
106
     * @param $sitemapItem
107
     * @param $isCachable
108
     * @throws \Exception
109
     */
110
    private function handleSitemapComponentCaching($sitemapItem, $isCachable)
111
    {
112
        if (($isCachable && !$sitemapItem->object->isCachable()) || $isCachable === false) {
113
            $sitemapItem->object->render($this->application);
114
            ob_clean();
115
            $this->setNotCachingHeaders();
116
        } elseif ($isCachable && $sitemapItem->object->isCachable()) {
117
            if (!$sitemapItem->object->isCacheValid()) {
118
                $sitemapItem->object->render($this->application);
119
            }
120
            ob_clean();
121
            $this->setCachingHeaders($sitemapItem->object->getMaxAge());
122
        }
123
    }
124
}