Completed
Pull Request — master (#20)
by Vladimir
02:43
created

PageView::isDynamicPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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