AdapterFactory::createValidateOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Plugin\Filesystem\Adapter\Aws\Business\Adapter;
15
16
use Aws\S3\S3Client;
17
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
18
use League\Flysystem\FilesystemAdapter;
19
use Micro\Plugin\Filesystem\Adapter\Aws\Configuration\Adapter\AwsS3AdapterConfigurationInterface;
20
use Micro\Plugin\Filesystem\Business\Adapter\AdapterFactoryInterface;
21
use Micro\Plugin\Filesystem\Configuration\Adapter\FilesystemAdapterConfigurationInterface;
22
23
class AdapterFactory implements AdapterFactoryInterface
24
{
25 2
    public function create(FilesystemAdapterConfigurationInterface $adapterConfiguration): FilesystemAdapter
26
    {
27 2
        if (!($adapterConfiguration instanceof AwsS3AdapterConfigurationInterface)) {
28 1
            throw new \InvalidArgumentException(sprintf('Adapter configuration should be instance of %s.', AwsS3AdapterConfigurationInterface::class));
29
        }
30
31
        // TODO:
32
        // validate ['required, min, max, pattern]
33
        // signature_version service retries
34
35 1
        $clientOptions = [
36 1
            'version' => $adapterConfiguration->getVersion(),
37 1
            'region' => $adapterConfiguration->getRegion(),
38 1
            'use_aws_shared_config_files' => $adapterConfiguration->isUseSharedConfigFile(),
39 1
            'use_path_style_endpoint' => true,
40 1
            'ua_append' => $adapterConfiguration->getUserAgentParameters(),
41 1
            'scheme' => $adapterConfiguration->getScheme(),
42 1
            'retries' => $adapterConfiguration->getRetries(),
43 1
            'endpoint' => $adapterConfiguration->getEndpoint(),
44 1
            'signature_version' => $adapterConfiguration->getSignatureVersion(),
45 1
            'validate' => $this->createValidateOptions($adapterConfiguration),
46 1
            'credentials' => [
47 1
                'key' => $adapterConfiguration->getKeyAccess(),
48 1
                'secret' => $adapterConfiguration->getKeySecret(),
49 1
            ],
50 1
        ];
51
52 1
        $client = new S3Client(array_filter($clientOptions));
53
54 1
        return new AwsS3V3Adapter(
55 1
            $client,
56 1
            $adapterConfiguration->getBucket(),
57 1
        );
58
    }
59
60
    /**
61
     * @param AwsS3AdapterConfigurationInterface $adapterConfiguration
62
     *
63
     * @return array<string|mixed>
64
     */
65 1
    protected function createValidateOptions(AwsS3AdapterConfigurationInterface $adapterConfiguration): array
66
    {
67 1
        $validateOptions = [
68 1
            'required' => $adapterConfiguration->getValidatorOptionRequired(),
69 1
            'min' => $adapterConfiguration->getValidatorOptionMin(),
70 1
            'max' => $adapterConfiguration->getValidatorOptionMax(),
71 1
            'pattern' => $adapterConfiguration->getValidatorOptionPattern(),
72 1
        ];
73
74 1
        return array_filter($validateOptions);
75
    }
76
}
77