Passed
Push — develop ( ad1a9e...8ea77e )
by Jens
02:45
created

ApplicationRenderer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 89
rs 10
wmc 12
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A renderApplicationComponents() 0 6 2
C renderSitemapComponents() 0 24 7
A setCachingHeaders() 0 11 1
A setNotCachingHeaders() 0 7 1
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
        foreach ($matchedSitemapItems as $sitemapItem) {
54
            if (($sitemapItem->object instanceof CachableBaseComponent
55
                && !$sitemapItem->object->isCachable()) | !$sitemapItem->object instanceof CachableBaseComponent) {
56
                $sitemapItem->object->render($this->application);
57
                ob_clean();
58
                $this->setNotCachingHeaders();
59
                echo $sitemapItem->object->get();
60
                ob_end_flush();
61
                exit;
62
            } elseif ($sitemapItem->object instanceof CachableBaseComponent
63
                && $sitemapItem->object->isCachable()) {
64
                if (!$sitemapItem->object->isCacheValid()) {
65
                    $sitemapItem->object->render($this->application);
66
                }
67
                ob_clean();
68
                $this->setCachingHeaders($sitemapItem->object->getMaxAge());
69
                echo $sitemapItem->object->get();
70
                ob_end_flush();
71
                exit;
72
            }
73
        }
74
    }
75
76
    /**
77
     * Set the default caching of pages
78
     * @param string $intervalString
79
     */
80
    public function setCachingHeaders($intervalString = CachableBaseComponent::DEFAULT_MAXAGE)
81
    {
82
        $expires = new \DateTime();
83
        $interval = new \DateInterval($intervalString);
84
        $maxAge = date_create('@0')->add($interval)->getTimestamp();
85
        $expires = $expires->add($interval);
86
        header('X-Powered-By: ' . self::HEADER_POWERED_BY);
87
        header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', $expires->getTimestamp()));
88
        header('Cache-Control: max-age=' . $maxAge);
89
        header('Pragma: cache');
90
    }
91
92
    /**
93
     * Set non caching
94
     * @throws \Exception
95
     */
96
    public function setNotCachingHeaders()
97
    {
98
        header('X-Powered-By: ' . self::HEADER_POWERED_BY);
99
        header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
100
        header("Cache-Control: post-check=0, pre-check=0", false);
101
        header("Pragma: no-cache");
102
    }
103
}