1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saltwater\Capability\Provider; |
4
|
|
|
|
5
|
|
|
use Saltwater\Utils as U; |
6
|
|
|
use Saltwater\Salt\Provider; |
7
|
|
|
use Saltwater\Salt\Context as SwContext; |
8
|
|
|
use Saltwater\Salt\Service as SwService; |
9
|
|
|
|
10
|
|
View Code Duplication |
class Service extends Provider |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string $name |
14
|
|
|
* @param SwContext $context |
15
|
|
|
* |
16
|
|
|
* @return SwService |
17
|
|
|
*/ |
18
|
|
|
public function get($name, $context) |
19
|
|
|
{ |
20
|
|
|
$class = $this->getServiceClass($context, $name); |
21
|
|
|
|
22
|
|
|
// TODO: RB Fallback is rather dirty, try parent context or fail out |
23
|
|
|
return new $class($context); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param SwContext $context |
28
|
|
|
* @param string $service |
29
|
|
|
*/ |
30
|
|
|
private function getServiceClass($context, $service) |
31
|
|
|
{ |
32
|
|
|
// See whether the current context namespace has this service |
33
|
|
|
$class = U::className($context->namespace, 'service', $service); |
34
|
|
|
|
35
|
|
|
if (class_exists($class)) { |
36
|
|
|
return $class; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this->getServiceClassFallback($context, $service); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function getServiceClassFallback($context, $service) |
43
|
|
|
{ |
44
|
|
|
// Check whether we have a RestService in the context namespace |
45
|
|
|
$class = U::className($context->namespace, 'service', 'rest'); |
46
|
|
|
|
47
|
|
|
if (class_exists($class)) { |
48
|
|
|
return $class; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Next up, try for a root service |
52
|
|
|
$class = U::className('saltwater', 'root', 'service', $service); |
53
|
|
|
|
54
|
|
|
if (class_exists($class)) { |
55
|
|
|
return $class; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Fall back to the RedBean RestService |
59
|
|
|
// TODO: Needs to be decoupled |
60
|
|
|
return 'Saltwater\RedBean\Service\Rest'; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
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.