AdaptorManager::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace WShafer\PSR11FlySystem\Service;
5
6
use League\Flysystem\AdapterInterface;
7
use Psr\Container\ContainerInterface;
8
use WShafer\PSR11FlySystem\Config\MainConfig;
9
use WShafer\PSR11FlySystem\Exception\UnknownAdaptorException;
10
use WShafer\PSR11FlySystem\MapperInterface;
11
12
class AdaptorManager implements ContainerInterface
13
{
14
    /** @var MainConfig */
15
    protected $config;
16
17
    /** @var MapperInterface */
18
    protected $adaptorMapper;
19
20
    /** @var AdapterInterface[] */
21
    protected $adaptors = [];
22
23
    /** @var ContainerInterface */
24
    protected $container;
25
26
    /**
27
     * Manager constructor.
28
     * @param MainConfig         $config
29
     * @param MapperInterface    $adaptorMapper
30
     * @param ContainerInterface $container
31
     */
32 8
    public function __construct(
33
        MainConfig $config,
34
        MapperInterface $adaptorMapper,
35
        ContainerInterface $container
36
    ) {
37 8
        $this->config = $config;
38 8
        $this->adaptorMapper = $adaptorMapper;
39 8
        $this->container = $container;
40 8
    }
41
42
    /**
43
     * @param string $id
44
     * @return AdapterInterface
45
     */
46 3
    public function get($id)
47
    {
48 3
        if (!$this->has($id)) {
49 1
            throw new UnknownAdaptorException(
50 1
                'Unable to locate adaptor '.$id.'.  Please check your configuration.'
51
            );
52
        }
53
54 2
        if (!key_exists($id, $this->adaptors)) {
55 2
            $adaptorConfig = $this->config->getAdaptorConfig($id);
56 2
            $this->adaptors[$id] = $this->adaptorMapper->get(
57 2
                $adaptorConfig->getType(),
58 2
                $adaptorConfig->getOptions()
59
            );
60
        }
61
62 2
        return $this->adaptors[$id];
63
    }
64
65
    /**
66
     * @param string $id
67
     * @return bool
68
     */
69 5
    public function has($id)
70
    {
71 5
        return $this->config->hasAdaptorConfig($id);
72
    }
73
}
74