Completed
Pull Request — master (#40)
by
unknown
10:59
created

CrudService::table()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2016 - 2017, 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 - 2017, 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 Cake\Utility\Inflector;
15
16
/**
17
 * Class CrudService
18
 *
19
 * @package CakeDC\Api\Service
20
 */
21
abstract class CrudService extends Service
22
{
23
24
    /**
25
     * Actions classes map.
26
     *
27
     * @var array
28
     */
29
    protected $_actionsClassMap = [
30
        'describe' => '\CakeDC\Api\Service\Action\CrudDescribeAction',
31
        'index' => '\CakeDC\Api\Service\Action\CrudIndexAction',
32
        'view' => '\CakeDC\Api\Service\Action\CrudViewAction',
33
        'add' => '\CakeDC\Api\Service\Action\CrudAddAction',
34
        'edit' => '\CakeDC\Api\Service\Action\CrudEditAction',
35
        'delete' => '\CakeDC\Api\Service\Action\CrudDeleteAction',
36
    ];
37
38
    /**
39
     * Table name.
40
     *
41
     * @var string
42
     */
43
    protected $_table = null;
44
45
    /**
46
     * Id param name.
47
     *
48
     * @var string
49
     */
50
    protected $_idName = 'id';
51
52
    /**
53
     * CrudService constructor.
54
     *
55
     * @param array $config Service configuration.
56
     */
57 130
    public function __construct(array $config = [])
58
    {
59 130
        parent::__construct($config);
60 130
        if (isset($config['table'])) {
61
            $this->setTable($config['table']);
62
        } else {
63 130
            $this->setTable(Inflector::camelize($this->getName()));
0 ignored issues
show
Bug introduced by
It seems like \Cake\Utility\Inflector:...elize($this->getName()) targeting Cake\Utility\Inflector::camelize() can also be of type boolean; however, CakeDC\Api\Service\CrudService::setTable() 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...
64
        }
65 130
    }
66
67
    /**
68
     * Gets a Table name.
69
     *
70
     * @return string
71
     */
72 113
    public function getTable()
73
    {
74 113
        return $this->_table;
75
    }
76
77
    /**
78
     * Sets the table instance.
79
     *
80
     * @param string $table A Table name.
81
     * @return $this
82
     */
83 130
    public function setTable($table)
84
    {
85 130
        $this->_table = $table;
86
87 130
        return $this;
88
    }
89
90
    /**
91
     * Api method for table.
92
     *
93
     * @param string $table A Table name.
94
     * @deprecated 3.4.0 Use setTable()/getTable() instead.
95
     * @return string
96
     */
97
    public function table($table = null)
98
    {
99
        if ($table !== null) {
100
            return $this->setTable($table);
101
        }
102
103
        return $this->getTable();
104
    }
105
106
    /**
107
     * Action constructor options.
108
     *
109
     * @param array $route Activated route.
110
     * @return array
111
     */
112 55
    protected function _actionOptions($route)
113
    {
114 55
        $id = null;
115 55
        if (isset($route[$this->_idName])) {
116 17
            $id = $route[$this->_idName];
117 17
        }
118
119 55
        return parent::_actionOptions($route) + [
120 55
            'id' => $id,
121 55
            'idName' => $this->_idName,
122 55
        ];
123
    }
124
}
125