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