Passed
Push — develop ( 56c45f...63c71d )
by Jens
05:34
created

ApplicationRenderer::renderSitemapComponent()   C

Complexity

Conditions 8
Paths 0

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 13
c 1
b 0
f 0
nc 0
nop 1
dl 0
loc 19
rs 6.6666
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
        if (($isCachable && !$sitemapItem->object->isCachable()) || $isCachable === false) {
99
            $sitemapItem->object->render($this->application);
100
            ob_clean();
101
            $this->setNotCachingHeaders();
102
        } elseif ($isCachable && $sitemapItem->object->isCachable()) {
103
            if (!$sitemapItem->object->isCacheValid()) {
104
                $sitemapItem->object->render($this->application);
105
            }
106
            ob_clean();
107
            $this->setCachingHeaders($sitemapItem->object->getMaxAge());
108
        }
109
110
        echo $sitemapItem->object->get();
111
        ob_end_flush();
112
        exit;
113
    }
114
}