Completed
Push — feature/other-validation ( c4ebdf...a3189f )
by Narcotic
152:45 queued 146:40
created

BasicLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 98.33%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 159
ccs 59
cts 60
cp 0.9833
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 2
    public function __construct($routes, $services)
42
    {
43 2
        $this->routes = $routes;
44 2
        $this->services = $services;
45 2
    }
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 2
    public function load($resource, $type = null)
56
    {
57 2
        if (true === $this->loaded) {
58
            throw new \RuntimeException('Do not add the "graviton.rest.routing.loader" loader twice');
59
        }
60
61 2
        foreach ($this->services as $service => $serviceConfig) {
62 2
            $this->loadService($service, $serviceConfig);
63 1
        }
64
65 2
        $this->loaded = true;
66
67 2
        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 2
    private function loadService($service, $serviceConfig)
79
    {
80 2
        list($app, $bundle, , $entity) = explode('.', $service);
81 2
        $resource = implode('.', array($app, $bundle, 'rest', $entity));
82
83 2
        $this->loadReadOnlyRoutes($service, $resource, $serviceConfig);
84 2
        if (!($serviceConfig[0] && array_key_exists('read-only', $serviceConfig[0]))) {
85 2
            $this->loadWriteRoutes($service, $resource, $serviceConfig);
86 1
        }
87 2
    }
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 2
    public function loadReadOnlyRoutes($service, $resource, $serviceConfig)
99
    {
100
101 2
        $actionOptions = ActionUtils::getRouteOptions($service, $serviceConfig);
102 2
        $this->routes->add($resource . '.options', $actionOptions);
103
104 2
        $actionOptionsNoSlash = ActionUtils::getRouteOptions($service, $serviceConfig);
105 2
        $actionOptionsNoSlash->setPath(substr($actionOptionsNoSlash->getPath(), 0, -1));
106 2
        $this->routes->add($resource . '.optionsNoSlash', $actionOptionsNoSlash);
107
108 2
        $actionHead = ActionUtils::getRouteHead($service, $serviceConfig);
109 2
        $this->routes->add($resource . '.head', $actionHead);
110
111 2
        $actionHead = ActionUtils::getRouteHead($service, $serviceConfig, array(), true);
112 2
        $this->routes->add($resource . '.idHead', $actionHead);
113
114 2
        $actionGet = ActionUtils::getRouteGet($service, $serviceConfig);
115 2
        $this->routes->add($resource . '.get', $actionGet);
116
117 2
        $actionAll = ActionUtils::getRouteAll($service, $serviceConfig);
118 2
        $this->routes->add($resource . '.all', $actionAll);
119
120 2
        $actionOptions = ActionUtils::getCanonicalSchemaRoute($service, $serviceConfig, 'collection');
121 2
        $this->routes->add($resource . '.canonicalSchema', $actionOptions);
122
123 2
        $actionOptions = ActionUtils::getCanonicalSchemaRoute($service, $serviceConfig, 'collection', true);
124 2
        $this->routes->add($resource . '.canonicalSchemaOptions', $actionOptions);
125
126 2
        $actionOptions = ActionUtils::getCanonicalSchemaRoute($service, $serviceConfig);
127 2
        $this->routes->add($resource . '.canonicalIdSchema', $actionOptions);
128
129 2
        $actionOptions = ActionUtils::getCanonicalSchemaRoute($service, $serviceConfig, 'item', true);
130 2
        $this->routes->add($resource . '.canonicalIdSchemaOptions', $actionOptions);
131
132 2
        $actionOptions = ActionUtils::getRouteOptions($service, $serviceConfig, array(), true);
133 2
        $this->routes->add($resource . '.idOptions', $actionOptions);
134 2
    }
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 2
    public function loadWriteRoutes($service, $resource, $serviceConfig)
146
    {
147 2
        $actionPost = ActionUtils::getRoutePost($service, $serviceConfig);
148 2
        $this->routes->add($resource . '.post', $actionPost);
149
150 2
        $actionPut = ActionUtils::getRoutePut($service, $serviceConfig);
151 2
        $this->routes->add($resource . '.put', $actionPut);
152
153 2
        $actionPostNoSlash = ActionUtils::getRoutePost($service, $serviceConfig);
154 2
        $actionPostNoSlash->setPath(substr($actionPostNoSlash->getPath(), 0, -1));
155 2
        $this->routes->add($resource . '.postNoSlash', $actionPostNoSlash);
156
157 2
        $actionPatch = ActionUtils::getRoutePatch($service, $serviceConfig);
158 2
        $this->routes->add($resource . '.patch', $actionPatch);
159
160 2
        $actionDelete = ActionUtils::getRouteDelete($service, $serviceConfig);
161 2
        $this->routes->add($resource . '.delete', $actionDelete);
162 2
    }
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 2
    public function supports($resource, $type = null)
173
    {
174 2
        return 'graviton.rest.routing.loader' === $type;
175
    }
176
}
177