FactoryAdapter::getManager()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8017
c 0
b 0
f 0
cc 6
nc 4
nop 3
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\ContainerInterface;
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
     * @var array
32
     */
33
    protected $instances = array();
34
35
    /**
36
     * @var string
37
     */
38
    protected $defaultClass;
39
40
    /**
41
     * @var array
42
     */
43
    protected $settings;
44
45
    /**
46
     * @var EventDispatcherInterface
47
     */
48
    protected $dispatcher;
49
50
    /**
51
     * @var LoggerInterface
52
     */
53
    protected $logger;
54
55
    /**
56
     * @var ContainerInterface
57
     */
58
    protected $container;
59
60
    /**
61
     * @param string                   $defaultClass
62
     * @param EventDispatcherInterface $dispatcher
63
     * @param LoggerInterface          $logger
64
     */
65
    public function __construct(
66
      $defaultClass = null,
67
      EventDispatcherInterface $dispatcher = null,
68
      LoggerInterface $logger = null
69
    ) {
70
        $this->defaultClass = $defaultClass;
71
        $this->dispatcher   = $dispatcher;
72
        $this->logger       = $logger;
73
    }
74
75
    /**
76
     * Sets the Container associated with this Controller.
77
     *
78
     * @param ContainerInterface $container A ContainerInterface instance
79
     *
80
     * @api
81
     */
82
    public function setContainer(ContainerInterface $container = null)
83
    {
84
        $this->container = $container;
85
86
        // Load settings.
87
        $this->settings = $this->container->getParameter('magento');
88
    }
89
90
    /**
91
     * @param string $name
92
     * @param array  $options
93
     * @param bool   $autoLogin
94
     *
95
     * @return RemoteAdapterInterface
96
     * @throws MagentoException
97
     */
98
    public function getManager($name = null, $options = array(), $autoLogin = true)
99
    {
100
        // Get default connection if necessary.
101
        $name = $this->getConnectionName($name);
102
103
        // Check availability of connection name.
104
        if (empty($name) || !isset($this->settings['connections'][$name])) {
105
            throw new MagentoException('Missing or not found connector name.');
106
        }
107
108
        // Create new instance.
109
        if (!isset($this->instances[$name])) {
110
            $settings = $this->settings['connections'][$name];
111
112
            $settings += array(
113
              'logging'    => false,
114
              'logger'     => null,
115
              'dispatcher' => null,
116
            );
117
118
            if (isset($settings['class']) && !empty($settings['class'])) {
119
                $class = $settings['class'];
120
            } else {
121
                $class = $this->defaultClass;
122
            }
123
124
            $this->instances[$name] = $this->createInstance($name, $class, $settings, $options, $autoLogin);
125
        }
126
127
        return $this->instances[$name];
128
    }
129
130
    /**
131
     * @param string $name
132
     *
133
     * @return string
134
     */
135
    protected function getConnectionName($name)
136
    {
137
        // Use default connection.
138
        if (null === $name) {
139
            if (isset($this->settings['default_connection'])) {
140
                $name = $this->settings['default_connection'];
141
            } else {
142
                if (null === $this->settings['default_connection'] && count($this->settings['connections']) == 1) {
143
                    /** @var array $connections */
144
                    $connections = $this->settings['connections'];
145
                    $name        = key($connections);
146
                }
147
            }
148
        }
149
150
        return $name;
151
    }
152
153
    /**
154
     * @param string $name
155
     * @param string $class
156
     * @param array  $settings
157
     * @param array  $options
158
     * @param bool   $autoLogin
159
     *
160
     * @return RemoteAdapter
161
     */
162
    protected function createInstance($name, $class, $settings, $options = array(), $autoLogin = true)
163
    {
164
        /** @var RemoteAdapter $instance */
165
        $instance = new $class($name, $settings['url'], $settings['api_user'], $settings['api_key'], $options, $autoLogin);
166
        /** @var EventDispatcherInterface $dispatcher */
167
        $dispatcher = (null !== $settings['dispatcher'] ? $settings['dispatcher'] : $this->dispatcher);
168
        $instance->setDispatcher($dispatcher);
169
        /** @var LoggerInterface $logger */
170
        $logger = (null !== $settings['logger'] && $settings['logging'] ? $settings['logger'] : $this->logger);
171
        $instance->setLogger($logger);
172
173
        return $instance;
174
    }
175
}
176