FallbackService::initialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 2
1
<?php
2
/**
3
 * Copyright 2016 - 2018, 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 - 2018, 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 130
    public function initialize()
40
    {
41 130
        parent::initialize();
42 130
        if (empty($this->_table)) {
43 130
            $this->_table = Inflector::pluralize(Inflector::camelize($this->getName()));
44 130
        }
45 130
    }
46
47
    /**
48
     * Initialize service level routes
49
     *
50
     * @return void
51
     */
52 56
    public function loadRoutes()
53
    {
54 56
        $table = TableRegistry::getTableLocator()->get($this->_table);
55
56 56
        $defaultOptions = $this->routerDefaultOptions();
57
        ApiRouter::scope('/', $defaultOptions, function (RouteBuilder $routes) use ($table, $defaultOptions) {
58 56
            $routes->setExtensions($this->_routeExtensions);
59 56
            $options = $defaultOptions;
60 56
            $options['map'] = array_merge($options['map'], [
61 56
                'describe' => ['action' => 'describe', 'method' => 'OPTIONS', 'path' => ''],
62 56
                'describeId' => ['action' => 'describe', 'method' => 'OPTIONS', 'path' => ':id'],
63 56
            ]);
64 56
            $routes->resources($this->getName(), $options, function ($routes) use ($table) {
65 56
                if (is_array($this->_routeExtensions)) {
66 56
                    $routes->setExtensions($this->_routeExtensions);
67
68 56
                    $keys = ['HasMany'/*, 'HasOne'*/];
69
70 56
                    foreach ($keys as $type) {
71 56
                        foreach ($table->associations()->getByType($type) as $assoc) {
72 21
                            $target = $assoc->getTarget();
73 21
                            $alias = $target->getAlias();
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 56
                        }
90 56
                    }
91 56
                }
92 56
            });
93 56
        });
94 56
    }
95
}
96