Completed
Push — master ( ec3f5a...4ff794 )
by Tom
03:20
created

Page   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 12
eloc 29
c 2
b 1
f 1
dl 0
loc 73
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 7 1
A parse() 0 13 5
A page() 0 13 3
A data() 0 7 1
A toHtml() 0 3 1
A __construct() 0 5 1
1
<?php
2
3
namespace Astroromic\Stancy\Models;
4
5
use Exception;
6
use Illuminate\Contracts\Support\Htmlable;
7
use Illuminate\Contracts\View\Factory as ViewFactory;
8
use Illuminate\Contracts\View\View;
9
use Spatie\Sheets\Facades\Sheets;
10
11
class Page implements Htmlable
12
{
13
    /** @var string */
14
    protected $view;
15
16
    /** @var string|null */
17
    protected $page;
18
19
    /** @var PageData|array */
0 ignored issues
show
introduced by
@var annotation of property \Astroromic\Stancy\Models\Page::$data does not specify type hint for its items.
Loading history...
20
    protected $data;
21
22
    /** @var ViewFactory */
23
    protected $viewFactory;
24
25
    public function __construct(ViewFactory $viewFactory, array $data, ?string $page = null)
0 ignored issues
show
introduced by
Method \Astroromic\Stancy\Models\Page::__construct() does not have @param annotation for its traversable parameter $data.
Loading history...
26
    {
27
        $this->viewFactory = $viewFactory;
28
        $this->page($page);
29
        $this->data($data);
30
    }
31
32
    public static function make(string $collection, string $name, ?string $page = null): self
33
    {
34
        $sheet = Sheets::collection($collection)->get($name);
35
36
        return app(static::class, [
0 ignored issues
show
Bug Best Practice introduced by
The expression return app(static::class...ay(), 'page' => $page)) could return the type Illuminate\Contracts\Foundation\Application which is incompatible with the type-hinted return Astroromic\Stancy\Models\Page. Consider adding an additional type-check to rule them out.
Loading history...
37
            'data' => $sheet->toArray(),
38
            'page' => $page,
39
        ]);
40
    }
41
42
    public function page(?string $page): self
43
    {
44
        if (is_string($page)) {
45
            if (! ($page instanceof PageData)) {
0 ignored issues
show
introduced by
$page is never a sub-type of Astroromic\Stancy\Models\PageData.
Loading history...
46
                throw new Exception(sprintf('The page data class [%s] has to extend %s.', $page, PageData::class));
47
            }
48
        }
49
50
        $this->page = $page;
51
52
        $this->parse();
53
54
        return $this;
55
    }
56
57
    public function data(array $data): self
0 ignored issues
show
introduced by
Method \Astroromic\Stancy\Models\Page::data() does not have @param annotation for its traversable parameter $data.
Loading history...
58
    {
59
        $this->data = $data;
60
61
        $this->parse();
62
63
        return $this;
64
    }
65
66
    public function toHtml(): View
67
    {
68
        return $this->viewFactory->make($this->view, $this->data);
69
    }
70
71
    protected function parse(): void
72
    {
73
        if ($this->page === null) {
74
            return;
75
        }
76
77
        if (is_array($this->data) && empty($this->data)) {
78
            return;
79
        }
80
81
        $this->data = forward_static_call(
82
            [$this->page, 'make'],
83
            is_array($this->data) ? $this->data : $this->data->toArray()
84
        );
85
    }
86
}
87