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

PageView::createRedirect()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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