Passed
Push — master ( a9d122...71aa5b )
by Tom
02:53
created

Page::toResponse()   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
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Astrotomic\Stancy\Models;
4
5
use Exception;
6
use Illuminate\Contracts\Support\Htmlable;
7
use Illuminate\Contracts\Support\Renderable;
8
use Illuminate\Contracts\Support\Responsable;
9
use Illuminate\Contracts\View\Factory as ViewFactory;
10
use Illuminate\Contracts\View\View;
11
use Spatie\Sheets\Facades\Sheets;
12
use Symfony\Component\HttpFoundation\Response;
13
14
class Page implements Htmlable, Renderable, Responsable
15
{
16
    /** @var string|null */
17
    protected $view;
18
19
    /** @var string|null */
20
    protected $page;
21
22
    /** @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...
23
    protected $data = [];
24
25
    /** @var ViewFactory */
26
    protected $viewFactory;
27
28 10
    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...
29
    {
30 10
        $this->viewFactory = $viewFactory;
31 10
        $this->data($data);
32 10
        $this->page($page);
33 8
    }
34
35 9
    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...
36
    {
37 9
        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...
38 9
            'data' => $data,
39 9
            'page' => $page,
40
        ]);
41
    }
42
43 5
    public static function makeFromSheet(string $collection, string $name, ?string $page = null): self
44
    {
45 5
        $sheet = Sheets::collection($collection)->get($name);
46
47 5
        if ($sheet === null) {
48 1
            throw new Exception(sprintf('No sheet found in collection [%s] with name [%s].', $collection, $name));
49
        }
50
51 4
        return static::make($sheet->toArray(), $page);
52
    }
53
54 10
    public function page(?string $page): self
55
    {
56 10
        if (is_string($page)) {
57 5
            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 8
        $this->page = $page;
63
64 8
        $this->parse();
65
66 8
        return $this;
67
    }
68
69 10
    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...
70
    {
71 10
        $this->data = $data;
72
73 10
        $this->parse();
74
75 10
        return $this;
76
    }
77
78 2
    public function view(string $view): self
79
    {
80 2
        $this->view = $view;
81
82 2
        return $this;
83
    }
84
85 3
    public function render(): View
86
    {
87 3
        if (empty($this->view)) {
88 1
            throw new Exception('You have to define a view before the page can render.');
89
        }
90
91 2
        return $this->viewFactory->make($this->view, $this->data);
92
    }
93
94 2
    public function toHtml(): string
95
    {
96 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...
97
    }
98
99
    /**
100
     * @inheritDoc
0 ignored issues
show
introduced by
You should change the following
<fg=red>- * @inheritDoc
</><fg=green>+ * @inheritdoc
</>
Loading history...
101
     */
102 3
    public function toResponse($request): Response
103
    {
104 3
        if ($request->wantsJson()) {
105 1
            return response()->json($this->data);
106
        }
107
108 2
        return response($this->render());
109
    }
110
111 10
    protected function parse(): void
112
    {
113 10
        if ($this->page === null) {
114 10
            return;
115
        }
116
117 3
        $this->data = forward_static_call(
118 3
            [$this->page, 'make'],
119 3
            is_array($this->data) ? $this->data : $this->data->toArray()
120
        );
121 2
    }
122
}
123