Passed
Push — master ( 7b7005...55ec05 )
by Brent
02:29
created

PageRenderProcess::execute()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 10
nop 0
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Pageon\Pcntl;
4
5
use Brendt\Stitcher\App;
6
use Brendt\Stitcher\Application\DevController;
7
use Brendt\Stitcher\Parser\Site\PageParser;
8
use Brendt\Stitcher\Site\Page;
9
use Brendt\Stitcher\Site\Seo\SiteMap;
10
use Symfony\Component\Filesystem\Filesystem;
11
12
class PageRenderProcess extends Process
13
{
14
    /**
15
     * @var PageParser
16
     */
17
    private $pageParser;
18
19
    /**
20
     * @var Page
21
     */
22
    private $page;
23
24
    /**
25
     * @var null
26
     */
27
    private $filterValue;
28
29
    /**
30
     * @var string
31
     */
32
    private $publicDir;
33
34
    /**
35
     * @var bool
36
     */
37
    private $async = false;
38
39
    /**
40
     * @var string
41
     */
42
    private $environment = null;
43
44
    public function __construct(PageParser $pageParser, Page $page, string $publicDir, string $filterValue = null) {
45
        $this->name = $page->getId();
46
        $this->pageParser = $pageParser;
47
        $this->page = $page;
48
        $this->filterValue = $filterValue;
0 ignored issues
show
Documentation Bug introduced by
It seems like $filterValue can also be of type string. However, the property $filterValue is declared as type null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
49
        $this->publicDir = $publicDir;
50
    }
51
52
    public function setAsync(bool $async = true) {
53
        $this->async = $async;
54
    }
55
56
    public function setEnvironment($environment) {
57
        $this->environment = $environment;
58
    }
59
60
    public function execute() {
61
        $this->pageParser->validate($this->page);
62
        $pages = $this->pageParser->parseAdapters($this->page, $this->filterValue);
63
        $blanket = [];
64
        $fs = new Filesystem();
65
66
        /** @var Page $page */
67
        foreach ($pages as $page) {
68
            $path = $page->getId();
69
70
            if ($path === '/') {
71
                $path = 'index';
72
            }
73
74
            if ($this->environment !== DevController::ENVIRONMENT) {
75
                $fs->dumpFile($this->publicDir . "/{$path}.html", $this->pageParser->parsePage($page));
76
            }
77
78
            $blanket[$page->getId()] = $this->pageParser->parsePage($page);
79
        }
80
81
        if ($this->async) {
82
            return array_keys($blanket);
83
        } else {
84
            return $blanket;
85
        }
86
    }
87
}
88