Completed
Push — master ( 5e287c...36811c )
by Alexander
03:40
created

Transformer::includePivot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 1
b 0
f 1
1
<?php
2
3
namespace Flugg\Responder;
4
5
use Flugg\Responder\Contracts\Transformable;
6
use Illuminate\Database\Eloquent\Relations\Pivot;
7
use League\Fractal\Scope;
8
use League\Fractal\TransformerAbstract;
9
10
/**
11
 * An abstract base transformer. Yourr transformers should extend this, and this class
12
 * itself extends Fractal's transformer.
13
 *
14
 * @package Laravel Responder
15
 * @author  Alexander Tømmerås <[email protected]>
16
 * @license The MIT License
17
 */
18
abstract class Transformer extends TransformerAbstract
19
{
20
    /**
21
     * The transformable model associated with the transformer.
22
     *
23
     * @var Transformable
24
     */
25
    protected $model;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param Transformable|null $model
31
     */
32
    public function __construct( Transformable $model = null )
33
    {
34
        $this->model = $model;
35
    }
36
37
    /**
38
     * Get avilable includes.
39
     *
40
     * @return array
41
     */
42
    public function getAvailableIncludes()
43
    {
44
        return array_keys( $this->model->getRelations() );
45
    }
46
47
    /**
48
     * This method is fired to loop through available includes, see if any of
49
     * them are requested and permitted for this scope.
50
     *
51
     * @param  Scope $scope
52
     * @param  mixed $data
53
     * @return array
54
     */
55
    public function processIncludedResources( Scope $scope, $data )
56
    {
57
        $includedData = [ ];
58
        $includes = array_merge( $this->getDefaultIncludes(), $this->getAvailableIncludes() );
59
60
        foreach ( $includes as $include ) {
61
            $includedData = $this->includeResourceIfAvailable( $scope, $data, $includedData, $include );
62
        }
63
64
        return $includedData === [ ] ? false : $includedData;
65
    }
66
67
    /**
68
     * Include a resource only if it is available on the method.
69
     *
70
     * @param  Scope  $scope
71
     * @param  mixed  $data
72
     * @param  array  $includedData
73
     * @param  string $include
74
     * @return array
75
     */
76
    protected function includeResourceIfAvailable( Scope $scope, $data, $includedData, $include )
77
    {
78
        if ( $resource = $this->callIncludeMethod( $scope, $include, $data ) ) {
79
            $childScope = $scope->embedChildScope( $include, $resource );
0 ignored issues
show
Bug introduced by
It seems like $resource defined by $this->callIncludeMethod($scope, $include, $data) on line 78 can also be of type boolean; however, League\Fractal\Scope::embedChildScope() does only seem to accept object<League\Fractal\Resource\ResourceInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
80
81
            $includedData[ $include ] = $childScope->toArray();
82
        }
83
84
        return $includedData;
85
    }
86
87
    /**
88
     * Call Include Method.
89
     *
90
     * @param  Scope  $scope
91
     * @param  string $includeName
92
     * @param  mixed  $data
93
     * @return \League\Fractal\Resource\ResourceInterface|bool
94
     * @throws \Exception
95
     */
96
    protected function callIncludeMethod( Scope $scope, $includeName, $data )
97
    {
98
        if ( ! $data instanceof Transformable || ! $data->relationLoaded( $includeName ) ) {
99
            return false;
100
        }
101
102
        $relation = $data->$includeName;
103
104
        if ( $relation instanceof Pivot ) {
105
            return $this->includePivot( $relation );
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->includePivot($relation); of type League\Fractal\Resource\ResourceInterface|boolean adds the type boolean to the return on line 105 which is incompatible with the return type of the parent method League\Fractal\Transform...ract::callIncludeMethod of type false|League\Fractal\Resource\ResourceAbstract.
Loading history...
106
        }
107
108
        return app( 'responder.success' )->transform( $relation );
109
    }
110
111
    /**
112
     * Include pivot table data to the response.
113
     *
114
     * @param  Pivot $pivot
115
     * @return \League\Fractal\Resource\ResourceInterface|bool
116
     */
117
    protected function includePivot( Pivot $pivot )
118
    {
119
        if ( method_exists( $this, 'transformPivot' ) ) {
120
            $data = $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...
121
122
            return app( 'responder.success' )->transform( $pivot, function () use ( $data ) {
123
                return $data;
124
            } );
125
        }
126
127
        return false;
128
    }
129
}