Service::getComponent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace diecoding\aws\s3;
4
5
use Aws\ResultInterface;
6
use diecoding\aws\s3\interfaces\commands\Command;
7
use diecoding\aws\s3\interfaces\HandlerResolver as HandlerResolverInterface;
8
use diecoding\aws\s3\interfaces\Service as ServiceInterface;
9
use yii\base\Component;
10
use yii\base\InvalidConfigException;
11
use yii\helpers\ArrayHelper;
12
13
/**
14
 * Class Service
15
 *
16
 * @property HandlerResolverInterface $resolver
17
 *
18
 * @method ResultInterface  get(string $filename)
19
 * @method ResultInterface  put(string $filename, $body)
20
 * @method ResultInterface  delete(string $filename)
21
 * @method ResultInterface  upload(string $filename, $source)
22
 * @method ResultInterface  restore(string $filename, int $days)
23
 * @method ResultInterface  list(string $prefix)
24
 * @method bool             exist(string $filename)
25
 * @method string           getUrl(string $filename)
26
 * @method string           getPresignedUrl(string $filename, $expires)
27
 *
28
 * @package diecoding\aws\s3
29
 */
30
class Service extends Component implements ServiceInterface
31
{
32
    /** @var string */
33
    public $defaultBucket = '';
34
35
    /** @var string */
36
    public $defaultAcl = '';
37
38
    /** @var array S3Client config */
39
    protected $clientConfig = ['version' => '2006-03-01'];
40
41
    /** @var array */
42
    private $components = [];
43
44
    /**
45
     * Initializes the object.
46
     * This method is invoked at the end of the constructor after the object is initialized with the
47
     * given configuration.
48
     *
49
     * @throws InvalidConfigException
50
     */
51
    public function init()
52
    {
53
        if (empty($this->clientConfig['credentials'])) {
54
            throw new InvalidConfigException('The "clientConfig[credentials]" property must be set.');
55
        }
56
57
        if (empty($this->clientConfig['region'])) {
58
            throw new InvalidConfigException('The "clientConfig[region]" property must be set.');
59
        }
60
61
        if (empty($this->defaultBucket)) {
62
            throw new InvalidConfigException('The "defaultBucket" property must be set.');
63
        }
64
65
        foreach ($this->defaultComponentDefinitions() as $name => $definition) {
66
            $this->components[$name] = $this->components[$name] ?? $definition;
67
        }
68
    }
69
70
    /**
71
     * Executes a command.
72
     *
73
     * @param Command $command
74
     *
75
     * @return mixed
76
     */
77
    public function execute(Command $command)
78
    {
79
        return $this->getComponent('bus')->execute($command);
80
    }
81
82
    /**
83
     * Creates a command with default params.
84
     *
85
     * @param string $commandClass
86
     *
87
     * @return Command
88
     */
89
    public function create(string $commandClass): Command
90
    {
91
        return $this->getComponent('builder')->build($commandClass);
92
    }
93
94
    /**
95
     * Returns command factory.
96
     *
97
     * @return CommandFactory
98
     */
99
    public function commands(): CommandFactory
100
    {
101
        return $this->getComponent('factory');
102
    }
103
104
    /**
105
     * Returns handler resolver.
106
     *
107
     * @return HandlerResolverInterface
108
     */
109
    public function getResolver(): HandlerResolverInterface
110
    {
111
        return $this->getComponent('resolver');
112
    }
113
114
    /**
115
     * @param string $name
116
     * @param array  $params
117
     *
118
     * @return mixed
119
     */
120
    public function __call($name, $params)
121
    {
122
        if (method_exists($this->commands(), $name)) {
123
            $result = call_user_func_array([$this->commands(), $name], $params);
124
125
            return $result instanceof Command ? $this->execute($result) : $result;
126
        }
127
128
        return parent::__call($name, $params);
129
    }
130
131
    /**
132
     * @param \Aws\Credentials\CredentialsInterface|array|callable $credentials
133
     */
134
    public function setCredentials($credentials)
135
    {
136
        $this->clientConfig['credentials'] = $credentials;
137
    }
138
    
139
    /**
140
     * @param \Aws\Credentials\CredentialsInterface|array|callable $credentials
141
     */
142
    public function setEndpoint($endpoint)
143
    {
144
        $this->clientConfig['endpoint'] = $endpoint;
145
    }
146
147
    /**
148
     * @param string $region
149
     */
150
    public function setRegion(string $region)
151
    {
152
        $this->clientConfig['region'] = $region;
153
    }
154
155
    /**
156
     * @param array|bool $debug
157
     */
158
    public function setDebug($debug)
159
    {
160
        $this->clientConfig['debug'] = $debug;
161
    }
162
163
    /**
164
     * @param array $options
165
     */
166
    public function setHttpOptions(array $options)
167
    {
168
        $this->clientConfig['http'] = $options;
169
    }
170
171
    /**
172
     * @param string $usePathStyleEndpoint
173
     */
174
    public function setUsePathStyleEndpoint($usePathStyleEndpoint)
175
    {
176
        $this->clientConfig['use_path_style_endpoint'] = $usePathStyleEndpoint;
177
    }
178
179
    /**
180
     * @param string|array|object $resolver
181
     */
182
    public function setResolver($resolver)
183
    {
184
        $this->setComponent('resolver', $resolver);
185
    }
186
187
    /**
188
     * @param string|array|object $bus
189
     */
190
    public function setBus($bus)
191
    {
192
        $this->setComponent('bus', $bus);
193
    }
194
195
    /**
196
     * @param string|array|object $builder
197
     */
198
    public function setBuilder($builder)
199
    {
200
        $this->setComponent('builder', $builder);
201
    }
202
203
    /**
204
     * @param string|array|object $factory
205
     */
206
    public function setFactory($factory)
207
    {
208
        $this->setComponent('factory', $factory);
209
    }
210
211
    /**
212
     * @param string $name
213
     *
214
     * @return object
215
     */
216
    protected function getComponent(string $name)
217
    {
218
        if (!is_object($this->components[$name])) {
219
            $this->components[$name] = $this->createComponent($name);
220
        }
221
222
        return $this->components[$name];
223
    }
224
225
    /**
226
     * @param string              $name
227
     * @param array|object|string $definition
228
     */
229
    protected function setComponent(string $name, $definition)
230
    {
231
        if (!is_object($definition)) {
232
            $definition = !is_array($definition) ? ['class' => $definition] : $definition;
233
            $definition = ArrayHelper::merge($this->defaultComponentDefinitions()[$name], $definition);
234
        }
235
236
        $this->components[$name] = $definition;
237
    }
238
239
    /**
240
     * @param string $name
241
     *
242
     * @return object
243
     * @throws \yii\base\InvalidConfigException
244
     */
245
    protected function createComponent(string $name)
246
    {
247
        $definition = $this->components[$name];
248
        $params = $this->getComponentParams($name);
249
250
        return \Yii::createObject($definition, $params);
251
    }
252
253
    /**
254
     * @return array
255
     */
256
    protected function defaultComponentDefinitions()
257
    {
258
        return [
259
            'client'   => ['class' => 'Aws\S3\S3Client'],
260
            'resolver' => ['class' => 'diecoding\aws\s3\HandlerResolverInterface'],
261
            'bus'      => ['class' => 'diecoding\aws\s3\Bus'],
262
            'builder'  => ['class' => 'diecoding\aws\s3\CommandBuilder'],
263
            'factory'  => ['class' => 'diecoding\aws\s3\CommandFactory'],
264
        ];
265
    }
266
267
    /**
268
     * @param string $name
269
     *
270
     * @return array
271
     */
272
    protected function getComponentParams(string $name): array
273
    {
274
        switch ($name) {
275
            case 'client':
276
                $params = [$this->clientConfig];
277
                break;
278
            case 'resolver':
279
                $params = [$this->getComponent('client')];
280
                break;
281
            case 'bus':
282
                $params = [$this->getComponent('resolver')];
283
                break;
284
            case 'builder':
285
                $params = [$this->getComponent('bus'), $this->defaultBucket, $this->defaultAcl];
286
                break;
287
            case 'factory':
288
                $params = [$this->getComponent('builder')];
289
                break;
290
            default:
291
                $params = [];
292
        }
293
294
        return $params;
295
    }
296
}
297