Completed
Push — master ( 0c3733...9b1319 )
by Vladimir
02:41
created

PageView::getJailedChildren()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 6
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
     * {@inheritdoc}
41
     */
42 1
    public function __construct($filePath)
43
    {
44 1
        parent::__construct($filePath);
45
46 1
        $this->children = array();
47 1
        $this->type = PageView::STATIC_TYPE;
48 1
    }
49
50
    //
51
    // Twig Jail
52
    // =========
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 1
    public function createJail ()
58
    {
59 1
        return (new JailObject($this, array_merge(self::$whiteListFunctions, array(
60
            'getUrl'
61 1
        )), array('getChildren' => 'getJailedChildren')));
62
    }
63
64
    public function getJailedChildren ()
65
    {
66
        $children = $this->children;
67
68
        foreach ($children as &$child)
69
        {
70
            $child = $child->createJail();
71
        }
72
73
        return $children;
74
    }
75
76
    //
77
    // Getters
78
    // =======
79
80
    /**
81
     * Get child PageViews
82
     *
83
     * A child is defined as a static PageView whose URL has a parent. For example, a PageView with a URL of
84
     * `/gallery/france/` would have the PageView whose URL is `/gallery` as a parent.
85
     *
86
     * @return PageView[]
87
     */
88
    public function &getChildren ()
89
    {
90
        return $this->children;
91
    }
92
93
    /**
94
     * @return string Twig body
95
     */
96 1
    public function getContent ()
97
    {
98 1
        return $this->bodyContent;
99
    }
100
101
    /**
102
     * Returns the type of the PageView
103
     *
104
     * @return string
105
     */
106 1
    public function getType ()
107
    {
108 1
        return $this->type;
109
    }
110
111
    /**
112
     * A fallback for the site menus that use the `url` field.
113
     *
114
     * @deprecated 0.1.0
115
     * @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...
116
     */
117
    public function getUrl ()
118
    {
119
        return $this->getPermalink();
120
    }
121
122
    //
123
    // Factory
124
    // =======
125
126
    /**
127
     * Create the appropriate object type when parsing a PageView
128
     *
129
     * @param  string $filePath The path to the file that will be parsed into a PageView
130
     *
131
     * @return DynamicPageView|PageView|RepeaterPageView
132
     */
133 1
    public static function create ($filePath)
134
    {
135 1
        $instance = new self($filePath);
136
137 1
        if (isset($instance->getFrontMatter(false)['collection']))
138 1
        {
139
            return (new DynamicPageView($filePath));
140
        }
141
142 1
        $instance->getFrontMatter();
143
144 1
        if ($instance->hasExpandedFrontMatter())
145 1
        {
146 1
            return (new RepeaterPageView($filePath));
147
        }
148
149
        return $instance;
150
    }
151
152
    //
153
    // Virtual PageViews
154
    // =================
155
156
    /**
157
     * Create a virtual PageView
158
     *
159
     * @param  array  $frontMatter The Front Matter that this virtual PageView will have
160
     * @param  string $body        The body of the virtual PageView
161
     *
162
     * @return PageView
163
     */
164
    public static function createVirtual ($frontMatter, $body)
165
    {
166
        if (is_null(self::$vfsRoot))
167
        {
168
            self::$vfsRoot = vfsStream::setup();
169
        }
170
171
        $redirectFile = vfsStream::newFile(sprintf('%s.html.twig', uniqid()));
172
        $redirectFile
173
            ->setContent(sprintf(self::TEMPLATE, Yaml::dump($frontMatter, 2), $body))
174
            ->at(self::$vfsRoot);
175
176
        return (new PageView($redirectFile->url()));
177
    }
178
179
    /**
180
     * Create a virtual PageView to create redirect files
181
     *
182
     * @param  string      $redirectFrom     The URL that will be redirecting to the target location
183
     * @param  string      $redirectTo       The URL of the destination
184
     * @param  string|bool $redirectTemplate The path to the template
185
     *
186
     * @return PageView A virtual PageView with the redirection template
187
     */
188
    public static function createRedirect ($redirectFrom, $redirectTo, $redirectTemplate = false)
189
    {
190
        if (is_null(self::$fileSys))
191
        {
192
            self::$fileSys = new Filesystem();
193
        }
194
195
        $frontMatter  = array(
196
            'permalink' => $redirectFrom,
197
            'redirect'  => $redirectTo,
198
            'menu' => false
199
        );
200
201
        if (!$redirectTemplate || !self::$fileSys->exists(self::$fileSys->absolutePath($redirectTemplate)))
202
        {
203
            $contentItemBody = StakxResource::getResource('redirect.html.twig');
204
        }
205
        else
206
        {
207
            $contentItemBody = file_get_contents(self::$fileSys->absolutePath($redirectTemplate));
208
        }
209
210
        return self::createVirtual($frontMatter, $contentItemBody);
211
    }
212
}
213