Passed
Push — master ( b5b8d0...1c0f33 )
by Tom
03:11
created

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