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

PageView::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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