Completed
Push — master ( c915d2...2ad4b2 )
by Alexander
04:32
created

Transformer::callIncludeMethod()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
dl 0
loc 10
rs 9.4285
c 3
b 1
f 0
cc 3
eloc 5
nc 2
nop 3
1
<?php
2
3
namespace Flugg\Responder;
4
5
use League\Fractal\Scope;
6
use League\Fractal\TransformerAbstract;
7
use Flugg\Responder\Contracts\Transformable;
8
9
/**
10
 * An abstract base transformer. All transformers should extend this, and this class
11
 * itself extends the Fractal transformer.
12
 *
13
 * @package 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
     * The transformable model associated with the transformer.
21
     *
22
     * @var Transformable
23
     */
24
    protected $model;
25
26
    /**
27
     * Constructor.
28
     *
29
     * @param Transformable|null $model
30
     */
31
    public function __construct( Transformable $model = null )
32
    {
33
        $this->model = $model;
34
    }
35
36
    /**
37
     * Get avilable includes.
38
     *
39
     * @return array
40
     */
41
    public function getAvailableIncludes()
42
    {
43
        return array_keys( $this->model->getRelations() );
44
    }
45
46
    /**
47
     * This method is fired to loop through available includes, see if any of
48
     * them are requested and permitted for this scope.
49
     *
50
     * @param  Scope $scope
51
     * @param  mixed $data
52
     * @return array
53
     */
54
    public function processIncludedResources( Scope $scope, $data )
55
    {
56
        $includedData = [ ];
57
        $includes = array_merge( $this->getDefaultIncludes(), $this->getAvailableIncludes() );
58
59
        foreach ( $includes as $include ) {
60
            $includedData = $this->includeResourceIfAvailable( $scope, $data, $includedData, $include );
61
        }
62
63
        return $includedData === [ ] ? false : $includedData;
64
    }
65
66
    /**
67
     * Include a resource only if it is available on the method.
68
     *
69
     * @param  Scope  $scope
70
     * @param  mixed  $data
71
     * @param  array  $includedData
72
     * @param  string $include
73
     * @return array
74
     */
75
    protected function includeResourceIfAvailable( Scope $scope, $data, $includedData, $include )
76
    {
77
        if ( $resource = $this->callIncludeMethod( $scope, $include, $data ) ) {
78
            $childScope = $scope->embedChildScope( $include, $resource );
79
80
            $includedData[ $include ] = $childScope->toArray();
81
        }
82
83
        return $includedData;
84
    }
85
86
    /**
87
     * Call Include Method.
88
     *
89
     * @param  Scope  $scope
90
     * @param  string $includeName
91
     * @param  mixed  $data
92
     * @return \League\Fractal\Resource\ResourceInterface
93
     * @throws \Exception
94
     */
95
    protected function callIncludeMethod( Scope $scope, $includeName, $data )
96
    {
97
        if ( ! $data instanceof Transformable || ! $data->relationLoaded( $includeName ) ) {
98
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Flugg\Responder\Transformer::callIncludeMethod of type League\Fractal\Resource\ResourceInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
99
        }
100
101
        $data = $data->$includeName;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $data. This often makes code more readable.
Loading history...
102
103
        return app( Responder::class )->transform( $data );
104
    }
105
}