Completed
Pull Request — develop (#455)
by Lucas
37:54 queued 31:44
created

BasicLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 159
ccs 0
cts 69
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A load() 0 14 3
A loadService() 0 10 3
B loadReadOnlyRoutes() 0 37 1
A loadWriteRoutes() 0 18 1
A supports() 0 4 1
1
<?php
2
/**
3
 * Load routes for all rest services
4
 */
5
6
namespace Graviton\RestBundle\Routing\Loader;
7
8
use Symfony\Component\Config\Loader\Loader;
9
use Symfony\Component\Routing\RouteCollection;
10
11
/**
12
 * Load routes for all rest services
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class BasicLoader extends Loader
19
{
20
    /**
21
     * @var boolean
22
     */
23
    private $loaded = false;
24
25
    /**
26
     * @var \Symfony\Component\Routing\RouteCollection
27
     */
28
    private $routes;
29
30
    /**
31
     * @var array
32
     */
33
    private $services;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param \Symfony\Component\Routing\RouteCollection $routes   route collection
39
     * @param array                                      $services configs for all services tagged as graviton.rest
40
     */
41
    public function __construct($routes, $services)
42
    {
43
        $this->routes = $routes;
44
        $this->services = $services;
45
    }
46
47
    /**
48
     * Load routes for all services tagged with graviton.rest
49
     *
50
     * @param string $resource unused
51
     * @param string $type     unused
0 ignored issues
show
Documentation introduced by
Should the type for parameter $type not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
52
     *
53
     * @return RouteCollection
54
     */
55
    public function load($resource, $type = null)
56
    {
57
        if (true === $this->loaded) {
58
            throw new \RuntimeException('Do not add the "graviton.rest.routing.loader" loader twice');
59
        }
60
61
        foreach ($this->services as $service => $serviceConfig) {
62
            $this->loadService($service, $serviceConfig);
63
        }
64
65
        $this->loaded = true;
66
67
        return $this->routes;
68
    }
69
70
    /**
71
     * load routes for a single service
72
     *
73
     * @param string $service       service name
74
     * @param array  $serviceConfig service configuration
75
     *
76
     * @return void
77
     */
78
    private function loadService($service, $serviceConfig)
79
    {
80
        list($app, $bundle, , $entity) = explode('.', $service);
81
        $resource = implode('.', array($app, $bundle, 'rest', $entity));
82
83
        $this->loadReadOnlyRoutes($service, $resource, $serviceConfig);
84
        if (!($serviceConfig[0] && array_key_exists('read-only', $serviceConfig[0]))) {
85
            $this->loadWriteRoutes($service, $resource, $serviceConfig);
86
        }
87
    }
88
89
    /**
90
     * generate ro routes
91
     *
92
     * @param string $service       service name
93
     * @param string $resource      resource name
94
     * @param array  $serviceConfig service configuration
95
     *
96
     * @return void
97
     */
98
    public function loadReadOnlyRoutes($service, $resource, $serviceConfig)
99
    {
100
101
        $actionOptions = ActionUtils::getRouteOptions($service, $serviceConfig);
102
        $this->routes->add($resource . '.options', $actionOptions);
103
104
        $actionOptionsNoSlash = ActionUtils::getRouteOptions($service, $serviceConfig);
105
        $actionOptionsNoSlash->setPath(substr($actionOptionsNoSlash->getPath(), 0, -1));
106
        $this->routes->add($resource . '.optionsNoSlash', $actionOptionsNoSlash);
107
108
        $actionHead = ActionUtils::getRouteHead($service, $serviceConfig);
109
        $this->routes->add($resource . '.head', $actionHead);
110
111
        $actionHead = ActionUtils::getRouteHead($service, $serviceConfig, array(), true);
112
        $this->routes->add($resource . '.idHead', $actionHead);
113
114
        $actionGet = ActionUtils::getRouteGet($service, $serviceConfig);
115
        $this->routes->add($resource . '.get', $actionGet);
116
117
        $actionAll = ActionUtils::getRouteAll($service, $serviceConfig);
118
        $this->routes->add($resource . '.all', $actionAll);
119
120
        $actionOptions = ActionUtils::getCanonicalSchemaRoute($service, $serviceConfig, 'collection');
121
        $this->routes->add($resource . '.canonicalSchema', $actionOptions);
122
123
        $actionOptions = ActionUtils::getCanonicalSchemaRoute($service, $serviceConfig, 'collection', true);
124
        $this->routes->add($resource . '.canonicalSchemaOptions', $actionOptions);
125
126
        $actionOptions = ActionUtils::getCanonicalSchemaRoute($service, $serviceConfig);
127
        $this->routes->add($resource . '.canonicalIdSchema', $actionOptions);
128
129
        $actionOptions = ActionUtils::getCanonicalSchemaRoute($service, $serviceConfig, 'item', true);
130
        $this->routes->add($resource . '.canonicalIdSchemaOptions', $actionOptions);
131
132
        $actionOptions = ActionUtils::getRouteOptions($service, $serviceConfig, array(), true);
133
        $this->routes->add($resource . '.idOptions', $actionOptions);
134
    }
135
136
    /**
137
     * generate write routes
138
     *
139
     * @param string $service       service name
140
     * @param string $resource      resource name
141
     * @param array  $serviceConfig service configuration
142
     *
143
     * @return void
144
     */
145
    public function loadWriteRoutes($service, $resource, $serviceConfig)
146
    {
147
        $actionPost = ActionUtils::getRoutePost($service, $serviceConfig);
148
        $this->routes->add($resource . '.post', $actionPost);
149
150
        $actionPut = ActionUtils::getRoutePut($service, $serviceConfig);
151
        $this->routes->add($resource . '.put', $actionPut);
152
153
        $actionPostNoSlash = ActionUtils::getRoutePost($service, $serviceConfig);
154
        $actionPostNoSlash->setPath(substr($actionPostNoSlash->getPath(), 0, -1));
155
        $this->routes->add($resource . '.postNoSlash', $actionPostNoSlash);
156
157
        $actionPatch = ActionUtils::getRoutePatch($service, $serviceConfig);
158
        $this->routes->add($resource . '.patch', $actionPatch);
159
160
        $actionDelete = ActionUtils::getRouteDelete($service, $serviceConfig);
161
        $this->routes->add($resource . '.delete', $actionDelete);
162
    }
163
164
    /**
165
     * {@inheritDoc}
166
     *
167
     * @param string $resource unused
168
     * @param string $type     Type to match against
0 ignored issues
show
Documentation introduced by
Should the type for parameter $type not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
169
     *
170
     * @return Boolean
171
     */
172
    public function supports($resource, $type = null)
173
    {
174
        return 'graviton.rest.routing.loader' === $type;
175
    }
176
}
177