AbstractService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 19
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 10
cc 1
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php namespace Comodojo\Dispatcher\Service;
2
3
use \Comodojo\Dispatcher\Components\AbstractModel;
4
use \Comodojo\Dispatcher\Request\Model as Request;
5
use \Comodojo\Dispatcher\Router\Model as Router;
6
use \Comodojo\Dispatcher\Response\Model as Response;
7
use \Comodojo\Dispatcher\Extra\Model as Extra;
8
use \Comodojo\Dispatcher\Traits\CacheTrait;
9
use \Comodojo\Dispatcher\Traits\RequestTrait;
10
use \Comodojo\Dispatcher\Traits\ResponseTrait;
11
use \Comodojo\Dispatcher\Traits\RouterTrait;
12
use \Comodojo\Dispatcher\Traits\ExtraTrait;
13
use \Comodojo\Foundation\Events\EventsTrait;
14
use \Comodojo\SimpleCache\Manager as CacheManager;
15
use \Comodojo\Foundation\Base\Configuration;
16
use \Comodojo\Foundation\Events\Manager as EventsManager;
17
use \Psr\Log\LoggerInterface;
18
use \Exception;
19
20
/**
21
 * @package     Comodojo Dispatcher
22
 * @author      Marco Giovinazzi <[email protected]>
23
 * @author      Marco Castiello <[email protected]>
24
 * @license     MIT
25
 *
26
 * LICENSE:
27
 *
28
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34
 * THE SOFTWARE.
35
 */
36
37
abstract class AbstractService extends AbstractModel {
38
39
    use CacheTrait;
40
    use EventsTrait;
41
    use RequestTrait;
42
    use RouterTrait;
43
    use ResponseTrait;
44
    use ExtraTrait;
45
46
    protected static $supported_methods = ['GET','PUT','POST','DELETE','OPTIONS','HEAD','TRACE','CONNECT','PURGE'];
47
48 1
    public function __construct(
49
        Configuration $configuration,
50
        LoggerInterface $logger,
51
        CacheManager $cache,
52
        EventsManager $events,
53
        Request $request,
54
        Router $router,
55
        Response $response,
56
        Extra $extra
57
    ) {
58
59 1
        parent::__construct($configuration, $logger);
60
61 1
        $this->setCache($cache);
62 1
        $this->setEvents($events);
63 1
        $this->setRequest($request);
64 1
        $this->setRouter($router);
65 1
        $this->setResponse($response);
66 1
        $this->setExtra($extra);
67
68 1
    }
69
70
    /**
71
     * Get service-implemented HTTP methods
72
     *
73
     * @return  array   Service implemented methods, in uppercase
74
     */
75 2
    public function getImplementedMethods() {
76
77 2
        $supported_methods = $this->getConfiguration()->get('supported-http-methods');
78
79 2
        if ( is_null($supported_methods) ) $supported_methods = self::$supported_methods;
80
81 2
        if ( method_exists($this, 'any') ) {
82
83
            return $supported_methods;
84
85
        }
86
87 2
        $implemented_methods = [];
88
89 2
        foreach ( $supported_methods as $method ) {
90
91 2
            if ( method_exists($this, strtolower($method)) ) array_push($implemented_methods, $method);
92
93 2
        }
94
95 2
        return $implemented_methods;
96
97
    }
98
99
    /**
100
     * Return the callable class method that reflect the requested one
101
     *
102
     */
103 2
    public function getMethod($method) {
104
105 2
        $method = strtolower($method);
106
107 2
        if ( method_exists($this, $method) ) {
108
109 2
            return $method;
110
111
        } else if ( method_exists($this, 'any') ) {
112
113
            return 'any';
114
115
        } else {
116
117
            return null;
118
119
        }
120
121
    }
122
123
}
124