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

PageView::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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