Passed
Pull Request — master (#16)
by
unknown
03:22
created

Factory::createAssetsFromPaths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Aoe\Asdis\Domain\Model\Asset;
4
5
use Aoe\Asdis\Domain\Model\Asset;
6
use Aoe\Asdis\Domain\Model\Asset\Collection as AssetCollection;
7
use Aoe\Asdis\System\Uri\Filter\Chain;
8
use Aoe\Asdis\System\Uri\Normalizer;
9
use Aoe\Asdis\System\Uri\Filter\ChainFactory;
10
use TYPO3\CMS\Core\Utility\GeneralUtility;
11
12
/**
13
 * Factory which builds asset objects and collections.
14
 */
15
class Factory
16
{
17
    private ?Normalizer $uriNormalizer = null;
18
19
    private ?Chain $filterChain = null;
20
21 2
    public function injectUriNormalizer(Normalizer $uriNormalizer): void
22
    {
23 2
        $this->uriNormalizer = $uriNormalizer;
24 2
    }
25
26 2
    public function injectFilterChainFactory(ChainFactory $filterChainFactory): void
27
    {
28 2
        $this->filterChain = $filterChainFactory->buildChain();
29 2
    }
30
31
    /**
32
     * @param array $paths Array of path strings.
33
     * @param array $masks Array of mask strings.
34
     */
35 2
    public function createAssetsFromPaths(array $paths, array $masks): Collection
36
    {
37 2
        $filteredPaths = $this->filterChain->filter($paths);
0 ignored issues
show
Bug introduced by
The method filter() does not exist on null. ( Ignorable by Annotation )

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

37
        /** @scrutinizer ignore-call */ 
38
        $filteredPaths = $this->filterChain->filter($paths);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38 2
        $paths = array_intersect($paths, $filteredPaths);
39 2
        $masks = array_intersect_key($masks, $paths);
40
41 2
        $assets = $this->createAssetCollection();
42 2
        foreach ($paths as $key => $path) {
43 2
            $assets->append($this->createAssetFromPath($path, $masks[$key]));
44
        }
45 2
        return $assets;
46
    }
47
48 2
    protected function createAssetFromPath(string $path, string $mask): Asset
49
    {
50 2
        $asset = $this->createAsset();
51 2
        $asset->setOriginalPath($path);
52 2
        $asset->setMask($mask);
53 2
        $asset->setNormalizedPath($this->getNormalizedPath($path));
54 2
        return $asset;
55
    }
56
57
    protected function createAsset(): Asset
58
    {
59
        return GeneralUtility::makeInstance(Asset::class);
60
    }
61
62
    protected function createAssetCollection(): Collection
63
    {
64
        return GeneralUtility::makeInstance(AssetCollection::class);
65
    }
66
67 2
    private function getNormalizedPath(string $originalPath): string
68
    {
69 2
        return $this->uriNormalizer->normalizePath($originalPath);
0 ignored issues
show
Bug introduced by
The method normalizePath() does not exist on null. ( Ignorable by Annotation )

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

69
        return $this->uriNormalizer->/** @scrutinizer ignore-call */ normalizePath($originalPath);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
    }
71
}
72