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

PageView::getType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 10
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 DYNAMIC_TYPE  = 'dynamic';
16
    const STATIC_TYPE   = 'static';
17
18
    /**
19
     * @var vfsStreamDirectory
20
     */
21
    private static $vfsRoot;
22
23
    /**
24
     * @var Filesystem
25
     */
26
    private static $fileSys;
27
28
    /**
29
     * The Content Items that belong to this Page View. This array will only have elements if it is a dynamic Page View.
30
     *
31
     * @var ContentItem[]
32
     */
33
    private $contentItems;
34
35
    /**
36
     * @var PageView[]
37
     */
38
    private $children;
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function __construct($filePath)
44
    {
45
        parent::__construct($filePath);
46
47
        $this->children = array();
48
    }
49
50
    //
51
    // Dynamic PageView functionality
52
    // ==============================
53
54
    /**
55
     * Add a ContentItem to this Dynamic PageView
56
     *
57
     * @param ContentItem $contentItem
58
     */
59
    public function addContentItem (&$contentItem)
60
    {
61
        $filePath = $this->fs->getRelativePath($contentItem->getFilePath());
62
63
        $this->contentItems[$filePath] = &$contentItem;
64
        $contentItem->setPageView($this);
65
    }
66
67
    /**
68
     * Get all of the ContentItems that belong to this Dynamic PageView
69
     *
70
     * @return ContentItem[]
71
     */
72
    public function getContentItems ()
73
    {
74
        return $this->contentItems;
75
    }
76
77
    //
78
    // Getters
79
    // =======
80
81
    /**
82
     * Get child PageViews
83
     *
84
     * A child is defined as a static PageView whose URL has a parent. For example, a PageView with a URL of
85
     * `/gallery/france/` would have the PageView whose URL is `/gallery` as a parent.
86
     *
87
     * @return PageView[]
88
     */
89
    public function &getChildren ()
90
    {
91
        return $this->children;
92
    }
93
94
    /**
95
     * @return string Twig body
96
     */
97
    public function getContent ()
98
    {
99
        return $this->bodyContent;
100
    }
101
102
    /**
103
     * Returns the type of the PageView
104
     *
105
     * @return string
106
     */
107
    public function getType ()
108
    {
109
110
        if (isset($this->frontMatter['collection']))
111
        {
112
            return self::DYNAMIC_TYPE;
113
        }
114
115
        return self::STATIC_TYPE;
116
    }
117
118
    /**
119
     * A fallback for the site menus that use the `url` field.
120
     *
121
     * @deprecated 0.1.0
122
     * @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...
123
     */
124
    public function getUrl ()
125
    {
126
        return $this->getPermalink();
127
    }
128
129
    //
130
    // Virtual PageViews
131
    // =================
132
133
    /**
134
     * Create a virtual PageView
135
     *
136
     * @param  array  $frontMatter The Front Matter that this virtual PageView will have
137
     * @param  string $body        The body of the virtual PageView
138
     *
139
     * @return PageView
140
     */
141
    public static function createVirtual ($frontMatter, $body)
142
    {
143
        if (is_null(self::$vfsRoot))
144
        {
145
            self::$vfsRoot = vfsStream::setup();
146
        }
147
148
        $redirectFile = vfsStream::newFile(sprintf('%s.html.twig', uniqid()));
149
        $redirectFile
150
            ->setContent(sprintf(self::TEMPLATE, Yaml::dump($frontMatter, 2), $body))
151
            ->at(self::$vfsRoot);
152
153
        return (new PageView($redirectFile->url()));
154
    }
155
156
    /**
157
     * Create a virtual PageView to create redirect files
158
     *
159
     * @param  string      $redirectFrom     The URL that will be redirecting to the target location
160
     * @param  string      $redirectTo       The URL of the destination
161
     * @param  string|bool $redirectTemplate The path to the template
162
     *
163
     * @return PageView A virtual PageView with the redirection template
164
     */
165
    public static function createRedirect ($redirectFrom, $redirectTo, $redirectTemplate = false)
166
    {
167
        if (is_null(self::$fileSys))
168
        {
169
            self::$fileSys = new Filesystem();
170
        }
171
172
        $frontMatter  = array(
173
            'permalink' => $redirectFrom,
174
            'redirect'  => $redirectTo,
175
            'menu' => false
176
        );
177
178
        if (!$redirectTemplate || !self::$fileSys->exists(self::$fileSys->absolutePath($redirectTemplate)))
179
        {
180
            $contentItemBody = StakxResource::getResource('redirect.html.twig');
181
        }
182
        else
183
        {
184
            $contentItemBody = file_get_contents(self::$fileSys->absolutePath($redirectTemplate));
185
        }
186
187
        return self::createVirtual($frontMatter, $contentItemBody);
188
    }
189
}
190