Passed
Push — develop ( 8ea77e...10f50e )
by Jens
03:13
created

ApplicationRenderer::renderSitemapComponent()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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