|
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\Transport\TransportInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class ServiceFactory |
|
14
|
|
|
* |
|
15
|
|
|
* @package Yandex\Direct |
|
16
|
|
|
*/ |
|
17
|
|
|
class ServiceFactory |
|
18
|
|
|
{ |
|
19
|
|
|
const E_INVALID_NAME = 1; |
|
20
|
|
|
const E_INVALID_OPTION = 2; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Default Service options |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $defaultOptions = [ |
|
28
|
|
|
'transport' => 'Yandex\\Direct\\Transport\\JsonTransport', |
|
29
|
|
|
'transportOptions' => [], |
|
30
|
|
|
'credentials' => null |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Set default service options |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $options |
|
37
|
|
|
*/ |
|
38
|
48 |
|
public function setDefaultOptions(array $options) |
|
39
|
|
|
{ |
|
40
|
48 |
|
$this->defaultOptions = array_merge($this->defaultOptions, $options); |
|
41
|
48 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $serviceName |
|
45
|
|
|
* @param array $options |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
* @throws InvalidArgumentException |
|
48
|
|
|
*/ |
|
49
|
48 |
|
public function createService($serviceName, array $options = []) |
|
50
|
|
|
{ |
|
51
|
48 |
|
$className = $this->getServiceNamespace() . '\\' . ucfirst($serviceName); |
|
52
|
|
|
|
|
53
|
|
|
// Override service base options |
|
54
|
48 |
|
$options = array_merge($this->defaultOptions, [ |
|
55
|
|
|
'name' => $serviceName |
|
56
|
48 |
|
], $options); |
|
57
|
|
|
|
|
58
|
|
|
// Create transport instance if got classname |
|
59
|
48 |
|
if (is_string($options['transport'])) { |
|
60
|
32 |
|
$options['transport'] = $this->buildTransport( |
|
61
|
32 |
|
$options['transport'], |
|
62
|
32 |
|
$options['transportOptions'] |
|
63
|
32 |
|
); |
|
64
|
32 |
|
} |
|
65
|
|
|
|
|
66
|
48 |
|
unset($options['transportOptions']); |
|
67
|
|
|
|
|
68
|
48 |
|
if (class_exists($className)) { |
|
69
|
45 |
|
$instance = new $className($serviceName); |
|
70
|
45 |
|
if (!$instance instanceof Service) { |
|
71
|
|
|
throw new InvalidArgumentException( |
|
72
|
|
|
"Service class `{$className}` is not instance of `Yandex\\Direct\\Service`.", |
|
73
|
|
|
self::E_INVALID_NAME |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
45 |
|
$instance->setOptions($options); |
|
77
|
45 |
|
return $instance; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
throw new InvalidArgumentException("Service class `{$className}` is not found.", self::E_INVALID_NAME); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $transportClass |
|
85
|
|
|
* @param array $transportOptions |
|
86
|
|
|
* @return TransportInterface |
|
87
|
|
|
* @throws InvalidArgumentException |
|
88
|
|
|
*/ |
|
89
|
32 |
|
protected function buildTransport($transportClass, array $transportOptions = []) |
|
90
|
|
|
{ |
|
91
|
32 |
|
if (!class_exists($transportClass)) { |
|
92
|
|
|
throw new InvalidArgumentException("Transport class `{$transportClass}` is not found.", self::E_INVALID_OPTION); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
32 |
|
$transport = new $transportClass; |
|
96
|
|
|
|
|
97
|
32 |
|
if (!$transport instanceof TransportInterface) { |
|
98
|
|
|
throw new InvalidArgumentException( |
|
99
|
|
|
"Transport class `{$transportClass}` is not instance of |
|
100
|
|
|
`Yandex\\Direct\\Transport\\TransportInterface`", |
|
101
|
|
|
self::E_INVALID_OPTION |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
32 |
|
$transport->setOptions($transportOptions); |
|
106
|
|
|
|
|
107
|
32 |
|
return $transport; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
48 |
|
protected function getServiceNamespace() |
|
114
|
|
|
{ |
|
115
|
48 |
|
return __NAMESPACE__ . '\\' . 'Service'; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|