Completed
Pull Request — master (#41)
by Vladimir
02:46
created

PageView::getJailedChildren()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace allejo\stakx\Object;
4
5
use allejo\stakx\System\Filesystem;
6
use allejo\stakx\System\StakxResource;
7
use org\bovigo\vfs\vfsStream;
8
use org\bovigo\vfs\vfsStreamDirectory;
9
use Symfony\Component\Yaml\Yaml;
10
11
class PageView extends FrontMatterObject
12
{
13
    const TEMPLATE = "---\n%s\n---\n\n%s";
14
15
    const REPEATER_TYPE = 0;
16
    const DYNAMIC_TYPE  = 1;
17
    const STATIC_TYPE   = 2;
18
19
    /**
20
     * @var vfsStreamDirectory
21
     */
22
    private static $vfsRoot;
23
24
    /**
25
     * @var Filesystem
26
     */
27
    private static $fileSys;
28
29
    /**
30
     * @var string
31
     */
32
    protected $type;
33
34
    /**
35
     * @var PageView[]
36
     */
37
    private $children;
38
39
    /**
40
     * @var JailObject
41
     */
42
    private $jailInstance;
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 12
    public function __construct($filePath)
48
    {
49 12
        parent::__construct($filePath);
50
51 12
        $this->children = array();
52 12
        $this->type = PageView::STATIC_TYPE;
53 12
    }
54
55
    //
56
    // Twig Jail
57
    // =========
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 11
    public function createJail ()
63
    {
64 11
        if (is_null($this->jailInstance))
65
        {
66 11
            $this->jailInstance = (new JailObject($this, array_merge(self::$whiteListFunctions, array(
67
                'getUrl'
68 11
            )), array('getChildren' => 'getJailedChildren')));
69
        }
70
71 11
        return $this->jailInstance;
72
    }
73
74 3
    public function getJailedChildren ()
75
    {
76 3
        $children = $this->children;
77
78 3
        foreach ($children as &$child)
79
        {
80 3
            $child = $child->createJail();
81
        }
82
83 3
        return $children;
84
    }
85
86
    //
87
    // Getters
88
    // =======
89
90
    /**
91
     * Get child PageViews
92
     *
93
     * A child is defined as a static PageView whose URL has a parent. For example, a PageView with a URL of
94
     * `/gallery/france/` would have the PageView whose URL is `/gallery` as a parent.
95
     *
96
     * @return PageView[]
97
     */
98 3
    public function &getChildren ()
99
    {
100 3
        return $this->children;
101
    }
102
103
    /**
104
     * @return string Twig body
105
     */
106
    public function getContent ()
107
    {
108
        return $this->bodyContent;
109
    }
110
111
    /**
112
     * Returns the type of the PageView
113
     *
114
     * @return string
115
     */
116
    public function getType ()
117
    {
118
        return $this->type;
119
    }
120
121
    /**
122
     * A fallback for the site menus that use the `url` field.
123
     *
124
     * @deprecated 0.1.0
125
     * @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...
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
    public static function create ($filePath)
144
    {
145
        $instance = new self($filePath);
146
147
        if (isset($instance->getFrontMatter(false)['collection']))
148
        {
149
            return (new DynamicPageView($filePath));
150
        }
151
152
        $instance->getFrontMatter();
153
154
        if ($instance->hasExpandedFrontMatter())
155
        {
156
            return (new RepeaterPageView($filePath));
157
        }
158
159
        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
    public static function createVirtual ($frontMatter, $body)
175
    {
176
        if (is_null(self::$vfsRoot))
177
        {
178
            self::$vfsRoot = vfsStream::setup();
179
        }
180
181
        $redirectFile = vfsStream::newFile(sprintf('%s.html.twig', uniqid()));
182
        $redirectFile
183
            ->setContent(sprintf(self::TEMPLATE, Yaml::dump($frontMatter, 2), $body))
184
            ->at(self::$vfsRoot);
185
186
        return (new PageView($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
    public static function createRedirect ($redirectFrom, $redirectTo, $redirectTemplate = false)
199
    {
200
        if (is_null(self::$fileSys))
201
        {
202
            self::$fileSys = new Filesystem();
203
        }
204
205
        $frontMatter  = array(
206
            'permalink' => $redirectFrom,
207
            'redirect'  => $redirectTo,
208
            'menu' => false
209
        );
210
211
        if (!$redirectTemplate || !self::$fileSys->exists(self::$fileSys->absolutePath($redirectTemplate)))
212
        {
213
            $contentItemBody = StakxResource::getResource('redirect.html.twig');
214
        }
215
        else
216
        {
217
            $contentItemBody = file_get_contents(self::$fileSys->absolutePath($redirectTemplate));
218
        }
219
220
        return self::createVirtual($frontMatter, $contentItemBody);
221
    }
222
}
223