Completed
Push — 4.0 ( 0ca36c...7855d8 )
by Marco
12:29
created

AbstractService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 20
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 20
loc 20
rs 9.4285
cc 1
eloc 12
nc 1
nop 6
1
<?php namespace Comodojo\Dispatcher\Service;
2
3
use \Comodojo\Dispatcher\Components\Model as DispatcherClassModel;
4
use \Comodojo\Dispatcher\Components\Configuration;
5
use \Comodojo\Dispatcher\Request\Model as Request;
6
use \Comodojo\Dispatcher\Router\Collector as Router;
7
use \Comodojo\Dispatcher\Response\Model as Response;
8
use \Comodojo\Dispatcher\Extra\Model as Extra;
9
use \Monolog\Logger;
10
use \Exception;
11
12
/**
13
 * @package     Comodojo Dispatcher
14
 * @author      Marco Giovinazzi <[email protected]>
15
 * @author      Marco Castiello <[email protected]>
16
 * @license     GPL-3.0+
17
 *
18
 * LICENSE:
19
 *
20
 * This program is free software: you can redistribute it and/or modify
21
 * it under the terms of the GNU Affero General Public License as
22
 * published by the Free Software Foundation, either version 3 of the
23
 * License, or (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU Affero General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU Affero General Public License
31
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32
 */
33
34
abstract class AbstractService extends DispatcherClassModel {
35
36
    protected $request;
37
38
    protected $router;
39
40
    protected $response;
41
42
    protected $extra;
43
44 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
        Configuration $configuration,
46
        Logger $logger,
47
        Request $request,
48
        Router $router,
49
        Response $response,
50
        Extra $extra
51
    ) {
52
53
        parent::__construct($configuration, $logger);
54
55
        $this->request = $request;
56
57
        $this->router = $router;
58
59
        $this->response = $response;
60
61
        $this->extra = $extra;
62
63
    }
64
65
    final public function request() {
66
67
        return $this->request;
68
69
    }
70
71
    final public function router() {
72
73
        return $this->router;
74
75
    }
76
77
    final public function response() {
78
79
        return $this->response;
80
81
    }
82
83
    final public function extra() {
84
85
        return $this->extra;
86
87
    }
88
89
    /**
90
     * Get service-implemented HTTP methods
91
     *
92
     * @return  array   Service implemented methods, in uppercase
93
     * @throw Exception
94
     */
95
    public function getImplementedMethods() {
96
97
        $supported_methods = $this->configuration()->get('supported-methods');
98
99
100
        if ( method_exists($this, 'any') ) {
101
102
            return $supported_methods;
103
104
        }
105
106
        $implemented_methods = array();
107
108
        foreach ( $supported_methods as $method ) {
109
110
            if ( method_exists($this, strtolower($method)) ) array_push($implemented_methods,$method);
111
112
        }
113
114
        return $implemented_methods;
115
116
    }
117
118
    /**
119
     * Return the callable class method that reflect the requested one
120
     *
121
     */
122
    public function getMethod($method) {
123
124
        if ( method_exists($this, strtolower($method)) ) {
125
126
            return strtolower($method);
127
128
        } else if ( method_exists($this, strtolower('any')) ) {
129
130
            return 'any';
131
132
        } else {
133
134
            return "any";
135
136
        }
137
138
    }
139
140
}
141