Completed
Pull Request — master (#62)
by Vladimir
02:32
created

BasePageView::createRedirect()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.243

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 7
cts 10
cp 0.7
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 3
crap 3.243
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\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 18
    public function getType()
32
    {
33 18
        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 18
    public static function create(File $filePath, array $complexVariables = [])
46
    {
47 18
        $instance = new StaticPageView($filePath);
48
49 18
        if (isset($instance->getRawFrontMatter()['collection']) ||
50 18
            isset($instance->getRawFrontMatter()['dataset'])
51
        ) {
52 4
            return new DynamicPageView($filePath);
53
        }
54
55 14
        $instance->evaluateFrontMatter([], $complexVariables);
56
57 14
        if ($instance->hasExpandedFrontMatter())
58
        {
59 3
            return new RepeaterPageView($filePath);
60
        }
61
62 11
        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 2
    public static function createVirtual($frontMatter, $body)
78
    {
79 2
        if (vfsStreamWrapper::getRoot() == null)
80
        {
81
            vfsStream::setup();
82
        }
83
84 2
        $redirectFile = vfsStream::newFile(sprintf('redirect_%s.html.twig', uniqid()));
85
        $redirectFile
86 2
            ->setContent(sprintf(self::TEMPLATE, Yaml::dump($frontMatter, 2), $body))
87 2
            ->at(vfsStreamWrapper::getRoot());
88
89 2
        $file = new File($redirectFile->url());
90
91 2
        return new StaticPageView($file);
92
    }
93
94
    /**
95
     * Create a virtual PageView to create redirect files.
96
     *
97
     * @param string      $redirectFrom     The URL that will be redirecting to the target location
98
     * @param string      $redirectTo       The URL of the destination
99
     * @param string|bool $redirectTemplate The path to the template
100
     *
101
     * @return StaticPageView A virtual PageView with the redirection template
102
     */
103 2
    public static function createRedirect($redirectFrom, $redirectTo, $redirectTemplate = false)
104
    {
105
        $frontMatter = array(
106 2
            'permalink' => $redirectFrom,
107 2
            'redirect'  => $redirectTo,
108
            'menu'      => false,
109
        );
110
111 2
        $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...
112
113
        try
114
        {
115 2
            if (!empty($redirectTemplate))
116
            {
117
                $redirectView = new File($redirectTemplate);
0 ignored issues
show
Bug introduced by
It seems like $redirectTemplate defined by parameter $redirectTemplate on line 103 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...
118 2
                $contentItemBody = $redirectView->getContents();
119
            }
120
        }
121
        catch (\Exception $e)
122
        {
123
            trigger_error($e->getMessage(), E_USER_WARNING);
124
        }
125
126 2
        return self::createVirtual($frontMatter, $contentItemBody);
127
    }
128
}
129