HttpAdapterFactory::createAdapter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of Laravel HTTP Adapter.
5
 *
6
 * (c) Hidde Beydals <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace HiddeCo\HttpAdapter;
13
14
use HiddeCo\HttpAdapter\Adapters\AbstractEventDispatcher;
15
use Ivory\HttpAdapter\EventDispatcherHttpAdapter;
16
use Ivory\HttpAdapter\HttpAdapterFactory as AdapterFactory;
17
18
class HttpAdapterFactory
19
{
20
    /**
21
     * The adapter factory instance.
22
     *
23
     * @var \Ivory\HttpAdapter\HttpAdapterFactory
24
     */
25
    protected $adapter;
26
27
    /**
28
     * The configuration factory instance.
29
     *
30
     * @var \HiddeCo\HttpAdapter\ConfigurationFactory
31
     */
32
    protected $configuration;
33
34
    /**
35
     * The event dispatcher instance.
36
     *
37
     * @var \HiddeCo\HttpAdapter\Adapters\AbstractEventDispatcher
38
     */
39
    protected $eventDispatcher;
40
41
    /**
42
     * Create a new HTTP adapter factory instance.
43
     *
44
     * @param \Ivory\HttpAdapter\HttpAdapterFactory
45
     * @param \HiddeCo\HttpAdapter\ConfigurationFactory
46
     */
47
    public function __construct(AdapterFactory $adapter, ConfigurationFactory $configuration, AbstractEventDispatcher $eventDispatcher)
48
    {
49
        $this->adapter = $adapter;
50
        $this->configuration = $configuration;
51
        $this->eventDispatcher = $eventDispatcher;
52
    }
53
54
    /**
55
     * Make a new Ivory HTTP adapter instance.
56
     *
57
     * @param array $config
58
     *
59
     * @return \Ivory\HttpAdapter\HttpAdapterInterface
60
     */
61
    public function make(array $config)
62
    {
63
        $adapter = $this->createAdapter($config);
64
65
        if (is_array($parameters = array_get($config, 'config'))) {
66
            $configuration = $this->createConfiguration($parameters);
67
            $adapter->setConfiguration($configuration);
68
        }
69
70
        if (array_get($config, 'eventable', false)) {
71
            return new EventDispatcherHttpAdapter($adapter, $this->eventDispatcher);
72
        }
73
74
        return $adapter;
75
    }
76
77
    /**
78
     * Establish an adapter connection.
79
     *
80
     * @param array $config
81
     *
82
     * @return \Ivory\HttpAdapter\HttpAdapterInterface
83
     */
84
    public function createAdapter(array $config)
85
    {
86
        return $this->adapter->create($config['adapter']);
87
    }
88
89
    /**
90
     * Establish an configuration.
91
     *
92
     * @param array $config
93
     *
94
     * @return \Ivory\HttpAdapter\ConfigurationInterface
95
     */
96
    public function createConfiguration(array $config)
97
    {
98
        return $this->configuration->create($config);
99
    }
100
}
101