1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lamoda\Codeception\Extension; |
4
|
|
|
|
5
|
|
|
use Codeception\Exception\ModuleConfigException; |
6
|
|
|
use Codeception\Exception\TestRuntimeException; |
7
|
|
|
use Codeception\Module as CodeceptionModule; |
8
|
|
|
use Lamoda\Codeception\Extension\AdapterFactory\AdapterFactoryInterface; |
9
|
|
|
use League\Flysystem\Filesystem as FlySystem; |
10
|
|
|
|
11
|
|
|
class FlySystemModule extends CodeceptionModule |
12
|
|
|
{ |
13
|
|
|
/** @var FileSystem[] */ |
14
|
|
|
protected $instances = []; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $path; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
protected $requiredParametersConfig = [ |
21
|
|
|
'builderAdapter' => 'string', |
22
|
|
|
'config' => 'array', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @throws ModuleConfigException |
27
|
|
|
*/ |
28
|
6 |
|
public function _initialize() |
29
|
|
|
{ |
30
|
6 |
|
if (isset($this->config['adapters']) && is_array($this->config['adapters'])) { |
31
|
5 |
|
foreach ($this->config['adapters'] as $name => $adapterConfig) { |
32
|
5 |
|
$this->validateAdapterConfig($adapterConfig, $name); |
33
|
3 |
|
$this->instances[$name] = $this->createFileSystemHelper($adapterConfig); |
34
|
|
|
} |
35
|
|
|
} |
36
|
2 |
|
parent::_initialize(); |
37
|
2 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $name |
41
|
|
|
* |
42
|
|
|
* @return FileSystem |
43
|
|
|
*/ |
44
|
1 |
|
public function getFileSystem($name) |
45
|
|
|
{ |
46
|
1 |
|
if (!isset($this->instances[$name])) { |
47
|
|
|
throw new TestRuntimeException("Undefined adapter $name"); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
return $this->instances[$name]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param array $config |
55
|
|
|
* @param string $name |
56
|
|
|
* |
57
|
|
|
* @throws ModuleConfigException |
58
|
|
|
*/ |
59
|
5 |
|
private function validateAdapterConfig(array $config, $name) |
60
|
|
|
{ |
61
|
5 |
|
foreach ($this->requiredParametersConfig as $parameter => $type) { |
62
|
5 |
|
if (!isset($parameter, $config)) { |
63
|
|
|
$message = sprintf('Configuration for %s is broken. Configuration must contains %s', $name, implode($this->requiredParametersConfig)); |
64
|
|
|
throw new ModuleConfigException(__CLASS__, $message); |
65
|
|
|
} |
66
|
|
|
switch ($type) { |
67
|
5 |
|
case 'array': |
68
|
4 |
|
$valid = is_array($config[$parameter]); |
69
|
4 |
|
break; |
70
|
|
|
default: |
71
|
5 |
|
$valid = is_string($config[$parameter]); |
72
|
|
|
} |
73
|
|
|
|
74
|
5 |
|
if (!$valid) { |
75
|
2 |
|
$message = sprintf('Configuration for %s is broken. Expected type is %s for parameter %s', $name, $type, $parameter); |
76
|
5 |
|
throw new ModuleConfigException(__CLASS__, $message); |
77
|
|
|
} |
78
|
|
|
} |
79
|
3 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $className |
83
|
|
|
* |
84
|
|
|
* @throws ModuleConfigException |
85
|
|
|
* |
86
|
|
|
* @return AdapterFactoryInterface |
87
|
|
|
*/ |
88
|
3 |
|
private function createAdapterFactory($className) |
89
|
|
|
{ |
90
|
3 |
|
if (!class_exists($className)) { |
91
|
1 |
|
$message = sprintf('Adapter %s does not exist, please use another one', $className); |
92
|
1 |
|
throw new ModuleConfigException(__CLASS__, $message); |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
$adapterFactory = new $className(); |
96
|
|
|
|
97
|
2 |
|
if (!$adapterFactory instanceof AdapterFactoryInterface) { |
98
|
1 |
|
$message = sprintf('Adapter factory class must implement %s, %s adapter factory given', AdapterFactoryInterface::class, $className); |
99
|
1 |
|
throw new ModuleConfigException(__CLASS__, $message); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
return $adapterFactory; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $adapterConfig |
107
|
|
|
* |
108
|
|
|
* @throws ModuleConfigException |
109
|
|
|
* |
110
|
|
|
* @return FileSystem |
111
|
|
|
*/ |
112
|
3 |
|
private function createFileSystemHelper(array $adapterConfig) |
113
|
|
|
{ |
114
|
3 |
|
$adapterFactoryClass = $adapterConfig['builderAdapter']; |
115
|
3 |
|
$config = $adapterConfig['config']; |
116
|
3 |
|
$builderAdapter = $this->createAdapterFactory($adapterFactoryClass); |
117
|
1 |
|
$adapter = $builderAdapter::createAdapter($config); |
118
|
1 |
|
$fileSystem = new FlySystem($adapter); |
119
|
|
|
|
120
|
1 |
|
return new FileSystem($fileSystem); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|