Passed
Pull Request — master (#407)
by Kirill
06:33
created

StorageConfigTrait::buildBucketNameByServer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tests\Storage\Traits;
6
7
use League\Flysystem\Local\LocalFilesystemAdapter;
8
use Spiral\Storage\Config\DTO\FileSystemInfo\LocalInfo;
9
use Spiral\Storage\Config\StorageConfig;
10
use Spiral\Storage\Exception\ConfigException;
11
12
trait StorageConfigTrait
13
{
14
    /**
15
     * Build storage config by provided servers and buckets
16
     * If no buckets were defined it will be built for each defined server
17
     *
18
     * @param array|null $servers
19
     * @param array|null $buckets
20
     *
21
     * @return StorageConfig
22
     *
23
     * @throws ConfigException
24
     */
25
    protected function buildStorageConfig(?array $servers = null, ?array $buckets = null): StorageConfig
26
    {
27
        if (empty($servers)) {
28
            $servers[self::SERVER_NAME] = [
0 ignored issues
show
Bug introduced by
The constant Spiral\Tests\Storage\Tra...onfigTrait::SERVER_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
29
                LocalInfo::ADAPTER_KEY => LocalFilesystemAdapter::class,
30
                LocalInfo::OPTIONS_KEY => [
31
                    LocalInfo::ROOT_DIR_KEY => self::ROOT_DIR,
0 ignored issues
show
Bug introduced by
The constant Spiral\Tests\Storage\Tra...geConfigTrait::ROOT_DIR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
32
                    LocalInfo::HOST_KEY => self::CONFIG_HOST,
0 ignored issues
show
Bug introduced by
The constant Spiral\Tests\Storage\Tra...onfigTrait::CONFIG_HOST was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
33
                ],
34
            ];
35
        }
36
37
        if (!empty($servers) && empty($buckets)) {
38
            $buckets = [];
39
            foreach ($servers as $server => $serverInfo) {
40
                $buckets[$this->buildBucketNameByServer($server)] = $this->buildServerBucketInfoDesc($server);
41
            }
42
        }
43
44
        return new StorageConfig([
45
            'servers' => $servers,
46
            'buckets' => $buckets
47
        ]);
48
    }
49
50
    protected function buildServerBucketInfoDesc(string $serverName): array
51
    {
52
        return [
53
            'server' => $serverName,
54
            'directory' => 'tmp/',
55
        ];
56
    }
57
58
    protected function buildBucketNameByServer(string $server): string
59
    {
60
        return $server . 'Bucket';
61
    }
62
}
63