AdapterContainer::load()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 112
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 69
nc 1
nop 0
dl 0
loc 112
rs 8.6763
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * This file is part of Compressy.
4
 *
5
 * (c) Alchemy <[email protected]>
6
 * (c) Miguel Gocobachi <[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
namespace Gocobachi\Compressy\Adapter;
12
13
use Gocobachi\Compressy\Adapter\BSDTar\TarBSDTarAdapter;
14
use Gocobachi\Compressy\Adapter\BSDTar\TarBz2BSDTarAdapter;
15
use Gocobachi\Compressy\Adapter\BSDTar\TarGzBSDTarAdapter;
16
use Gocobachi\Compressy\Adapter\GNUTar\TarBz2GNUTarAdapter;
17
use Gocobachi\Compressy\Adapter\GNUTar\TarGNUTarAdapter;
18
use Gocobachi\Compressy\Adapter\GNUTar\TarGzGNUTarAdapter;
19
use Gocobachi\Compressy\Resource\RequestMapper;
20
use Gocobachi\Compressy\Resource\ResourceManager;
21
use Gocobachi\Compressy\Resource\ResourceTeleporter;
22
use Gocobachi\Compressy\Resource\TargetLocator;
23
use Gocobachi\Compressy\Resource\TeleporterContainer;
24
use Symfony\Component\Filesystem\Filesystem;
25
use Symfony\Component\Process\ExecutableFinder;
26
27
class AdapterContainer implements \ArrayAccess
28
{
29
    private $items = [];
30
31
    /**
32
     * Builds the adapter container
33
     *
34
     * @return AdapterContainer
35
     */
36
    public static function load()
37
    {
38
        $container = new static();
39
40
        $container['zip.inflator'] = null;
41
        $container['zip.deflator'] = null;
42
43
        $container['resource-manager'] = function($container) {
44
            return new ResourceManager(
45
                $container['request-mapper'],
46
                $container['resource-teleporter'],
47
                $container['filesystem']
48
            );
49
        };
50
51
        $container['executable-finder'] = function() {
52
            return new ExecutableFinder();
53
        };
54
55
        $container['request-mapper'] = function($container) {
56
            return new RequestMapper($container['target-locator']);
57
        };
58
59
        $container['target-locator'] = function() {
60
            return new TargetLocator();
61
        };
62
63
        $container['teleporter-container'] = function() {
64
            return TeleporterContainer::load();
65
        };
66
67
        $container['resource-teleporter'] = function($container) {
68
            return new ResourceTeleporter($container['teleporter-container']);
69
        };
70
71
        $container['filesystem'] = function() {
72
            return new Filesystem();
73
        };
74
75
        $container[ZipAdapter::class] = function($container) {
76
            return ZipAdapter::newInstance(
77
                $container['executable-finder'],
78
                $container['resource-manager'],
79
                $container['zip.inflator'],
80
                $container['zip.deflator']
81
            );
82
        };
83
84
        $container['gnu-tar.inflator'] = null;
85
        $container['gnu-tar.deflator'] = null;
86
87
        $container[TarGNUTarAdapter::class] = function($container) {
88
            return TarGNUTarAdapter::newInstance(
89
                $container['executable-finder'],
90
                $container['resource-manager'],
91
                $container['gnu-tar.inflator'],
92
                $container['gnu-tar.deflator']
93
            );
94
        };
95
96
        $container[TarGzGNUTarAdapter::class] = function($container) {
97
            return TarGzGNUTarAdapter::newInstance(
98
                $container['executable-finder'],
99
                $container['resource-manager'],
100
                $container['gnu-tar.inflator'],
101
                $container['gnu-tar.deflator']
102
            );
103
        };
104
105
        $container[TarBz2GNUTarAdapter::class] = function($container) {
106
            return TarBz2GNUTarAdapter::newInstance(
107
                $container['executable-finder'],
108
                $container['resource-manager'],
109
                $container['gnu-tar.inflator'],
110
                $container['gnu-tar.deflator']
111
            );
112
        };
113
114
        $container['bsd-tar.inflator'] = null;
115
        $container['bsd-tar.deflator'] = null;
116
117
        $container[TarBSDTarAdapter::class] = function($container) {
118
            return TarBSDTarAdapter::newInstance(
119
                $container['executable-finder'],
120
                $container['resource-manager'],
121
                $container['bsd-tar.inflator'],
122
                $container['bsd-tar.deflator']
123
            );
124
        };
125
126
        $container[TarGzBSDTarAdapter::class] = function($container) {
127
            return TarGzBSDTarAdapter::newInstance(
128
                $container['executable-finder'],
129
                $container['resource-manager'],
130
                $container['bsd-tar.inflator'],
131
                $container['bsd-tar.deflator']
132
            );
133
        };
134
135
        $container[TarBz2BSDTarAdapter::class] = function($container) {
136
            return TarBz2BSDTarAdapter::newInstance(
137
                $container['executable-finder'],
138
                $container['resource-manager'],
139
                $container['bsd-tar.inflator'],
140
                $container['bsd-tar.deflator']);
141
        };
142
143
        $container[ZipExtensionAdapter::class] = function() {
144
            return ZipExtensionAdapter::newInstance();
145
        };
146
147
        return $container;
148
    }
149
150
    /**
151
     * (PHP 5 &gt;= 5.0.0)<br/>
152
     * Whether a offset exists
153
     *
154
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
155
     *
156
     * @param mixed $offset <p>
157
     *                      An offset to check for.
158
     *                      </p>
159
     *
160
     * @return bool true on success or false on failure.
161
     * <p>The return value will be casted to boolean if non-boolean was returned.</p>
162
     */
163
    public function offsetExists($offset)
164
    {
165
        return isset($this->items[$offset]);
166
    }
167
168
    /**
169
     * (PHP 5 &gt;= 5.0.0)<br/>
170
     * Offset to retrieve
171
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
172
     * @param mixed $offset <p>
173
     * The offset to retrieve.
174
     * </p>
175
     * @return mixed Can return all value types.
176
     */
177
    public function offsetGet($offset)
178
    {
179
        if (array_key_exists($offset, $this->items) && is_callable($this->items[$offset])) {
180
            $this->items[$offset] = call_user_func($this->items[$offset], $this);
181
        }
182
183
        if (array_key_exists($offset, $this->items)) {
184
            return $this->items[$offset];
185
        }
186
187
        throw new \InvalidArgumentException();
188
    }
189
190
    /**
191
     * (PHP 5 &gt;= 5.0.0)<br/>
192
     * Offset to set
193
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
194
     * @param mixed $offset <p>
195
     * The offset to assign the value to.
196
     * </p>
197
     * @param mixed $value <p>
198
     * The value to set.
199
     * </p>
200
     * @return void
201
     */
202
    public function offsetSet($offset, $value)
203
    {
204
        $this->items[$offset] = $value;
205
    }
206
207
    /**
208
     * (PHP 5 &gt;= 5.0.0)<br/>
209
     * Offset to unset
210
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
211
     * @param mixed $offset <p>
212
     * The offset to unset.
213
     * </p>
214
     * @return void
215
     */
216
    public function offsetUnset($offset)
217
    {
218
        unset($this->items[$offset]);
219
    }
220
}
221