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

AbstractAdapterResolver::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 19
rs 9.9332
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Storage\Resolver;
13
14
use Spiral\Storage\Config\ConfigInterface;
15
use Spiral\Storage\Config\DTO\FileSystemInfo\FileSystemInfoInterface;
16
use Spiral\Storage\Exception\StorageException;
17
use Spiral\Storage\Exception\UriException;
18
use Spiral\Storage\Parser\UriParserInterface;
19
20
/**
21
 * Abstract class for any resolver
22
 * Depends on adapter by default
23
 */
24
abstract class AbstractAdapterResolver implements AdapterResolverInterface
25
{
26
    /**
27
     * Filesystem info class required for resolver
28
     * In case other filesystem info will be provided - exception will be thrown
29
     */
30
    protected const FILE_SYSTEM_INFO_CLASS = '';
31
32
    /**
33
     * @var FileSystemInfoInterface
34
     */
35
    protected $fsInfo;
36
37
    /**
38
     * @var UriParserInterface
39
     */
40
    protected $uriParser;
41
42
    /**
43
     * @param UriParserInterface $uriParser
44
     * @param ConfigInterface $config
45
     * @param string $fs
46
     *
47
     * @throws StorageException
48
     */
49
    public function __construct(UriParserInterface $uriParser, ConfigInterface $config, string $fs)
50
    {
51
        $requiredClass = static::FILE_SYSTEM_INFO_CLASS;
52
53
        $fsInfo = $config->buildFileSystemInfo($fs);
54
55
        if (empty($requiredClass) || !$fsInfo instanceof $requiredClass) {
0 ignored issues
show
introduced by
The condition empty($requiredClass) is always true.
Loading history...
56
            throw new StorageException(
57
                \sprintf(
58
                    'Wrong filesystem info (`%s`) for resolver `%s`',
59
                    get_class($fsInfo),
60
                    static::class
61
                )
62
            );
63
        }
64
65
        $this->uriParser = $uriParser;
66
67
        $this->fsInfo = $fsInfo;
68
    }
69
70
    /**
71
     * Normalize filepath for filesystem operation
72
     * In case uri provided path to file will be extracted
73
     * In case filepath provided it will be returned
74
     *
75
     * @param string $filePath
76
     *
77
     * @return string
78
     */
79
    public function normalizeFilePath(string $filePath): string
80
    {
81
        try {
82
            $uri = $this->uriParser->parse($filePath);
0 ignored issues
show
Bug introduced by
$filePath of type string is incompatible with the type Spiral\Storage\Parser\UriLikeType expected by parameter $uri of Spiral\Storage\Parser\UriParserInterface::parse(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
            $uri = $this->uriParser->parse(/** @scrutinizer ignore-type */ $filePath);
Loading history...
83
84
            return $uri->getPath();
85
        } catch (UriException $e) {
86
            // if filePath is not uri we suppose it is short form of filepath - without fs part
87
        }
88
89
        return $filePath;
90
    }
91
}
92