1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @project Magento Bridge for Symfony 2. |
5
|
|
|
* |
6
|
|
|
* @author Sébastien MALOT <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @url <https://github.com/smalot/magento-bundle> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Smalot\MagentoBundle\Adapter; |
15
|
|
|
|
16
|
|
|
use Smalot\Magento\RemoteAdapterInterface; |
17
|
|
|
use Smalot\MagentoBundle\Logger\LoggerInterface; |
18
|
|
|
use Smalot\MagentoBundle\MagentoException; |
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class FactoryAdapter |
25
|
|
|
* |
26
|
|
|
* @package Smalot\MagentoBundle\Adapter |
27
|
|
|
*/ |
28
|
|
|
class FactoryAdapter implements ContainerAwareInterface |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
use ContainerAwareTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $instances = array(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $defaultClass; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $settings; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface |
50
|
|
|
*/ |
51
|
|
|
protected $dispatcher; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \Smalot\MagentoBundle\Logger\LoggerInterface |
55
|
|
|
*/ |
56
|
|
|
protected $logger; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $defaultClass |
60
|
|
|
* @param EventDispatcherInterface $dispatcher |
61
|
|
|
* @param LoggerInterface $logger |
62
|
|
|
*/ |
63
|
|
|
public function __construct( |
64
|
|
|
$defaultClass = null, |
65
|
|
|
EventDispatcherInterface $dispatcher = null, |
66
|
|
|
LoggerInterface $logger = null |
67
|
|
|
) { |
68
|
|
|
$this->defaultClass = $defaultClass; |
69
|
|
|
$this->dispatcher = $dispatcher; |
70
|
|
|
$this->logger = $logger; |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $name |
76
|
|
|
* @param array $options |
77
|
|
|
* @param bool $autoLogin |
78
|
|
|
* |
79
|
|
|
* @return RemoteAdapterInterface |
80
|
|
|
* @throws MagentoException |
81
|
|
|
*/ |
82
|
|
|
public function getManager($name = null, $options = array(), $autoLogin = true) |
83
|
|
|
{ |
84
|
|
|
# Load Settings |
85
|
|
|
$this->settings = $this->container->getParameter('magento'); |
86
|
|
|
|
87
|
|
|
// Get default connection if necessary. |
88
|
|
|
$name = $this->getConnectionName($name); |
89
|
|
|
|
90
|
|
|
// Check availability of connection name. |
91
|
|
|
if (empty($name) || !isset($this->settings['connections'][$name])) { |
92
|
|
|
throw new MagentoException('Missing or not found connector name.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Create new instance. |
96
|
|
|
if (!isset($this->instances[$name])) { |
97
|
|
|
$settings = $this->settings['connections'][$name]; |
98
|
|
|
|
99
|
|
|
$settings += array( |
100
|
|
|
'logging' => false, |
101
|
|
|
'logger' => null, |
102
|
|
|
'dispatcher' => null, |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
if (isset($settings['class']) && !empty($settings['class'])) { |
106
|
|
|
$class = $settings['class']; |
107
|
|
|
} else { |
108
|
|
|
$class = $this->defaultClass; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->instances[$name] = $this->createInstance($name, $class, $settings, $options, $autoLogin); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->instances[$name]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $name |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
protected function getConnectionName($name) |
123
|
|
|
{ |
124
|
|
|
// Use default connection. |
125
|
|
|
if (null === $name) { |
126
|
|
|
if (isset($this->settings['default_connection'])) { |
127
|
|
|
$name = $this->settings['default_connection']; |
128
|
|
|
} else { |
129
|
|
|
if (null === $this->settings['default_connection'] && count($this->settings['connections']) == 1) { |
130
|
|
|
/** @var array $connections */ |
131
|
|
|
$connections = $this->settings['connections']; |
132
|
|
|
$name = key($connections); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $name; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $name |
142
|
|
|
* @param string $class |
143
|
|
|
* @param array $settings |
144
|
|
|
* @param array $options |
145
|
|
|
* @param bool $autoLogin |
146
|
|
|
* |
147
|
|
|
* @return RemoteAdapter |
148
|
|
|
*/ |
149
|
|
|
protected function createInstance($name, $class, $settings, $options = array(), $autoLogin = true) |
150
|
|
|
{ |
151
|
|
|
/** @var RemoteAdapter $instance */ |
152
|
|
|
$instance = new $class($name, $settings['url'], $settings['api_user'], $settings['api_key'], $options, $autoLogin); |
153
|
|
|
/** @var EventDispatcherInterface $dispatcher */ |
154
|
|
|
$dispatcher = (null !== $settings['dispatcher'] ? $settings['dispatcher'] : $this->dispatcher); |
155
|
|
|
$instance->setDispatcher($dispatcher); |
156
|
|
|
/** @var LoggerInterface $logger */ |
157
|
|
|
$logger = (null !== $settings['logger'] && $settings['logging'] ? $settings['logger'] : $this->logger); |
158
|
|
|
$instance->setLogger($logger); |
159
|
|
|
|
160
|
|
|
return $instance; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|