Completed
Push — master ( 62946e...cfb4fd )
by
unknown
07:23
created

FallbackService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 72
ccs 38
cts 40
cp 0.95
rs 10
wmc 7
lcom 1
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 7 2
B loadRoutes() 0 43 5
1
<?php
2
/**
3
 * Copyright 2016, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Service;
13
14
use CakeDC\Api\Routing\ApiRouter;
15
use Cake\ORM\TableRegistry;
16
use Cake\Routing\RouteBuilder;
17
use Cake\Utility\Inflector;
18
19
/**
20
 * Class FallbackService
21
 *
22
 * @package CakeDC\Api\Service
23
 */
24
class FallbackService extends NestedCrudService
25
{
26
27
    /**
28
     * Table name.
29
     *
30
     * @var string
31
     */
32
    protected $_table = null;
33
34
    /**
35
     * Initialize method
36
     *
37
     * @return void
38
     */
39 108
    public function initialize()
40
    {
41 108
        parent::initialize();
42 108
        if (empty($this->_table)) {
43 108
            $this->_table = Inflector::pluralize(Inflector::camelize($this->name()));
0 ignored issues
show
Bug introduced by
It seems like \Cake\Utility\Inflector::camelize($this->name()) targeting Cake\Utility\Inflector::camelize() can also be of type boolean; however, Cake\Utility\Inflector::pluralize() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
44 108
        }
45 108
    }
46
47
    /**
48
     * Initialize service level routes
49
     *
50
     * @return void
51
     */
52 55
    public function loadRoutes()
53
    {
54 55
        $table = TableRegistry::get($this->_table);
55
56 55
        $defaultOptions = $this->routerDefaultOptions();
57
        ApiRouter::scope('/', $defaultOptions, function (RouteBuilder $routes) use ($table, $defaultOptions) {
58 55
            $routes->extensions($this->_routeExtensions);
59 55
            $options = $defaultOptions;
60 55
            $options['map'] = array_merge($options['map'], [
61 55
                'describe' => ['action' => 'describe', 'method' => 'OPTIONS', 'path' => ''],
62 55
                'describeId' => ['action' => 'describe', 'method' => 'OPTIONS', 'path' => ':id'],
63 55
            ]);
64 55
            $routes->resources($this->name(), $options, function ($routes) use ($table) {
65 55
                if (is_array($this->_routeExtensions)) {
66 55
                    $routes->extensions($this->_routeExtensions);
67
68 55
                    $keys = ['HasMany'/*, 'HasOne'*/];
69
70 55
                    foreach ($keys as $type) {
71 55
                        foreach ($table->associations()->type($type) as $assoc) {
72 21
                            $target = $assoc->target();
73 21
                            $alias = $target->alias();
74
75 21
                            $targetClass = get_class($target);
76 21
                            list(, $className) = namespaceSplit($targetClass);
77 21
                            $className = preg_replace('/(.*)Table$/', '\1', $className);
78 21
                            if ($className === '') {
79
                                $className = $alias;
80
                            }
81 21
                            $this->_innerServices[] = Inflector::underscore($className);
82
                            $options = [
83
                                'map' => [
84 21
                                    'describe' => ['action' => 'describe', 'method' => 'OPTIONS', 'path' => ''],
85 21
                                    'describeId' => ['action' => 'describe', 'method' => 'OPTIONS', 'path' => ':id'],
86
                                ]
87 21
                            ];
88 21
                            $routes->resources($className, $options);
89 55
                        }
90 55
                    }
91 55
                }
92 55
            });
93 55
        });
94 55
    }
95
}
96