Completed
Push — master ( 4513c4...1bdcd4 )
by Vladimir
02:30
created

PageView::createVirtual()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.003

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 11
cp 0.9091
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 2
crap 2.003
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Document;
9
10
use allejo\stakx\Filesystem\File;
11
use allejo\stakx\FrontMatter\FrontMatterDocument;
12
use allejo\stakx\System\Filesystem;
13
use allejo\stakx\System\StakxResource;
14
use org\bovigo\vfs\vfsStream;
15
use org\bovigo\vfs\vfsStreamWrapper;
16
use Symfony\Component\Yaml\Yaml;
17
18
class PageView extends FrontMatterDocument
19
{
20
    const REPEATER_TYPE = 'repeater';
21
    const DYNAMIC_TYPE = 'dynamic';
22
    const STATIC_TYPE = 'static';
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
     * @var JailedDocument
41
     */
42
    private $jailInstance;
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 32
    public function __construct($filePath)
48
    {
49 32
        parent::__construct($filePath);
50
51 32
        $this->children = array();
52 32
        $this->type = self::STATIC_TYPE;
53 32
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 16
    public function getObjectName()
59
    {
60 16
        return $this->getRelativeFilePath();
61
    }
62
63
    //
64
    // Twig Jail
65
    // =========
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 24
    public function createJail()
71
    {
72 24
        if (is_null($this->jailInstance))
73
        {
74 24
            $this->jailInstance = (new JailedDocument($this, array_merge(self::$whiteListFunctions, array(
75 24
                'getUrl',
76 24
            )), array('getChildren' => 'getJailedChildren')));
77
        }
78
79 24
        return $this->jailInstance;
80
    }
81
82 3
    public function getJailedChildren()
83
    {
84 3
        $children = $this->children;
85
86 3
        foreach ($children as &$child)
87
        {
88 3
            $child = $child->createJail();
89
        }
90
91 3
        return $children;
92
    }
93
94
    //
95
    // Getters
96
    // =======
97
98
    /**
99
     * Get child PageViews.
100
     *
101
     * A child is defined as a static PageView whose URL has a parent. For example, a PageView with a URL of
102
     * `/gallery/france/` would have the PageView whose URL is `/gallery` as a parent.
103
     *
104
     * @return PageView[]
105
     */
106 3
    public function &getChildren()
107
    {
108 3
        return $this->children;
109
    }
110
111
    /**
112
     * @return string Twig body
113
     */
114 14
    public function getContent()
115
    {
116 14
        return $this->bodyContent;
117
    }
118
119
    /**
120
     * Returns the type of the PageView.
121
     *
122
     * @return string
123
     */
124 20
    public function getType()
125
    {
126 20
        return $this->type;
127
    }
128
129
    /**
130
     * A fallback for the site menus that use the `url` field.
131
     *
132
     * @deprecated 0.1.0
133
     *
134
     * @todo       Remove this in the next major release
135
     */
136
    public function getUrl()
137
    {
138
        return $this->getPermalink();
139
    }
140
141
    //
142
    // Factory
143
    // =======
144
145
    /**
146
     * Create the appropriate object type when parsing a PageView.
147
     *
148
     * @param string $filePath The path to the file that will be parsed into a PageView
149
     *
150
     * @return DynamicPageView|PageView|RepeaterPageView
151
     */
152 6
    public static function create($filePath)
153
    {
154 6
        $instance = new self($filePath);
155
156 6
        if (isset($instance->getFrontMatter(false)['collection']) ||
157 6
            isset($instance->getFrontMatter(false)['dataset'])
158
        ) {
159 4
            return new DynamicPageView($filePath);
160
        }
161
162 2
        $instance->getFrontMatter();
163
164 2
        if ($instance->hasExpandedFrontMatter())
165
        {
166
            return new RepeaterPageView($filePath);
167
        }
168
169 2
        return $instance;
170
    }
171
172
    //
173
    // Virtual PageViews
174
    // =================
175
176
    /**
177
     * Create a virtual PageView.
178
     *
179
     * @param array  $frontMatter The Front Matter that this virtual PageView will have
180
     * @param string $body        The body of the virtual PageView
181
     *
182
     * @return PageView
183
     */
184 4
    public static function createVirtual($frontMatter, $body)
185
    {
186 4
        if (vfsStreamWrapper::getRoot() == null)
187
        {
188
            vfsStream::setup();
189
        }
190
191 4
        $redirectFile = vfsStream::newFile(sprintf('redirect_%s.html.twig', uniqid()));
192
        $redirectFile
193 4
            ->setContent(sprintf(self::TEMPLATE, Yaml::dump($frontMatter, 2), $body))
194 4
            ->at(vfsStreamWrapper::getRoot());
0 ignored issues
show
Documentation introduced by
\org\bovigo\vfs\vfsStreamWrapper::getRoot() is of type object<org\bovigo\vfs\vfsStreamContent>, but the function expects a object<org\bovigo\vfs\vfsStreamContainer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
195
196 4
        $file = new File(
197 4
            $redirectFile->url(),
198 4
            self::$fileSys->getFolderPath($redirectFile->url()),
199 4
            $redirectFile->url()
200
        );
201
202 4
        return new self($file);
203
    }
204
205
    /**
206
     * Create a virtual PageView to create redirect files.
207
     *
208
     * @param string      $redirectFrom     The URL that will be redirecting to the target location
209
     * @param string      $redirectTo       The URL of the destination
210
     * @param string|bool $redirectTemplate The path to the template
211
     *
212
     * @return PageView A virtual PageView with the redirection template
213
     */
214 4
    public static function createRedirect($redirectFrom, $redirectTo, $redirectTemplate = false)
215
    {
216 4
        if (is_null(self::$fileSys))
217
        {
218 1
            self::$fileSys = new Filesystem();
219
        }
220
221
        $frontMatter = array(
222 4
            'permalink' => $redirectFrom,
223 4
            'redirect'  => $redirectTo,
224
            'menu'      => false,
225
        );
226
227 4
        if (!$redirectTemplate || !self::$fileSys->exists(self::$fileSys->absolutePath($redirectTemplate)))
228
        {
229 4
            $contentItemBody = StakxResource::getResource('redirect.html.twig');
230
        }
231
        else
232
        {
233
            $contentItemBody = file_get_contents(self::$fileSys->absolutePath($redirectTemplate));
234
        }
235
236 4
        return self::createVirtual($frontMatter, $contentItemBody);
0 ignored issues
show
Bug introduced by
It seems like $contentItemBody defined by \allejo\stakx\System\Sta...e('redirect.html.twig') on line 229 can also be of type boolean; however, allejo\stakx\Document\PageView::createVirtual() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
237
    }
238
}
239