Completed
Pull Request — master (#69)
by Vladimir
05:36
created

BasePageView::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Document;
9
10
use allejo\stakx\Filesystem\File;
11
use allejo\stakx\Filesystem\FilesystemLoader as fs;
12
use org\bovigo\vfs\vfsStream;
13
use org\bovigo\vfs\vfsStreamWrapper;
14
use Symfony\Component\Yaml\Yaml;
15
16
abstract class BasePageView extends PermalinkFrontMatterDocument implements PermalinkDocument
17
{
18
    use TemplateEngineDependent;
19
20
    const REPEATER_TYPE = 'repeater';
21
    const DYNAMIC_TYPE = 'dynamic';
22
    const STATIC_TYPE = 'static';
23
24
    protected $type;
25
26
    /**
27
     * Returns the type of PageView.
28
     *
29
     * @return string
30
     */
31
    public function getType()
32
    {
33
        return $this->type;
34
    }
35
36
    ///
37
    // Static utilities
38
    ///
39
40
    /**
41
     * Create the appropriate object type when parsing a PageView.
42
     *
43
     * @return DynamicPageView|StaticPageView|RepeaterPageView
44
     */
45
    public static function create(File $filePath, array $complexVariables = [])
46
    {
47
        $instance = new StaticPageView($filePath);
48
49
        if (isset($instance->getRawFrontMatter()['collection']) ||
50
            isset($instance->getRawFrontMatter()['dataset'])
51
        ) {
52
            return new DynamicPageView($filePath);
53
        }
54
55
        $instance->evaluateFrontMatter([], $complexVariables);
56
57
        if ($instance->hasExpandedFrontMatter())
58
        {
59
            return new RepeaterPageView($filePath);
60
        }
61
62
        return $instance;
63
    }
64
65
    ///
66
    // Virtual PageViews
67
    ///
68
69
    /**
70
     * Create a virtual PageView.
71
     *
72
     * @param array  $frontMatter The Front Matter that this virtual PageView will have
73
     * @param string $body        The body of the virtual PageView
74
     *
75
     * @return StaticPageView
76
     */
77
    public static function createVirtual($frontMatter, $body)
78
    {
79
        if (vfsStreamWrapper::getRoot() == null)
80
        {
81
            vfsStream::setup();
82
        }
83
84
        $redirectFile = vfsStream::newFile(sprintf('redirect_%s.html.twig', uniqid()));
85
        $redirectFile
86
            ->setContent(sprintf(self::TEMPLATE, Yaml::dump($frontMatter, 2), $body))
87
            ->at(vfsStreamWrapper::getRoot())
88
        ;
89
90
        $file = new File($redirectFile->url());
91
92
        return new StaticPageView($file);
93
    }
94
95
    /**
96
     * Create a virtual PageView to create redirect files.
97
     *
98
     * @param string      $redirectFrom     The URL that will be redirecting to the target location
99
     * @param string      $redirectTo       The URL of the destination
100
     * @param string|bool $redirectTemplate The path to the template
101
     *
102
     * @return StaticPageView A virtual PageView with the redirection template
103
     */
104
    public static function createRedirect($redirectFrom, $redirectTo, $redirectTemplate = false)
105
    {
106
        $frontMatter = [
107
            'permalink' => $redirectFrom,
108
            'redirect' => $redirectTo,
109
            'menu' => false,
110
        ];
111
112
        $contentItemBody = fs::getInternalResource('redirect.html.twig');
0 ignored issues
show
Documentation introduced by
'redirect.html.twig' is of type string, but the function expects a object<string>.

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...
113
114
        try
115
        {
116
            if (!empty($redirectTemplate))
117
            {
118
                $redirectView = new File($redirectTemplate);
0 ignored issues
show
Bug introduced by
It seems like $redirectTemplate defined by parameter $redirectTemplate on line 104 can also be of type boolean; however, allejo\stakx\Filesystem\File::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
119
                $contentItemBody = $redirectView->getContents();
120
            }
121
        }
122
        catch (\Exception $e)
123
        {
124
            trigger_error($e->getMessage(), E_USER_WARNING);
125
        }
126
127
        return self::createVirtual($frontMatter, $contentItemBody);
128
    }
129
}
130