Completed
Pull Request — master (#35)
by Tom
04:36
created

Page::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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
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
@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 27
    public function __construct(ViewFactoryContract $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...
36
    {
37 27
        $this->viewFactory = $viewFactory;
38
39 27
        $this->view($data['_view'] ?? null);
40
41 27
        $page = $page ?? $data['_pageData'] ?? null;
42
43 27
        $data = array_merge($data, $this->getSheetData($data['_sheets'] ?? []));
44
45 26
        $this->data($data);
46 26
        $this->page($page);
47 23
    }
48
49 1
    public function __get($name)
0 ignored issues
show
introduced by
Method \Astrotomic\Stancy\Models\Page::__get() does not have parameter type hint nor @param annotation for its parameter $name.
Loading history...
introduced by
Method \Astrotomic\Stancy\Models\Page::__get() does not have return type hint nor @return annotation for its return value.
Loading history...
50
    {
51 1
        if (! isset($this->data->$name)) {
52
            throw new RuntimeException(sprintf('The property [%s] does not exist on %s with %s page data.', $name, static::class, get_class($this->data)));
0 ignored issues
show
Bug introduced by
It seems like $this->data can also be of type array; however, parameter $object of get_class() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
            throw new RuntimeException(sprintf('The property [%s] does not exist on %s with %s page data.', $name, static::class, get_class(/** @scrutinizer ignore-type */ $this->data)));
Loading history...
53
        }
54
55 1
        return $this->data->$name;
56
    }
57
58 26
    public function page(?string $page): PageContract
59
    {
60 26
        if (is_string($page)) {
61 18
            if (! is_subclass_of($page, PageData::class, true)) {
62 2
                throw new Exception(sprintf('The page data class [%s] has to extend %s.', $page, PageData::class));
63
            }
64
        }
65
66 24
        $this->page = $page;
67
68 24
        $this->parse();
69
70 23
        return $this;
71
    }
72
73 26
    public function data(array $data): PageContract
0 ignored issues
show
introduced by
Method \Astrotomic\Stancy\Models\Page::data() does not have @param annotation for its traversable parameter $data.
Loading history...
74
    {
75 26
        $this->data = $this->prepareData($data);
76
77 26
        $this->parse();
78
79 26
        return $this;
80
    }
81
82 27
    public function view(?string $view): PageContract
83
    {
84 27
        $this->view = $view;
85
86 27
        return $this;
87
    }
88
89 6
    public function render(): View
90
    {
91 6
        if (empty($this->view)) {
92 1
            throw new Exception('You have to define a view before the page can render.');
93
        }
94
95 5
        return $this->viewFactory->make($this->view, $this->toArray());
96
    }
97
98 3
    public function toHtml(): string
99
    {
100 3
        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...
101
    }
102
103 17
    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...
104
    {
105 17
        return is_array($this->data) ? $this->data : $this->data->all();
106
    }
107
108 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...
109
    {
110 2
        return is_array($this->data) ? $this->data : $this->data->toArray();
111
    }
112
113
    /** {@inheritdoc} */
114 1
    public function toJson($options = 0): string
115
    {
116 1
        return json_encode($this->jsonSerialize(), $options);
117
    }
118
119
    /** {@inheritdoc} */
120 6
    public function toResponse($request): Response
121
    {
122 6
        if ($request->wantsJson()) {
123 1
            return response()->json($this->jsonSerialize());
124
        }
125
126 5
        return response($this->render());
127
    }
128
129 4
    public function toFeedItem(): FeedItem
130
    {
131 4
        if (! ($this->data instanceof PageData)) {
132 1
            throw new Exception(sprintf('The page data has to extend %s to allow transformation to %s.', PageData::class, FeedItem::class));
133
        }
134
135 3
        return $this->data->toFeedItem();
136
    }
137
138 3
    public function toSitemapItem(): Tag
139
    {
140 3
        if (! ($this->data instanceof PageData)) {
141 1
            throw new Exception(sprintf('The page data has to extend %s to allow transformation to %s.', PageData::class, Tag::class));
142
        }
143
144 2
        return $this->data->toSitemapItem();
145
    }
146
147 3
    public function getUrl(): string
148
    {
149 3
        if (! ($this->data instanceof Routable)) {
150 1
            throw new Exception(sprintf('The page data has to be instance of %s to allow access to the URL.', Routable::class));
151
        }
152
153 2
        return $this->data->getUrl();
154
    }
155
156 26
    protected function parse(): void
157
    {
158 26
        if ($this->page === null) {
159 26
            return;
160
        }
161
162 16
        $this->data = forward_static_call(
163 16
            [$this->page, 'make'],
164 16
            $this->toArray()
165
        );
166 14
    }
167
168 27
    protected function getSheetData(array $sheets): array
0 ignored issues
show
introduced by
Method \Astrotomic\Stancy\Models\Page::getSheetData() does not have @param annotation for its traversable parameter $sheets.
Loading history...
introduced by
Method \Astrotomic\Stancy\Models\Page::getSheetData() does not have @return annotation for its traversable return value.
Loading history...
169
    {
170 27
        if (empty($sheets)) {
171 25
            return [];
172
        }
173
174 2
        if (! Arr::isAssoc($sheets)) {
175 1
            throw new RuntimeException('The [_sheets] data has to be an associative array.');
176
        }
177
178 1
        $data = [];
179
180 1
        foreach ($sheets as $key => $sheet) {
181 1
            [$collection, $path] = explode(':', $sheet);
182
183 1
            if ($path === '*') {
184
                $data[$key] = Sheets::collection($collection)->all()->map(function (Sheet $sheet) {
185 1
                    return $this->prepareData($sheet->toArray());
186 1
                })->all();
187
188 1
                continue;
189
            }
190
191 1
            $data[$key] = $this->prepareData(Sheets::collection($collection)->get($path)->toArray());
192
        }
193
194 1
        return $data;
195
    }
196
197 26
    protected function prepareData(array $data): array
0 ignored issues
show
introduced by
Method \Astrotomic\Stancy\Models\Page::prepareData() does not have @param annotation for its traversable parameter $data.
Loading history...
introduced by
Method \Astrotomic\Stancy\Models\Page::prepareData() does not have @return annotation for its traversable return value.
Loading history...
198
    {
199 26
        unset($data['_view'], $data['_pageData'], $data['_sheets']);
200
201 26
        return $data;
202
    }
203
}
204