Passed
Push — master ( f8044c...6aa085 )
by Alexander
03:26
created

OverridesFractal   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 94
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A callIncludeMethod() 0 6 1
A resolveScopedIncludes() 0 11 1
A resolveScopedParameters() 0 4 1
getCurrentScope() 0 1 ?
includeResource() 0 1 ?
A getAvailableIncludes() 0 12 3
A getDefaultIncludes() 0 6 2
1
<?php
2
3
namespace Flugg\Responder\Transformers\Concerns;
4
5
use Illuminate\Support\Collection;
6
use League\Fractal\Resource\ResourceInterface;
7
use League\Fractal\Scope;
8
9
/**
10
 * A trait to be used by a transformer to override Fractal's transformer methods.
11
 *
12
 * @package flugger/laravel-responder
13
 * @author  Alexander Tømmerås <[email protected]>
14
 * @license The MIT License
15
 */
16
trait OverridesFractal
17
{
18
    /**
19
     * Overrides Fractal's getter for available includes.
20
     *
21
     * @return array
22
     */
23
    public function getAvailableIncludes()
24
    {
25
        if (! isset($this->relations)) {
26
            return [];
27
        } elseif ($this->relations == ['*']) {
28
            return $this->resolveScopedIncludes($this->getCurrentScope());
29
        }
30
31
        return array_filter($this->relations, function ($relation) {
32
            return $relation != '*';
33
        });
34
    }
35
36
    /**
37
     * Overrides Fractal's getter for default includes.
38
     *
39
     * @return array
40
     */
41
    public function getDefaultIncludes()
42
    {
43
        return Collection::make($this->load)->map(function ($transformer, $relation) {
44
            return is_numeric($relation) ? $transformer : $relation;
45
        })->all();
46
    }
47
48
    /**
49
     * Overrides Fractal's method for including a relation.
50
     *
51
     * @param  \League\Fractal\Scope $scope
52
     * @param  string                $identifier
53
     * @param  mixed                 $data
54
     * @return \League\Fractal\Resource\ResourceInterface
55
     */
56
    protected function callIncludeMethod(Scope $scope, $identifier, $data)
57
    {
58
        $parameters = $this->resolveScopedParameters($scope, $identifier);
59
60
        return $this->includeResource($identifier, $data, $parameters);
61
    }
62
63
    /**
64
     * Resolve scoped includes for the given scope.
65
     *
66
     * @param  \League\Fractal\Scope $scope
67
     * @return array
68
     */
69
    protected function resolveScopedIncludes(Scope $scope): array
70
    {
71
        $level = count($scope->getParentScopes());
72
        $includes = $scope->getManager()->getRequestedIncludes();
73
74
        return Collection::make($includes)->map(function ($include) {
75
            return explode('.', $include);
76
        })->filter(function ($include) use ($level) {
77
            return count($include) > $level;
78
        })->pluck($level)->unique()->all();
79
    }
80
81
    /**
82
     * Resolve scoped parameters for the given scope.
83
     *
84
     * @param  \League\Fractal\Scope $scope
85
     * @param  string                $identifier
86
     * @return array
87
     */
88
    protected function resolveScopedParameters(Scope $scope, string $identifier): array
89
    {
90
        return iterator_to_array($scope->getManager()->getIncludeParams($scope->getIdentifier($identifier)));
91
    }
92
93
    /**
94
     * Get the current scope of the transformer.
95
     *
96
     * @return \League\Fractal\Scope
97
     */
98
    public abstract function getCurrentScope();
99
100
    /**
101
     * Include a related resource.
102
     *
103
     * @param  string $identifier
104
     * @param  mixed  $data
105
     * @param  array  $parameters
106
     * @return \League\Fractal\Resource\ResourceInterface
107
     */
108
    protected abstract function includeResource(string $identifier, $data, array $parameters): ResourceInterface;
109
}