TransformerAbstract::links()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sarala\Transformer;
6
7
use Illuminate\Support\Arr;
8
use League\Fractal\TransformerAbstract as BaseTransformerAbstract;
9
use Sarala\Links;
10
use Sarala\Query\Fields;
11
12
abstract class TransformerAbstract extends BaseTransformerAbstract
13
{
14
    const REQUIRED_FIELDS = [
15
        'id',
16
    ];
17
18
    public function transform($data): array
19
    {
20
        $user = auth(config('sarala.guard'))->user();
21
22
        $links = $this->links($data, $user)->all();
23
        $meta = $this->meta($data, $user);
24
        $data = $this->filterFields($this->data($data));
0 ignored issues
show
Bug introduced by
The method data() does not exist on Sarala\Transformer\TransformerAbstract. Since it exists in all sub-types, consider adding an abstract or default implementation to Sarala\Transformer\TransformerAbstract. ( Ignorable by Annotation )

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

24
        $data = $this->filterFields($this->/** @scrutinizer ignore-call */ data($data));
Loading history...
25
26
        if (! empty($links)) {
27
            $data['links'] = $links;
28
        }
29
30
        if (! empty($meta)) {
31
            $data['meta'] = $meta;
32
        }
33
34
        return $data;
35
    }
36
37
    protected function links($model, $user = null): Links
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

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

37
    protected function links($model, /** @scrutinizer ignore-unused */ $user = null): Links

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $model is not used and could be removed. ( Ignorable by Annotation )

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

37
    protected function links(/** @scrutinizer ignore-unused */ $model, $user = null): Links

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
    {
39
        return Links::make();
40
    }
41
42
    protected function meta($model, $user = null): array
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

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

42
    protected function meta($model, /** @scrutinizer ignore-unused */ $user = null): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $model is not used and could be removed. ( Ignorable by Annotation )

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

42
    protected function meta(/** @scrutinizer ignore-unused */ $model, $user = null): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
    {
44
        return [];
45
    }
46
47
    private function filterFields(array $data): array
48
    {
49
        $resourceName = $this->getCurrentScope()->getResource()->getResourceKey();
50
        /** @var Fields $fields */
51
        $fields = request()->fields();
52
53
        if (! $fields->has($resourceName)) {
54
            return $data;
55
        }
56
57
        $fields = array_merge($fields->get($resourceName), self::REQUIRED_FIELDS);
0 ignored issues
show
Bug introduced by
It seems like $fields->get($resourceName) can also be of type null; however, parameter $arrays of array_merge() does only seem to accept array, 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

57
        $fields = array_merge(/** @scrutinizer ignore-type */ $fields->get($resourceName), self::REQUIRED_FIELDS);
Loading history...
58
59
        return Arr::only($data, $fields);
60
    }
61
}
62