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

MakesResources::includeResourceFromModel()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 12
ccs 0
cts 7
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder\Transformers\Concerns;
4
5
use Flugg\Responder\Contracts\Resources\ResourceFactory;
6
use Illuminate\Contracts\Container\Container;
7
use Illuminate\Database\Eloquent\Model;
8
use League\Fractal\Resource\ResourceInterface;
9
10
/**
11
 * A trait to be used by a transformer to make related resources.
12
 *
13
 * @package flugger/laravel-responder
14
 * @author  Alexander Tømmerås <[email protected]>
15
 * @license The MIT License
16
 */
17
trait MakesResources
18
{
19
    /**
20
     * A list of cached related resources.
21
     *
22
     * @var \League\Fractal\ResourceInterface[]
23
     */
24
    protected $resources = [];
25
26
    /**
27
     * Make a resource.
28
     *
29
     * @param  null                                                           $data
30
     * @param  \Flugg\Responder\Transformers\Transformer|string|callable|null $transformer
31
     * @param  string|null                                                    $resourceKey
32
     * @return \League\Fractal\Resource\ResourceInterface
33
     */
34
    protected function resource($data = null, $transformer = null, string $resourceKey = null): ResourceInterface
35
    {
36
        $resourceFactory = $this->resolveContainer()->make(ResourceFactory::class);
37
38
        return $resourceFactory->make($data, $transformer, $resourceKey);
39
    }
40
41
    /**
42
     * Include a related resource.
43
     *
44
     * @param  string $identifier
45
     * @param  mixed  $data
46
     * @param  array  $parameters
47
     * @return \League\Fractal\Resource\ResourceInterface
48
     * @throws \LogicException
49
     */
50
    protected function includeResource(string $identifier, $data, array $parameters): ResourceInterface
51
    {
52
        if (method_exists($this, $method = 'include' . ucfirst($identifier))) {
53
            $resource = $this->$method($data, $parameters);
54
        } elseif ($data instanceof Model) {
55
            $resource = $this->includeResourceFromModel($data, $identifier);
56
        } else {
57
            throw new LogicException('Relation [' . $identifier . '] not found in [' . get_class($this) . '].');
58
        }
59
60
        return $resource;
61
    }
62
63
    /**
64
     * Include a related resource from a model.
65
     *
66
     * @param \Illuminate\Database\Eloquent\Model $model
67
     * @param string                              $identifier
68
     * @return \League\Fractal\Resource\ResourceInterface
69
     */
70
    protected function includeResourceFromModel(Model $model, string $identifier): ResourceInterface
71
    {
72
        $data = $this->resolveRelation($model, $identifier);
73
74
        if (! count($data)) {
75
            return $this->resource($data, null, $identifier);
76
        } elseif (key_exists($identifier, $this->resources)) {
77
            return $this->resources[$identifier]->setData($data);
78
        }
79
80
        return $this->resources[$identifier] = $this->resource($data, null, $identifier);
81
    }
82
83
    /**
84
     * Resolve a container using the resolver callback.
85
     *
86
     * @return \Illuminate\Contracts\Container\Container
87
     */
88
    protected abstract function resolveContainer(): Container;
89
90
    /**
91
     * Resolve relation data from a model.
92
     *
93
     * @param  \Illuminate\Database\Eloquent\Model $model
94
     * @param  string                              $identifier
95
     * @return mixed
96
     */
97
    protected abstract function resolveRelation(Model $model, string $identifier);
98
}