Completed
Push — master ( 9fa93a...252e01 )
by Alexander
03:36
created

Transformer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 88
rs 10
wmc 8
lcom 2
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRelations() 0 8 1
A setRelations() 0 6 1
A allRelationsAllowed() 0 4 1
A callIncludeMethod() 0 14 3
A includePivot() 0 10 2
1
<?php
2
3
namespace Flugg\Responder;
4
5
use Illuminate\Database\Eloquent\Relations\Pivot;
6
use League\Fractal\Scope;
7
use League\Fractal\TransformerAbstract;
8
9
/**
10
 * An abstract base transformer. Your transformers should extend this class, and this
11
 * class itself extends Fractal's transformer.
12
 *
13
 * @package flugger/laravel-responder
14
 * @author  Alexander Tømmerås <[email protected]>
15
 * @license The MIT License
16
 */
17
abstract class Transformer extends TransformerAbstract
18
{
19
    /**
20
     * A list of all available relations.
21
     *
22
     * @var array
23
     */
24
    protected $relations = ['*'];
25
26
    /**
27
     * Get relations set on the transformer.
28
     *
29
     * @return array
30
     */
31
    public function getRelations():array
32
    {
33
        $relations = array_unique(array_merge($this->getAvailableIncludes(), $this->relations));
34
35
        return array_filter($relations, function($relation) {
36
            return $relation !== '*';
37
        });
38
    }
39
40
    /**
41
     * Set relations on the transformer.
42
     *
43
     * @param  array|string $relations
44
     * @return self
45
     */
46
    public function setRelations($relations)
47
    {
48
        $this->setAvailableIncludes(array_merge($this->availableIncludes, (array) $relations));
49
50
        return $this;
51
    }
52
53
    /**
54
     * Check if the transformer has whitelisted all relations.
55
     *
56
     * @return bool
57
     */
58
    public function allRelationsAllowed():bool
59
    {
60
        return $this->relations == ['*'];
61
    }
62
63
    /**
64
     * Call method for retrieving a relation. This method overrides Fractal's own
65
     * [callIncludeMethod] method to load relations directly from your models.
66
     *
67
     * @param  Scope  $scope
68
     * @param  string $includeName
69
     * @param  mixed  $data
70
     * @return \League\Fractal\Resource\ResourceInterface|bool
71
     * @throws \Exception
72
     */
73
    protected function callIncludeMethod(Scope $scope, $includeName, $data)
74
    {
75
        if ($includeName === 'pivot') {
76
            return $this->includePivot($data->$includeName);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->includePivot($data->{$includeName}); of type League\Fractal\Resource\ResourceInterface|boolean adds the type boolean to the return on line 76 which is incompatible with the return type of the parent method League\Fractal\Transform...ract::callIncludeMethod of type League\Fractal\Resource\ResourceInterface.
Loading history...
77
        }
78
79
        $params = $scope->getManager()->getIncludeParams($scope->getIdentifier($includeName));
80
81
        if (method_exists($this, $includeName)) {
82
            return call_user_func([$this, $includeName], $data, $params);
83
        } else {
84
            return app(Responder::class)->transform($data->$includeName)->getResource();
85
        }
86
    }
87
88
    /**
89
     * Include pivot table data to the response.
90
     *
91
     * @param  Pivot $pivot
92
     * @return \League\Fractal\Resource\ResourceInterface|bool
93
     */
94
    protected function includePivot(Pivot $pivot)
95
    {
96
        if (! method_exists($this, 'transformPivot')) {
97
            return false;
98
        }
99
100
        return app(Responder::class)->transform($pivot, function ($pivot) {
101
            return $this->transformPivot($pivot);
0 ignored issues
show
Bug introduced by
The method transformPivot() does not seem to exist on object<Flugg\Responder\Transformer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
        })->getResource();
103
    }
104
}