Completed
Pull Request — master (#42)
by Vladimir
02:25
created

PageView   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 93.22%

Importance

Changes 0
Metric Value
dl 0
loc 206
ccs 55
cts 59
cp 0.9322
rs 10
c 0
b 0
f 0
wmc 18
lcom 2
cbo 8

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getChildren() 0 4 1
A getContent() 0 4 1
A getType() 0 4 1
A getUrl() 0 4 1
A createJail() 0 11 2
A getJailedChildren() 0 11 2
A create() 0 18 3
A createVirtual() 0 14 2
B createRedirect() 0 24 4
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Document;
9
10
use allejo\stakx\FrontMatter\Document;
11
use allejo\stakx\System\Filesystem;
12
use allejo\stakx\System\StakxResource;
13
use org\bovigo\vfs\vfsStream;
14
use org\bovigo\vfs\vfsStreamWrapper;
15
use Symfony\Component\Yaml\Yaml;
16
17
class PageView extends Document
18
{
19
    const REPEATER_TYPE = 0;
20
    const DYNAMIC_TYPE = 1;
21
    const STATIC_TYPE = 2;
22
23
    /**
24
     * @var Filesystem
25
     */
26
    private static $fileSys;
27
28
    /**
29
     * @var string
30
     */
31
    protected $type;
32
33
    /**
34
     * @var PageView[]
35
     */
36
    private $children;
37
38
    /**
39
     * @var JailedDocument
40
     */
41
    private $jailInstance;
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 31
    public function __construct($filePath)
47
    {
48 31
        parent::__construct($filePath);
49
50 31
        $this->children = array();
51 31
        $this->type = self::STATIC_TYPE;
0 ignored issues
show
Documentation Bug introduced by
The property $type was declared of type string, but self::STATIC_TYPE is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
52 31
    }
53
54
    //
55
    // Twig Jail
56
    // =========
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 24
    public function createJail()
62
    {
63 24
        if (is_null($this->jailInstance))
64 24
        {
65 24
            $this->jailInstance = (new JailedDocument($this, array_merge(self::$whiteListFunctions, array(
66 24
                'getUrl',
67 24
            )), array('getChildren' => 'getJailedChildren')));
68 24
        }
69
70 24
        return $this->jailInstance;
71
    }
72
73 3
    public function getJailedChildren()
74
    {
75 3
        $children = $this->children;
76
77 3
        foreach ($children as &$child)
78
        {
79 3
            $child = $child->createJail();
80 3
        }
81
82 3
        return $children;
83
    }
84
85
    //
86
    // Getters
87
    // =======
88
89
    /**
90
     * Get child PageViews.
91
     *
92
     * A child is defined as a static PageView whose URL has a parent. For example, a PageView with a URL of
93
     * `/gallery/france/` would have the PageView whose URL is `/gallery` as a parent.
94
     *
95
     * @return PageView[]
96
     */
97 3
    public function &getChildren()
98
    {
99 3
        return $this->children;
100
    }
101
102
    /**
103
     * @return string Twig body
104
     */
105 13
    public function getContent()
106
    {
107 13
        return $this->bodyContent;
108
    }
109
110
    /**
111
     * Returns the type of the PageView.
112
     *
113
     * @return string
114
     */
115 19
    public function getType()
116
    {
117 19
        return $this->type;
118
    }
119
120
    /**
121
     * A fallback for the site menus that use the `url` field.
122
     *
123
     * @deprecated 0.1.0
124
     *
125
     * @todo       Remove this in the next major release
126
     */
127
    public function getUrl()
128
    {
129
        return $this->getPermalink();
130
    }
131
132
    //
133
    // Factory
134
    // =======
135
136
    /**
137
     * Create the appropriate object type when parsing a PageView.
138
     *
139
     * @param string $filePath The path to the file that will be parsed into a PageView
140
     *
141
     * @return DynamicPageView|PageView|RepeaterPageView
142
     */
143 6
    public static function create($filePath)
144
    {
145 6
        $instance = new self($filePath);
146
147 6
        if (isset($instance->getFrontMatter(false)['collection']))
148 6
        {
149 4
            return new DynamicPageView($filePath);
150
        }
151
152 2
        $instance->getFrontMatter();
153
154 2
        if ($instance->hasExpandedFrontMatter())
155 2
        {
156
            return new RepeaterPageView($filePath);
157
        }
158
159 2
        return $instance;
160
    }
161
162
    //
163
    // Virtual PageViews
164
    // =================
165
166
    /**
167
     * Create a virtual PageView.
168
     *
169
     * @param array  $frontMatter The Front Matter that this virtual PageView will have
170
     * @param string $body        The body of the virtual PageView
171
     *
172
     * @return PageView
173
     */
174 4
    public static function createVirtual($frontMatter, $body)
175
    {
176 4
        if (vfsStreamWrapper::getRoot() == null)
177 4
        {
178
            vfsStream::setup();
179
        }
180
181 4
        $redirectFile = vfsStream::newFile(sprintf('redirect_%s.html.twig', uniqid()));
182
        $redirectFile
183 4
            ->setContent(sprintf(self::TEMPLATE, Yaml::dump($frontMatter, 2), $body))
184 4
            ->at(vfsStreamWrapper::getRoot());
185
186 4
        return new self($redirectFile->url());
187
    }
188
189
    /**
190
     * Create a virtual PageView to create redirect files.
191
     *
192
     * @param string      $redirectFrom     The URL that will be redirecting to the target location
193
     * @param string      $redirectTo       The URL of the destination
194
     * @param string|bool $redirectTemplate The path to the template
195
     *
196
     * @return PageView A virtual PageView with the redirection template
197
     */
198 4
    public static function createRedirect($redirectFrom, $redirectTo, $redirectTemplate = false)
199
    {
200 4
        if (is_null(self::$fileSys))
201 4
        {
202 1
            self::$fileSys = new Filesystem();
203 1
        }
204
205
        $frontMatter = array(
206 4
            'permalink' => $redirectFrom,
207 4
            'redirect'  => $redirectTo,
208 4
            'menu'      => false,
209 4
        );
210
211 4
        if (!$redirectTemplate || !self::$fileSys->exists(self::$fileSys->absolutePath($redirectTemplate)))
212 4
        {
213 4
            $contentItemBody = StakxResource::getResource('redirect.html.twig');
214 4
        }
215
        else
216
        {
217
            $contentItemBody = file_get_contents(self::$fileSys->absolutePath($redirectTemplate));
218
        }
219
220 4
        return self::createVirtual($frontMatter, $contentItemBody);
0 ignored issues
show
Security Bug introduced by
It seems like $contentItemBody defined by \allejo\stakx\System\Sta...e('redirect.html.twig') on line 213 can also be of type false; however, allejo\stakx\Document\PageView::createVirtual() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
221
    }
222
}
223