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\Model as Router; |
7
|
|
|
use \Comodojo\Dispatcher\Response\Model as Response; |
8
|
|
|
use \Comodojo\Dispatcher\Extra\Model as Extra; |
9
|
|
|
use \Psr\Log\LoggerInterface; |
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
|
1 |
View Code Duplication |
public function __construct( |
|
|
|
|
37
|
|
|
Configuration $configuration, |
38
|
1 |
|
LoggerInterface $logger, |
39
|
|
|
Request $request, |
40
|
|
|
Router $router, |
41
|
|
|
Response $response, |
42
|
|
|
Extra $extra |
43
|
|
|
) { |
44
|
|
|
|
45
|
1 |
|
parent::__construct($configuration, $logger); |
46
|
|
|
|
47
|
1 |
|
$this->request = $request; |
|
|
|
|
48
|
|
|
|
49
|
1 |
|
$this->router = $router; |
|
|
|
|
50
|
|
|
|
51
|
1 |
|
$this->response = $response; |
|
|
|
|
52
|
|
|
|
53
|
1 |
|
$this->extra = $extra; |
|
|
|
|
54
|
|
|
|
55
|
1 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get service-implemented HTTP methods |
59
|
|
|
* |
60
|
|
|
* @return array Service implemented methods, in uppercase |
61
|
|
|
* @throw Exception |
62
|
|
|
*/ |
63
|
2 |
|
public function getImplementedMethods() { |
64
|
|
|
|
65
|
2 |
|
$supported_methods = $this->configuration->get('supported-http-methods'); |
|
|
|
|
66
|
|
|
|
67
|
2 |
|
if ( is_null($supported_methods) ) $supported_methods = array('GET','PUT','POST','DELETE','OPTIONS','HEAD','TRACE','CONNECT','PURGE'); |
68
|
|
|
|
69
|
2 |
|
if ( method_exists($this, 'any') ) { |
70
|
|
|
|
71
|
|
|
return $supported_methods; |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
$implemented_methods = array(); |
76
|
|
|
|
77
|
2 |
|
foreach ( $supported_methods as $method ) { |
78
|
|
|
|
79
|
2 |
|
if ( method_exists($this, strtolower($method)) ) array_push($implemented_methods,$method); |
80
|
|
|
|
81
|
2 |
|
} |
82
|
|
|
|
83
|
2 |
|
return $implemented_methods; |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return the callable class method that reflect the requested one |
89
|
|
|
* |
90
|
|
|
*/ |
91
|
2 |
|
public function getMethod($method) { |
92
|
|
|
|
93
|
2 |
|
if ( method_exists($this, strtolower($method)) ) { |
94
|
|
|
|
95
|
2 |
|
return strtolower($method); |
96
|
|
|
|
97
|
|
|
} else if ( method_exists($this, strtolower('any')) ) { |
98
|
|
|
|
99
|
|
|
return 'any'; |
100
|
|
|
|
101
|
|
|
} else { |
102
|
|
|
|
103
|
|
|
return "any"; |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
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.