Completed
Pull Request — master (#41)
by Vladimir
04:25
created

PageView::getUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 1
b 0
f 0
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\Object;
9
10
use allejo\stakx\System\Filesystem;
11
use allejo\stakx\System\StakxResource;
12
use org\bovigo\vfs\vfsStream;
13
use org\bovigo\vfs\vfsStreamDirectory;
14
use Symfony\Component\Yaml\Yaml;
15
16
class PageView extends FrontMatterObject
17
{
18
    const TEMPLATE = "---\n%s\n---\n\n%s";
19
20
    const REPEATER_TYPE = 0;
21
    const DYNAMIC_TYPE = 1;
22
    const STATIC_TYPE = 2;
23
24
    /**
25
     * @var vfsStreamDirectory
26
     */
27
    private static $vfsRoot;
28
29
    /**
30
     * @var Filesystem
31
     */
32
    private static $fileSys;
33
34
    /**
35
     * @var string
36
     */
37
    protected $type;
38
39
    /**
40
     * @var PageView[]
41
     */
42
    private $children;
43
44
    /**
45
     * @var JailObject
46
     */
47
    private $jailInstance;
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 23
    public function __construct($filePath)
53
    {
54 23
        parent::__construct($filePath);
55
56 23
        $this->children = array();
57 23
        $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...
58 23
    }
59
60
    //
61
    // Twig Jail
62
    // =========
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 17
    public function createJail()
68
    {
69 17
        if (is_null($this->jailInstance))
70 17
        {
71 17
            $this->jailInstance = (new JailObject($this, array_merge(self::$whiteListFunctions, array(
72 17
                'getUrl',
73 17
            )), array('getChildren' => 'getJailedChildren')));
74 17
        }
75
76 17
        return $this->jailInstance;
77
    }
78
79 3
    public function getJailedChildren()
80
    {
81 3
        $children = $this->children;
82
83 3
        foreach ($children as &$child)
84
        {
85 3
            $child = $child->createJail();
86 3
        }
87
88 3
        return $children;
89
    }
90
91
    //
92
    // Getters
93
    // =======
94
95
    /**
96
     * Get child PageViews.
97
     *
98
     * A child is defined as a static PageView whose URL has a parent. For example, a PageView with a URL of
99
     * `/gallery/france/` would have the PageView whose URL is `/gallery` as a parent.
100
     *
101
     * @return PageView[]
102
     */
103 3
    public function &getChildren()
104
    {
105 3
        return $this->children;
106
    }
107
108
    /**
109
     * @return string Twig body
110
     */
111 5
    public function getContent()
112
    {
113 5
        return $this->bodyContent;
114
    }
115
116
    /**
117
     * Returns the type of the PageView.
118
     *
119
     * @return string
120
     */
121 11
    public function getType()
122
    {
123 11
        return $this->type;
124
    }
125
126
    /**
127
     * A fallback for the site menus that use the `url` field.
128
     *
129
     * @deprecated 0.1.0
130
     *
131
     * @todo       Remove this in the next major release
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
132
     */
133
    public function getUrl()
134
    {
135
        return $this->getPermalink();
136
    }
137
138
    //
139
    // Factory
140
    // =======
141
142
    /**
143
     * Create the appropriate object type when parsing a PageView.
144
     *
145
     * @param string $filePath The path to the file that will be parsed into a PageView
146
     *
147
     * @return DynamicPageView|PageView|RepeaterPageView
148
     */
149 6
    public static function create($filePath)
150
    {
151 6
        $instance = new self($filePath);
152
153 6
        if (isset($instance->getFrontMatter(false)['collection']))
154 6
        {
155 4
            return new DynamicPageView($filePath);
156
        }
157
158 2
        $instance->getFrontMatter();
159
160 2
        if ($instance->hasExpandedFrontMatter())
161 2
        {
162
            return new RepeaterPageView($filePath);
163
        }
164
165 2
        return $instance;
166
    }
167
168
    //
169
    // Virtual PageViews
170
    // =================
171
172
    /**
173
     * Create a virtual PageView.
174
     *
175
     * @param array  $frontMatter The Front Matter that this virtual PageView will have
176
     * @param string $body        The body of the virtual PageView
177
     *
178
     * @return PageView
179
     */
180
    public static function createVirtual($frontMatter, $body)
181
    {
182
        if (is_null(self::$vfsRoot))
183
        {
184
            self::$vfsRoot = vfsStream::setup();
185
        }
186
187
        $redirectFile = vfsStream::newFile(sprintf('%s.html.twig', uniqid()));
188
        $redirectFile
189
            ->setContent(sprintf(self::TEMPLATE, Yaml::dump($frontMatter, 2), $body))
190
            ->at(self::$vfsRoot);
191
192
        return new self($redirectFile->url());
193
    }
194
195
    /**
196
     * Create a virtual PageView to create redirect files.
197
     *
198
     * @param string      $redirectFrom     The URL that will be redirecting to the target location
199
     * @param string      $redirectTo       The URL of the destination
200
     * @param string|bool $redirectTemplate The path to the template
201
     *
202
     * @return PageView A virtual PageView with the redirection template
203
     */
204
    public static function createRedirect($redirectFrom, $redirectTo, $redirectTemplate = false)
205
    {
206
        if (is_null(self::$fileSys))
207
        {
208
            self::$fileSys = new Filesystem();
209
        }
210
211
        $frontMatter = array(
212
            'permalink' => $redirectFrom,
213
            'redirect'  => $redirectTo,
214
            'menu'      => false,
215
        );
216
217
        if (!$redirectTemplate || !self::$fileSys->exists(self::$fileSys->absolutePath($redirectTemplate)))
218
        {
219
            $contentItemBody = StakxResource::getResource('redirect.html.twig');
220
        }
221
        else
222
        {
223
            $contentItemBody = file_get_contents(self::$fileSys->absolutePath($redirectTemplate));
224
        }
225
226
        return self::createVirtual($frontMatter, $contentItemBody);
227
    }
228
}
229