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\Dispatcher\Traits\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 GPL-3.0+ |
25
|
|
|
* |
26
|
|
|
* LICENSE: |
27
|
|
|
* |
28
|
|
|
* This program is free software: you can redistribute it and/or modify |
29
|
|
|
* it under the terms of the GNU Affero General Public License as |
30
|
|
|
* published by the Free Software Foundation, either version 3 of the |
31
|
|
|
* License, or (at your option) any later version. |
32
|
|
|
* |
33
|
|
|
* This program is distributed in the hope that it will be useful, |
34
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
35
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
36
|
|
|
* GNU Affero General Public License for more details. |
37
|
|
|
* |
38
|
|
|
* You should have received a copy of the GNU Affero General Public License |
39
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
abstract class AbstractService extends AbstractModel { |
43
|
|
|
|
44
|
|
|
use CacheTrait; |
45
|
|
|
use EventsTrait; |
46
|
|
|
use RequestTrait; |
47
|
|
|
use RouterTrait; |
48
|
|
|
use ResponseTrait; |
49
|
|
|
use ExtraTrait; |
50
|
|
|
|
51
|
|
|
const SUPPORTED_METHODS = ['GET','PUT','POST','DELETE','OPTIONS','HEAD','TRACE','CONNECT','PURGE']; |
52
|
|
|
|
53
|
1 |
View Code Duplication |
public function __construct( |
|
|
|
|
54
|
|
|
Configuration $configuration, |
55
|
|
|
LoggerInterface $logger, |
56
|
|
|
CacheManager $cache, |
57
|
|
|
EventsManager $events, |
58
|
|
|
Request $request, |
59
|
|
|
Router $router, |
60
|
|
|
Response $response, |
61
|
|
|
Extra $extra |
62
|
|
|
) { |
63
|
|
|
|
64
|
1 |
|
parent::__construct($configuration, $logger); |
65
|
|
|
|
66
|
1 |
|
$this->setCache($cache); |
67
|
1 |
|
$this->setEvents($events); |
68
|
1 |
|
$this->setRequest($request); |
69
|
1 |
|
$this->setRouter($router); |
70
|
1 |
|
$this->setResponse($response); |
71
|
1 |
|
$this->setExtra($extra); |
72
|
|
|
|
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get service-implemented HTTP methods |
77
|
|
|
* |
78
|
|
|
* @return array Service implemented methods, in uppercase |
79
|
|
|
*/ |
80
|
2 |
|
public function getImplementedMethods() { |
81
|
|
|
|
82
|
2 |
|
$supported_methods = $this->getConfiguration()->get('supported-http-methods'); |
83
|
|
|
|
84
|
2 |
|
if ( is_null($supported_methods) ) $supported_methods = self::SUPPORTED_METHODS; |
85
|
|
|
|
86
|
2 |
|
if ( method_exists($this, 'any') ) { |
87
|
|
|
|
88
|
|
|
return $supported_methods; |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
$implemented_methods = []; |
93
|
|
|
|
94
|
2 |
|
foreach ( $supported_methods as $method ) { |
95
|
|
|
|
96
|
2 |
|
if ( method_exists($this, strtolower($method)) ) array_push($implemented_methods, $method); |
97
|
|
|
|
98
|
2 |
|
} |
99
|
|
|
|
100
|
2 |
|
return $implemented_methods; |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return the callable class method that reflect the requested one |
106
|
|
|
* |
107
|
|
|
*/ |
108
|
2 |
|
public function getMethod($method) { |
109
|
|
|
|
110
|
2 |
|
$method = strtolower($method); |
111
|
|
|
|
112
|
2 |
|
if ( method_exists($this, $method) ) { |
113
|
|
|
|
114
|
2 |
|
return $method; |
115
|
|
|
|
116
|
|
|
} else if ( method_exists($this, 'any') ) { |
117
|
|
|
|
118
|
|
|
return 'any'; |
119
|
|
|
|
120
|
|
|
} else { |
121
|
|
|
|
122
|
|
|
return null; |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
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.