ResourceKeyResolver   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 18.42 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 14
loc 76
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A bind() 0 6 2
A resolve() 14 14 4
A resolveFromModel() 0 8 2
A resolveTransformableItem() 0 10 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
44
     */
45 50 View Code Duplication
    public function resolve($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
46
    {
47 50
        $transformable = $this->resolveTransformableItem($data);
48
49 50
        if (is_object($transformable) && key_exists(get_class($transformable), $this->bindings)) {
50 2
            return $this->bindings[get_class($transformable)];
51
        }
52
53 48
        if ($transformable instanceof Model) {
54 43
            return $this->resolveFromModel($transformable);
55
        }
56
57 5
        return 'data';
58
    }
59
60
    /**
61
     * Resolve a resource key from the given model.
62
     *
63
     * @param  \Illuminate\Database\Eloquent\Model $model
64
     * @return string
65
     */
66 43
    public function resolveFromModel(Model $model)
67
    {
68 43
        if (method_exists($model, 'getResourceKey')) {
69 2
            return $model->getResourceKey();
70
        }
71
72 41
        return $model->getTable();
73
    }
74
75
    /**
76
     * Resolve a transformable item from the given data.
77
     *
78
     * @param  mixed $data
79
     * @return mixed
80
     */
81 50
    protected function resolveTransformableItem($data)
82
    {
83 50
        if (is_array($data) || $data instanceof Traversable) {
84 13
            foreach ($data as $item) {
85 12
                return $item;
86
            }
87
        }
88
89 38
        return $data;
90
    }
91
}