Completed
Push — master ( 0b105d...2d000c )
by Tom
03:34
created

Page::parse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 2
nop 0
crap 3
1
<?php
2
3
namespace Astrotomic\Stancy\Models;
4
5
use Exception;
6
use Illuminate\Contracts\Support\Htmlable;
7
use Illuminate\Contracts\Support\Renderable;
8
use Illuminate\Contracts\View\Factory as ViewFactory;
9
use Illuminate\Contracts\View\View;
10
use Spatie\Sheets\Facades\Sheets;
11
12
class Page implements Htmlable, Renderable
13
{
14
    /** @var string|null */
15
    protected $view;
16
17
    /** @var string|null */
18
    protected $page;
19
20
    /** @var PageData|array */
0 ignored issues
show
introduced by
@var annotation of property \Astrotomic\Stancy\Models\Page::$data does not specify type hint for its items.
Loading history...
21
    protected $data = [];
22
23
    /** @var ViewFactory */
24
    protected $viewFactory;
25
26 9
    public function __construct(ViewFactory $viewFactory, array $data = [], ?string $page = null)
0 ignored issues
show
introduced by
Method \Astrotomic\Stancy\Models\Page::__construct() does not have @param annotation for its traversable parameter $data.
Loading history...
27
    {
28 9
        $this->viewFactory = $viewFactory;
29 9
        $this->data($data);
30 9
        $this->page($page);
31 7
    }
32
33 8
    public static function make(array $data = [], ?string $page = null): self
0 ignored issues
show
introduced by
Method \Astrotomic\Stancy\Models\Page::make() does not have @param annotation for its traversable parameter $data.
Loading history...
34
    {
35 8
        return app(static::class, [
0 ignored issues
show
Bug Best Practice introduced by
The expression return app(static::class...data, 'page' => $page)) could return the type Illuminate\Contracts\Foundation\Application which is incompatible with the type-hinted return Astrotomic\Stancy\Models\Page. Consider adding an additional type-check to rule them out.
Loading history...
36 8
            'data' => $data,
37 8
            'page' => $page,
38
        ]);
39
    }
40
41 4
    public static function makeFromSheet(string $collection, string $name, ?string $page = null): self
42
    {
43 4
        $sheet = Sheets::collection($collection)->get($name);
44
45 4
        if($sheet === null) {
0 ignored issues
show
introduced by
You should change the following
<fg=red>- if($sheet === null) {
</><fg=green>+ if ($sheet === null) {
</>
Loading history...
46 1
            throw new Exception(sprintf('No sheet found in collection [%s] with name [%s].', $collection, $name));
47
        }
48
49 3
        return static::make($sheet->toArray(), $page);
50
    }
51
52 9
    public function page(?string $page): self
53
    {
54 9
        if (is_string($page)) {
55 4
            if (! is_subclass_of($page, PageData::class, true)) {
56 2
                throw new Exception(sprintf('The page data class [%s] has to extend %s.', $page, PageData::class));
57
            }
58
        }
59
60 7
        $this->page = $page;
61
62 7
        $this->parse();
63
64 7
        return $this;
65
    }
66
67 9
    public function data(array $data): self
0 ignored issues
show
introduced by
Method \Astrotomic\Stancy\Models\Page::data() does not have @param annotation for its traversable parameter $data.
Loading history...
68
    {
69 9
        $this->data = $data;
70
71 9
        $this->parse();
72
73 9
        return $this;
74
    }
75
76 2
    public function view(string $view): self
77
    {
78 2
        $this->view = $view;
79
80 2
        return $this;
81
    }
82
83 3
    public function render(): View
84
    {
85 3
        if (empty($this->view)) {
86 1
            throw new Exception('You have to define a view before the page can render.');
87
        }
88
89 2
        return $this->viewFactory->make($this->view, $this->data);
90
    }
91
92 2
    public function toHtml(): string
93
    {
94 2
        return $this->render()->render();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->render()->render() could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
95
    }
96
97 9
    protected function parse(): void
98
    {
99 9
        if ($this->page === null) {
100 9
            return;
101
        }
102
103 2
        $this->data = forward_static_call(
104 2
            [$this->page, 'make'],
105 2
            is_array($this->data) ? $this->data : $this->data->toArray()
106
        );
107 1
    }
108
}
109