Completed
Push — master ( a6c8f9...b97be3 )
by Vladimir
03:52 queued 19s
created

BasePageView::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 allejo\stakx\System\StakxResource;
13
use org\bovigo\vfs\vfsStream;
14
use org\bovigo\vfs\vfsStreamWrapper;
15
use Symfony\Component\Yaml\Yaml;
16
17
abstract class BasePageView extends PermalinkFrontMatterDocument implements PermalinkDocument
18
{
19
    use TemplateEngineDependent;
20
21
    const REPEATER_TYPE = 'repeater';
22
    const DYNAMIC_TYPE = 'dynamic';
23
    const STATIC_TYPE = 'static';
24
25
    protected $type;
26
27
    /**
28
     * Returns the type of PageView.
29
     *
30
     * @return string
31
     */
32 20
    public function getType()
33
    {
34 20
        return $this->type;
35
    }
36
37
    ///
38
    // Static utilities
39
    ///
40
41
    /**
42
     * Create the appropriate object type when parsing a PageView.
43
     *
44
     * @return DynamicPageView|StaticPageView|RepeaterPageView
45
     */
46 6
    public static function create(File $filePath)
47
    {
48 6
        $instance = new StaticPageView($filePath);
49
50 6
        if (isset($instance->getFrontMatter(false)['collection']) ||
51 2
            isset($instance->getFrontMatter(false)['dataset'])
52 6
        ) {
53 4
            return new DynamicPageView($filePath);
54
        }
55
56 2
        $instance->getFrontMatter();
57
58 2
        if ($instance->hasExpandedFrontMatter())
59 2
        {
60
            return new RepeaterPageView($filePath);
61
        }
62
63 2
        return $instance;
64
    }
65
66
    ///
67
    // Virtual PageViews
68
    ///
69
70
    /**
71
     * Create a virtual PageView.
72
     *
73
     * @param array  $frontMatter The Front Matter that this virtual PageView will have
74
     * @param string $body        The body of the virtual PageView
75
     *
76
     * @return StaticPageView
77
     */
78 4
    public static function createVirtual($frontMatter, $body)
79
    {
80 4
        if (vfsStreamWrapper::getRoot() == null)
81 4
        {
82
            vfsStream::setup();
83
        }
84
85 4
        $redirectFile = vfsStream::newFile(sprintf('redirect_%s.html.twig', uniqid()));
86
        $redirectFile
87 4
            ->setContent(sprintf(self::TEMPLATE, Yaml::dump($frontMatter, 2), $body))
88 4
            ->at(vfsStreamWrapper::getRoot());
89
90 4
        $file = new File($redirectFile->url());
91
92 4
        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 4
    public static function createRedirect($redirectFrom, $redirectTo, $redirectTemplate = false)
105
    {
106
        $frontMatter = array(
107 4
            'permalink' => $redirectFrom,
108 4
            'redirect'  => $redirectTo,
109 4
            'menu'      => false,
110 4
        );
111
112 4
        if (!$redirectTemplate || !fs::exists(fs::absolutePath($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\...mLoader::absolutePath() 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...
113 4
        {
114 4
            $contentItemBody = StakxResource::getResource('redirect.html.twig');
115 4
        }
116
        else
117
        {
118
            $contentItemBody = file_get_contents(fs::absolutePath($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\...mLoader::absolutePath() 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
        }
120
121 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 114 can also be of type boolean; however, allejo\stakx\Document\Ba...geView::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...
122
    }
123
}
124