Failed Conditions
Push — master ( 493ca2...80c807 )
by Florian
08:07 queued 04:14
created

StorageAdapterFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * Copyright (c) Florian Krämer (https://florian-kraemer.net)
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net)
10
 * @author    Florian Krämer
11
 * @link      https://github.com/Phauthentic
12
 * @license   https://opensource.org/licenses/MIT MIT License
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phauthentic\Infrastructure\Storage;
18
19
use League\Flysystem\AdapterInterface;
20
use Phauthentic\Infrastructure\Storage\Exception\AdapterFactoryNotFoundException;
21
use Psr\Container\ContainerInterface;
22
23
/**
24
 * StorageFactory - Manages and instantiates storage engine adapters.
25
 */
26
class StorageAdapterFactory implements StorageAdapterFactoryInterface
27
{
28
    /**
29
     * @var \Psr\Container\ContainerInterface
30
     */
31
    protected ?ContainerInterface $container;
32
33
    /**
34
     * @param \Psr\Container\ContainerInterface|null
35
     */
36 3
    public function __construct(
37
        ?ContainerInterface $container = null
38
    ) {
39 3
        $this->container = $container;
40 3
    }
41
42
    /**
43
     * Instantiates Flystem adapters.
44
     *
45
     * @param string $adapterClass Adapter alias or classname
46
     * @param array $options Options
47
     * @return \League\Flysystem\AdapterInterface
48
     */
49 3
    public function buildStorageAdapter(
50
        string $adapterClass,
51
        array $options
52
    ): AdapterInterface {
53 3
        if (!class_exists($adapterClass)) {
54 3
            $adapterClass = '\Phauthentic\Infrastructure\Storage\Factories\\' . $adapterClass . 'Factory';
55
        }
56
57 3
        if (!class_exists($adapterClass)) {
58 1
            throw AdapterFactoryNotFoundException::fromName($adapterClass);
59
        }
60
61 2
        if ($this->container !== null) {
62
            return $this->container->get($adapterClass)->build($options);
63
        }
64
65 2
        return (new $adapterClass())->build($options);
66
    }
67
}
68