Passed
Push — 1.0.0-alpha ( 268be3...1f7cc7 )
by Alex
02:54
created

ResourceRegistrar::addRelationshipUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 5
1
<?php
2
3
namespace Huntie\JsonApi\Routing;
4
5
class ResourceRegistrar extends \Illuminate\Routing\ResourceRegistrar
6
{
7
    /**
8
     * The default actions for a resourceful controller.
9
     *
10
     * @var array
11
     */
12
    protected $resourceDefaults = ['index', 'store', 'show', 'update', 'destroy'];
13
14
    /**
15
     * The default actions for each named resource relationship.
16
     *
17
     * @var array
18
     */
19
    protected $relationshipDefaults = ['show'];
20
21
    /**
22
     * Create a new resource registrar instance.
23
     *
24
     * @param \Huntie\JsonApi\Routing\Router $router
25
     */
26
    public function __construct(Router $router)
27
    {
28
        $this->router = $router;
29
    }
30
31
    /**
32
     * Route a resource to a controller.
33
     *
34
     * @param string $name
35
     * @param string $controller
36
     * @param array  $options
37
     */
38
    public function register($name, $controller, array $options = [])
39
    {
40
        parent::register($name, $controller, $options);
41
42
        $this->registerRelationships($name, $controller, $options);
43
    }
44
45
    /**
46
     * Route any resource relationships to a controller.
47
     *
48
     * @param string $name
49
     * @param string $controller
50
     * @param array  $options
51
     */
52
    protected function registerRelationships($name, $controller, array $options)
53
    {
54
        $base = $this->getResourceWildcard(last(explode('.', $name)));
55
56
        // Map non-associative members as keys, with default relationship actions
57
        $relationships = collect(array_get($options, 'relationships', []))
58
            ->mapWithKeys(function ($methods, $relationship) {
59
                return is_numeric($relationship)
60
                    ? [$methods => $this->relationshipDefaults]
61
                    : [$relationship => (array) $methods];
62
            });
63
64
        foreach (['show', 'update'] as $action) {
65
            $matched = $relationships->filter(function ($methods) use ($action) {
66
                return in_array($action, $methods);
67
            })->keys()->toArray();
68
69
            if (!empty($matched)) {
70
                $this->{'addRelationship' . ucfirst($action)}($name, $base, $matched, $controller, $options);
71
            }
72
        }
73
    }
74
75
    /**
76
     * Add a relationship show method to match named relationships on the resource.
77
     *
78
     * @param string $name
79
     * @param string $base
80
     * @param array  $relationships
81
     * @param string $controller
82
     * @param array  $options
83
     *
84
     * @return \Illuminate\Routing\Route
85
     */
86 View Code Duplication
    protected function addRelationshipShow($name, $base, array $relationships, $controller, array $options)
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...
87
    {
88
        $uri = $this->getRelationshipUri($name, $base);
89
        $action = $this->getResourceAction($name, $controller, 'showRelationship', $options);
90
91
        return $this->router->get($uri, $action)
92
            ->where('relationship', '(' . implode(')|(', $relationships) . ')');
93
    }
94
95
    /**
96
     * Add a relationship update method to match named relationships on the resource.
97
     *
98
     * @param string $name
99
     * @param string $base
100
     * @param array  $relationships
101
     * @param string $controller
102
     * @param array  $options
103
     *
104
     * @return \Illuminate\Routing\Route
105
     */
106 View Code Duplication
    protected function addRelationshipUpdate($name, $base, array $relationships, $controller, array $options)
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...
107
    {
108
        $uri = $this->getRelationshipUri($name, $base);
109
        $action = $this->getResourceAction($name, $controller, 'updateRelationship', $options);
110
111
        return $this->router->match(['PUT', 'PATCH'], $uri, $action)
112
            ->where('relationship', '(' . implode(')|(', $relationships) . ')');
113
    }
114
115
    /**
116
     * Get the URI for resource relationships.
117
     *
118
     * @param string $name
119
     * @param string $base
120
     *
121
     * @return string
122
     */
123
    public function getRelationshipUri($name, $base)
124
    {
125
        return sprintf('%s/{%s}/relationships/{relationship}', $this->getResourceUri($name), $base);
126
    }
127
128
    /**
129
     * Format a resource parameter for usage.
130
     *
131
     * @param string $value
132
     *
133
     * @return string
134
     */
135
    public function getResourceWildcard($value)
136
    {
137
        if (isset($this->parameters[$value])) {
138
            return $this->parameters[$value];
139
        } else if (isset(static::$parameterMap[$value])) {
140
            return static::$parameterMap[$value];
141
        }
142
143
        if ($this->parameters === 'singular' || static::$singularParameters) {
144
            $value = str_singular($value);
145
        }
146
147
        return camel_case($value);
148
    }
149
}
150