Completed
Pull Request — master (#56)
by Alexander
05:10
created

ResourceKeyResolver::bind()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
cc 2
eloc 3
nc 1
nop 2
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
1
<?php
2
3
namespace Flugg\Responder\Resources;
4
5
use Flugg\Responder\Contracts\Resources\ResourceKeyResolver as ResourceKeyResolverContract;
6
use Illuminate\Database\Eloquent\Model;
7
use Traversable;
8
9
/**
10
 * This class is responsible for resolving resource keys.
11
 *
12
 * @package flugger/laravel-responder
13
 * @author  Alexander Tømmerås <[email protected]>
14
 * @license The MIT License
15
 */
16
class ResourceKeyResolver implements ResourceKeyResolverContract
17
{
18
    /**
19
     * Transformable to resource key mappings.
20
     *
21
     * @var array
22
     */
23
    protected $bindings = [];
24
25
    /**
26
     * Register a transformable to resource key binding.
27
     *
28
     * @param  string|array $transformable
29
     * @param  string       $resourceKey
30
     * @return void
31
     */
32 2
    public function bind($transformable, string $resourceKey)
33
    {
34 2
        $this->bindings = array_merge($this->bindings, is_array($transformable) ? $transformable : [
35 2
            $transformable => $resourceKey,
36
        ]);
37 2
    }
38
39
    /**
40
     * Resolve a resource key from the given data.
41
     *
42
     * @param  mixed $data
43
     * @return string|null
44
     */
45 5
    public function resolve($data)
46
    {
47 5
        $transformable = $this->resolveTransformable($data);
48
49 5 View Code Duplication
        if (is_object($transformable) && key_exists(get_class($transformable), $this->bindings)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50 2
            return $this->bindings[get_class($transformable)];
51
        }
52
53 3
        if ($transformable instanceof Model) {
54 2
            return $this->resolveFromModel($transformable);
55
        }
56
57 1
        return null;
58
    }
59
60
    /**
61
     * Resolve a transformable from the given data.
62
     *
63
     * @param  mixed $data
64
     * @return mixed
65
     */
66 5
    protected function resolveTransformable($data)
67
    {
68 5
        if (is_array($data) || $data instanceof Traversable) {
69 2
            foreach ($data as $item) {
70 1
                return $item;
71
            }
72
        }
73
74 4
        return $data;
75
    }
76
77
    /**
78
     * Resolve a resource key from the given model.
79
     *
80
     * @param  \Illuminate\Database\Eloquent\Model $model
81
     * @return string
82
     */
83 2
    protected function resolveFromModel(Model $model)
84
    {
85 2
        if (method_exists($model, 'getResourceKey')) {
86 1
            return $model->getResourceKey();
87
        }
88
89 1
        return $model->getTable();
90
    }
91
}