Passed
Push — master ( da45e8...9bb787 )
by Tom
03:18
created

Page   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 22
eloc 44
c 2
b 0
f 2
dl 0
loc 129
ccs 54
cts 54
cp 1
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 3 2
A render() 0 7 2
A view() 0 5 1
A toHtml() 0 3 1
A toResponse() 0 7 2
A page() 0 13 3
A make() 0 5 1
A data() 0 7 1
A makeFromSheet() 0 9 2
A parse() 0 9 2
A toJson() 0 3 1
A __construct() 0 5 1
A jsonSerialize() 0 3 1
A toFeedItem() 0 7 2
1
<?php
2
3
namespace Astrotomic\Stancy\Models;
4
5
use Exception;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Contracts\Support\Htmlable;
8
use Illuminate\Contracts\Support\Jsonable;
9
use Illuminate\Contracts\Support\Renderable;
10
use Illuminate\Contracts\Support\Responsable;
11
use Illuminate\Contracts\View\Factory as ViewFactory;
12
use Illuminate\Contracts\View\View;
13
use JsonSerializable;
14
use Spatie\Feed\Feedable;
15
use Spatie\Feed\FeedItem;
16
use Spatie\Sheets\Facades\Sheets;
17
use Symfony\Component\HttpFoundation\Response;
18
19
class Page implements Htmlable, Renderable, Responsable, Arrayable, Jsonable, JsonSerializable, Feedable
20
{
21
    /** @var string|null */
22
    protected $view;
23
24
    /** @var string|null */
25
    protected $page;
26
27
    /** @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...
28
    protected $data = [];
29
30
    /** @var ViewFactory */
31
    protected $viewFactory;
32
33 14
    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...
34
    {
35 14
        $this->viewFactory = $viewFactory;
36 14
        $this->data($data);
37 14
        $this->page($page);
38 12
    }
39
40 13
    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...
41
    {
42 13
        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...
43 13
            'data' => $data,
44 13
            'page' => $page,
45
        ]);
46
    }
47
48 7
    public static function makeFromSheet(string $collection, string $name, ?string $page = null): self
49
    {
50 7
        $sheet = Sheets::collection($collection)->get($name);
51
52 7
        if ($sheet === null) {
53 1
            throw new Exception(sprintf('No sheet found in collection [%s] with name [%s].', $collection, $name));
54
        }
55
56 6
        return static::make($sheet->toArray(), $page);
57
    }
58
59 14
    public function page(?string $page): self
60
    {
61 14
        if (is_string($page)) {
62 8
            if (! is_subclass_of($page, PageData::class, true)) {
63 2
                throw new Exception(sprintf('The page data class [%s] has to extend %s.', $page, PageData::class));
64
            }
65
        }
66
67 12
        $this->page = $page;
68
69 12
        $this->parse();
70
71 12
        return $this;
72
    }
73
74 14
    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...
75
    {
76 14
        $this->data = $data;
77
78 14
        $this->parse();
79
80 14
        return $this;
81
    }
82
83 2
    public function view(string $view): self
84
    {
85 2
        $this->view = $view;
86
87 2
        return $this;
88
    }
89
90 3
    public function render(): View
91
    {
92 3
        if (empty($this->view)) {
93 1
            throw new Exception('You have to define a view before the page can render.');
94
        }
95
96 2
        return $this->viewFactory->make($this->view, $this->data);
97
    }
98
99 2
    public function toHtml(): string
100
    {
101 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...
102
    }
103
104 6
    public function toArray(): array
0 ignored issues
show
introduced by
Method \Astrotomic\Stancy\Models\Page::toArray() does not have @return annotation for its traversable return value.
Loading history...
105
    {
106 6
        return is_array($this->data) ? $this->data : $this->data->toArray();
107
    }
108
109 2
    public function jsonSerialize(): array
0 ignored issues
show
introduced by
Method \Astrotomic\Stancy\Models\Page::jsonSerialize() does not have @return annotation for its traversable return value.
Loading history...
110
    {
111 2
        return $this->toArray();
112
    }
113
114
    /** {@inheritdoc} */
115 1
    public function toJson($options = 0): string
116
    {
117 1
        return json_encode($this->jsonSerialize(), $options);
118
    }
119
120
    /** {@inheritdoc} */
121 3
    public function toResponse($request): Response
122
    {
123 3
        if ($request->wantsJson()) {
124 1
            return response()->json($this->jsonSerialize());
125
        }
126
127 2
        return response($this->render());
128
    }
129
130 2
    public function toFeedItem(): FeedItem
131
    {
132 2
        if (! ($this->data instanceof PageData)) {
133 1
            throw new Exception(sprintf('The page data has to extend %s to allow transformation to %s.', PageData::class, FeedItem::class));
134
        }
135
136 1
        return $this->data->toFeedItem();
137
    }
138
139 14
    protected function parse(): void
140
    {
141 14
        if ($this->page === null) {
142 14
            return;
143
        }
144
145 6
        $this->data = forward_static_call(
146 6
            [$this->page, 'make'],
147 6
            $this->toArray()
148
        );
149 5
    }
150
}
151