Completed
Push — master ( b3431b...b69b2e )
by Tom
03:17 queued 10s
created

Page::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Astrotomic\Stancy\Models;
4
5
use Astrotomic\Stancy\Contracts\Page as PageContract;
6
use Astrotomic\Stancy\Contracts\Routable;
7
use Exception;
8
use Illuminate\Contracts\View\Factory as ViewFactoryContract;
9
use Illuminate\Contracts\View\View;
10
use Illuminate\Support\Arr;
11
use Illuminate\Support\Traits\Macroable;
12
use RuntimeException;
13
use Spatie\Feed\FeedItem;
14
use Spatie\Sheets\Facades\Sheets;
15
use Spatie\Sheets\Sheet;
16
use Spatie\Sitemap\Tags\Tag;
17
use Symfony\Component\HttpFoundation\Response;
18
19
class Page implements PageContract
20
{
21
    use Macroable;
0 ignored issues
show
Bug introduced by jshayes
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by Astrotomic\Stancy\Models\Page.
Loading history...
22
23
    /** @var string|null */
24
    protected $view;
25
26
    /** @var string|null */
27
    protected $page;
28
29
    /** @var PageData|array */
0 ignored issues
show
introduced by Gummibeer
@var annotation of property \Astrotomic\Stancy\Models\Page::$data does not specify type hint for its items.
Loading history...
30
    protected $data = [];
31
32
    /** @var ViewFactoryContract */
33
    protected $viewFactory;
34
35 28
    public function __construct(ViewFactoryContract $viewFactory, array $data = [], ?string $page = null)
0 ignored issues
show
introduced by Gummibeer
Method \Astrotomic\Stancy\Models\Page::__construct() does not have @param annotation for its traversable parameter $data.
Loading history...
36
    {
37 28
        $this->viewFactory = $viewFactory;
38
39 28
        $this->view($data['_view'] ?? null);
40
41 28
        $page = $page ?? $data['_pageData'] ?? null;
42
43 28
        $data = array_merge($data, $this->getSheetData($data['_sheets'] ?? []));
44
45 27
        $this->data($data);
46 27
        $this->page($page);
47 24
    }
48
49 2
    public function __get($name)
0 ignored issues
show
introduced by Tom Witkowski
Method \Astrotomic\Stancy\Models\Page::__get() does not have parameter type hint nor @param annotation for its parameter $name.
Loading history...
introduced by Tom Witkowski
Method \Astrotomic\Stancy\Models\Page::__get() does not have return type hint nor @return annotation for its return value.
Loading history...
50
    {
51 2
        return data_get($this->data, $name);
52
    }
53
54 27
    public function page(?string $page): PageContract
55
    {
56 27
        if (is_string($page)) {
57 18
            if (! is_subclass_of($page, PageData::class, true)) {
58 2
                throw new Exception(sprintf('The page data class [%s] has to extend %s.', $page, PageData::class));
59
            }
60
        }
61
62 25
        $this->page = $page;
63
64 25
        $this->parse();
65
66 24
        return $this;
67
    }
68
69 27
    public function data(array $data): PageContract
0 ignored issues
show
introduced by Gummibeer
Method \Astrotomic\Stancy\Models\Page::data() does not have @param annotation for its traversable parameter $data.
Loading history...
70
    {
71 27
        $this->data = $this->prepareData($data);
72
73 27
        $this->parse();
74
75 27
        return $this;
76
    }
77
78 28
    public function view(?string $view): PageContract
79
    {
80 28
        $this->view = $view;
81
82 28
        return $this;
83
    }
84
85 6
    public function render(): View
86
    {
87 6
        if (empty($this->view)) {
88 1
            throw new Exception('You have to define a view before the page can render.');
89
        }
90
91 5
        return $this->viewFactory->make($this->view, $this->toArray());
92
    }
93
94 3
    public function toHtml(): string
95
    {
96 3
        return $this->render()->render();
0 ignored issues
show
Bug Best Practice introduced by Gummibeer
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...
97
    }
98
99 17
    public function toArray(): array
0 ignored issues
show
introduced by Gummibeer
Method \Astrotomic\Stancy\Models\Page::toArray() does not have @return annotation for its traversable return value.
Loading history...
100
    {
101 17
        return is_array($this->data) ? $this->data : $this->data->all();
102
    }
103
104 2
    public function jsonSerialize(): array
0 ignored issues
show
introduced by Gummibeer
Method \Astrotomic\Stancy\Models\Page::jsonSerialize() does not have @return annotation for its traversable return value.
Loading history...
105
    {
106 2
        return is_array($this->data) ? $this->data : $this->data->toArray();
107
    }
108
109
    /** {@inheritdoc} */
110 1
    public function toJson($options = 0): string
111
    {
112 1
        return json_encode($this->jsonSerialize(), $options);
113
    }
114
115
    /** {@inheritdoc} */
116 6
    public function toResponse($request): Response
117
    {
118 6
        if ($request->wantsJson()) {
119 1
            return response()->json($this->jsonSerialize());
120
        }
121
122 5
        return response($this->render());
123
    }
124
125 4
    public function toFeedItem(): FeedItem
126
    {
127 4
        if (! ($this->data instanceof PageData)) {
128 1
            throw new Exception(sprintf('The page data has to extend %s to allow transformation to %s.', PageData::class, FeedItem::class));
129
        }
130
131 3
        return $this->data->toFeedItem();
132
    }
133
134 3
    public function toSitemapItem(): Tag
135
    {
136 3
        if (! ($this->data instanceof PageData)) {
137 1
            throw new Exception(sprintf('The page data has to extend %s to allow transformation to %s.', PageData::class, Tag::class));
138
        }
139
140 2
        return $this->data->toSitemapItem();
141
    }
142
143 3
    public function getUrl(): string
144
    {
145 3
        if (! ($this->data instanceof Routable)) {
146 1
            throw new Exception(sprintf('The page data has to be instance of %s to allow access to the URL.', Routable::class));
147
        }
148
149 2
        return $this->data->getUrl();
150
    }
151
152 27
    protected function parse(): void
153
    {
154 27
        if ($this->page === null) {
155 27
            return;
156
        }
157
158 16
        $this->data = forward_static_call(
159 16
            [$this->page, 'make'],
160 16
            $this->toArray()
161
        );
162 14
    }
163
164 28
    protected function getSheetData(array $sheets): array
0 ignored issues
show
introduced by Gummibeer
Method \Astrotomic\Stancy\Models\Page::getSheetData() does not have @param annotation for its traversable parameter $sheets.
Loading history...
introduced by Gummibeer
Method \Astrotomic\Stancy\Models\Page::getSheetData() does not have @return annotation for its traversable return value.
Loading history...
165
    {
166 28
        if (empty($sheets)) {
167 26
            return [];
168
        }
169
170 2
        if (! Arr::isAssoc($sheets)) {
171 1
            throw new RuntimeException('The [_sheets] data has to be an associative array.');
172
        }
173
174 1
        $data = [];
175
176 1
        foreach ($sheets as $key => $sheet) {
177 1
            [$collection, $path] = explode(':', $sheet);
178
179 1
            if ($path === '*') {
180
                $data[$key] = Sheets::collection($collection)->all()->map(function (Sheet $sheet) {
181 1
                    return $this->prepareData($sheet->toArray());
182 1
                })->all();
183
184 1
                continue;
185
            }
186
187 1
            $data[$key] = $this->prepareData(Sheets::collection($collection)->get($path)->toArray());
188
        }
189
190 1
        return $data;
191
    }
192
193 27
    protected function prepareData(array $data): array
0 ignored issues
show
introduced by Gummibeer
Method \Astrotomic\Stancy\Models\Page::prepareData() does not have @param annotation for its traversable parameter $data.
Loading history...
introduced by Gummibeer
Method \Astrotomic\Stancy\Models\Page::prepareData() does not have @return annotation for its traversable return value.
Loading history...
194
    {
195 27
        unset($data['_view'], $data['_pageData'], $data['_sheets']);
196
197 27
        return $data;
198
    }
199
}
200