1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Dmitry Gladyshev <[email protected]> |
4
|
|
|
* @date 23/08/2016 13:42 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Yandex\Direct; |
8
|
|
|
|
9
|
|
|
use Yandex\Direct\Exception\InvalidArgumentException; |
10
|
|
|
use Yandex\Direct\Exception\ServiceNotFoundException; |
11
|
|
|
use Yandex\Direct\Transport\Json\Transport; |
12
|
|
|
use Yandex\Direct\Transport\TransportInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ServiceFactory |
16
|
|
|
* |
17
|
|
|
* @package Yandex\Direct |
18
|
|
|
*/ |
19
|
|
|
class ServiceFactory implements ServiceFactoryInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $serviceNamespace = __NAMESPACE__ . '\\' . 'Service'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
* @throws InvalidArgumentException |
29
|
|
|
*/ |
30
|
78 |
|
public function createService($serviceName, array $serviceOptions = []) |
31
|
|
|
{ |
32
|
78 |
|
if (empty($serviceOptions[self::OPTION_TRANSPORT])) { |
33
|
|
|
// Use default transport |
34
|
52 |
|
$serviceOptions[self::OPTION_TRANSPORT] = new Transport; |
35
|
52 |
|
} |
36
|
|
|
|
37
|
78 |
|
if (empty($serviceOptions[self::OPTION_CREDENTIALS])) { |
38
|
|
|
throw new InvalidArgumentException('Credentials is required.'); |
39
|
|
|
} |
40
|
|
|
|
41
|
78 |
|
$className = $this->getServiceNamespace() . '\\' . ucfirst($serviceName); |
42
|
|
|
|
43
|
78 |
|
if (!class_exists($className)) { |
44
|
3 |
|
throw new ServiceNotFoundException("Service class `{$className}` is not found."); |
45
|
|
|
} |
46
|
|
|
|
47
|
75 |
|
$instance = new $className; |
48
|
|
|
|
49
|
75 |
|
if (!$instance instanceof Service) { |
50
|
|
|
throw new ServiceNotFoundException( |
51
|
|
|
"Service class `{$className}` is not instance of `" . Service::class . "`." |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
75 |
|
$serviceOptions['name'] = ucfirst($serviceName); |
56
|
75 |
|
$instance->setOptions($serviceOptions); |
57
|
|
|
|
58
|
75 |
|
return $instance; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $namespace |
63
|
|
|
*/ |
64
|
|
|
public function setServiceNamespace($namespace) |
65
|
|
|
{ |
66
|
|
|
$this->serviceNamespace = $namespace; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
78 |
|
protected function getServiceNamespace() |
73
|
|
|
{ |
74
|
78 |
|
return $this->serviceNamespace; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|