IndexPage   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 46
c 1
b 0
f 0
dl 0
loc 241
ccs 49
cts 56
cp 0.875
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 6 1
A responseMeta() 0 3 1
A vueAttributes() 0 14 1
A bulkActionHandle() 0 3 1
A inlineActions() 0 3 1
A bulkActionUrl() 0 7 2
A useIdentificator() 0 5 1
A getResponseCollection() 0 10 2
A responseColumns() 0 3 1
A name() 0 3 1
A listingUrl() 0 7 2
A authorised() 0 3 1
A responsePerPage() 0 3 1
A headers() 0 5 1
A processBulkAction() 0 8 2
1
<?php
2
3
namespace UserAdmin\IndexPage;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\Resources\Json\JsonResource;
7
use Illuminate\Support\Str;
8
use UserAdmin\Http\Requests\BulkActionRequest;
9
use UserAdmin\Http\Resources\ListResource;
10
use UserAdmin\IndexPage\Actions\AbstractInlineAction;
11
use UserAdmin\IndexPage\Columns\AbstractColumn;
12
13
abstract class IndexPage
14
{
15
    /**
16
     * @var string Index page name
17
     */
18
    public static string $name = '';
19
20
    /**
21
     * @var string|JsonResource
22
     */
23
    public string $responseResource = ListResource::class;
24
25
    /**
26
     * @var string|null
27
     */
28
    public ?string $identifier = 'id';
29
30
    /**
31
     * @var bool
32
     */
33
    public bool $usePagination = true;
34
35
    /**
36
     * Get page name (class identification).
37
     *
38
     * @return string
39
     */
40 7
    public static function name(): string
41
    {
42 7
        return static::$name;
43
    }
44
45
    /**
46
     * Get columns list.
47
     *
48
     * @return array
49
     */
50
    abstract public function columns(): array;
51
52
    /**
53
     * @param Request $request
54
     *
55
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
56
     */
57
    abstract public function query(Request $request);
58
59
    /**
60
     * Get listing url.
61
     *
62
     * @return string
63
     */
64 1
    public function listingUrl(): string
65
    {
66 1
        if (property_exists($this, 'listingUrl')) {
67
            return (string) $this->listingUrl;
68
        }
69
70 1
        return route('user-admin-index-page.list');
71
    }
72
73
    /**
74
     * Get bulk action url.
75
     *
76
     * @return string
77
     */
78 1
    public function bulkActionUrl(): string
79
    {
80 1
        if (property_exists($this, 'bulkActionUrl')) {
81
            return (string) $this->bulkActionUrl;
82
        }
83
84 1
        return route('user-admin-index-page.bulk-action');
85
    }
86
87
    /**
88
     * Update identificator key.
89
     *
90
     * @param string|null $key
91
     *
92
     * @return $this
93
     */
94
    public function useIdentificator(?string $key = null): self
95
    {
96
        $this->identifier = (string) $key;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Check is request authorised
103
     *
104
     * @param Request $request
105
     *
106
     * @return bool
107
     */
108 4
    public function authorised(Request $request): bool
109
    {
110 4
        return true;
111
    }
112
113
    /**
114
     * @param BulkActionRequest $request
115
     *
116
     * @return mixed
117
     * @throws \Exception
118
     */
119 2
    public function processBulkAction(BulkActionRequest $request)
120
    {
121 2
        $methodName = Str::camel($request->group()) . 'BulkActionHandle';
122 2
        if (method_exists($this, $methodName)) {
123
            return $this->$methodName($request);
124
        }
125
126 2
        return $this->bulkActionHandle($request);
127
    }
128
129
    /**
130
     * @param BulkActionRequest $request
131
     *
132
     * @return mixed
133
     * @throws \Exception
134
     */
135 1
    public function bulkActionHandle(BulkActionRequest $request)
136
    {
137 1
        throw new \Exception('Please override method :' . __METHOD__);
138
    }
139
140
    /**
141
     * Process request
142
     *
143
     * @param Request $request
144
     *
145
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
146
     */
147 2
    public function process(Request $request)
148
    {
149 2
        $query = $this->query($request);
150
151 2
        return $this->responseResource::collection($this->getResponseCollection($request, $query))
152 2
                                      ->additional($this->responseMeta($request, $query));
153
    }
154
155
    /**
156
     * Default response meta.
157
     *
158
     * @param Request $request
159
     * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query
160
     *
161
     * @return array
162
     */
163 2
    public function responseMeta(Request $request, $query): array
164
    {
165 2
        return [];
166
    }
167
168
    /**
169
     * Response collection used in resource.
170
     *
171
     * @param Request $request
172
     * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query
173
     *
174
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
175
     */
176 2
    public function getResponseCollection(Request $request, $query)
177
    {
178 2
        $cols = $this->responseColumns($request, $query);
179 2
        if ($this->usePagination) {
180 2
            $perPage = $this->responsePerPage($request, $query);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $perPage is correct as $this->responsePerPage($request, $query) targeting UserAdmin\IndexPage\IndexPage::responsePerPage() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
181
182 2
            return $query->paginate($perPage, $cols);
183
        }
184
185
        return $query->get($cols);
186
    }
187
188
    /**
189
     * Get response columns.
190
     *
191
     * @param Request $request
192
     * @param $query
193
     *
194
     * @return string[]
195
     */
196 2
    public function responseColumns(Request $request, $query): array
197
    {
198 2
        return [ '*' ];
199
    }
200
201
    /**
202
     * Get per page value.
203
     *
204
     * @param Request $request
205
     * @param $query
206
     *
207
     * @return int|null
208
     */
209 1
    public function responsePerPage(Request $request, $query): ?int
210
    {
211 1
        return null;
212
    }
213
214
    /**
215
     * @return array|string[]
216
     */
217 1
    public function inlineActions(): array
218
    {
219 1
        return [];
220
    }
221
222
    /**
223
     * Vue tag attributes.
224
     *
225
     * @return string
226
     */
227 1
    public function vueAttributes(): string
228
    {
229 1
        $pageName      = static::name();
230 1
        $headers       = json_encode($this->headers());
231 1
        $inlineActions = collect($this->inlineActions())->map(fn (AbstractInlineAction $action) => $action->toArray())->toArray();
232 1
        $inlineActions = json_encode($inlineActions);
233
234 1
        return "
235 1
        page-name='{$pageName}'
236 1
        identifier='{$this->identifier}'
237 1
        listing-url='{$this->listingUrl()}'
238 1
        bulk-action-url='{$this->bulkActionUrl()}'
239 1
        :inline-actions='{$inlineActions}'
240 1
        :headers='{$headers}'
241
        ";
242
    }
243
244
    /**
245
     * Resolved headers data.
246
     *
247
     * @return array
248
     */
249 1
    public function headers(): array
250
    {
251 1
        return collect($this->columns())
252 1
            ->map(fn (AbstractColumn $row) => $row->headerData())
253 1
            ->toArray();
254
    }
255
}
256