Factory::createAssetFromPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Aoe\Asdis\Domain\Model\Asset;
3
4
use Aoe\Asdis\Domain\Model\Asset;
5
use Aoe\Asdis\Domain\Model\Asset\Collection as AssetCollection;
6
use Aoe\Asdis\System\Uri\Normalizer;
7
use Aoe\Asdis\System\Uri\Filter\Chain;
8
use Aoe\Asdis\System\Uri\Filter\ChainFactory;
9
10
/**
11
 * Factory which builds asset objects and collections.
12
 */
13
class Factory
14
{
15
    /**
16
     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
17
     */
18
    private $objectManager;
19
20
    /**
21
     * @var \Aoe\Asdis\System\Uri\Normalizer
22
     */
23
    private $uriNormalizer;
24
25
    /**
26
     * @var \Aoe\Asdis\System\Uri\Filter\Chain
27
     */
28
    private $filterChain;
29
30
    /**
31
     * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
32
     */
33
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
34
    {
35
        $this->objectManager = $objectManager;
36
    }
37
38
    /**
39
     * @param \Aoe\Asdis\System\Uri\Normalizer $uriNormalizer
40
     */
41 2
    public function injectUriNormalizer(Normalizer $uriNormalizer)
42
    {
43 2
        $this->uriNormalizer = $uriNormalizer;
44 2
    }
45
46
    /**
47
     * @param Aoe\Asdis\System\Uri\Filter\ChainFactory $filterChainFactory
0 ignored issues
show
Bug introduced by
The type Aoe\Asdis\Domain\Model\A...Uri\Filter\ChainFactory was not found. Did you mean Aoe\Asdis\System\Uri\Filter\ChainFactory? If so, make sure to prefix the type with \.
Loading history...
48
     */
49 2
    public function injectFilterChainFactory(ChainFactory $filterChainFactory)
50
    {
51 2
        $this->filterChain = $filterChainFactory->buildChain();
52 2
    }
53
54
    /**
55
     * @param array $paths Array of path strings.
56
     * @param array $masks Array of mask strings.
57
     * @return \Aoe\Asdis\Domain\Model\Asset\Collection
58
     */
59 2
    public function createAssetsFromPaths(array $paths, array $masks)
60
    {
61 2
        $filteredPaths  = $this->filterChain->filter($paths);
62 2
        $paths = array_intersect($paths, $filteredPaths);
63 2
        $masks = array_intersect_key($masks, $paths);
64
65 2
        $assets = $this->createAssetCollection();
66 2
        foreach ($paths as $key => $path) {
67 2
            $assets->append($this->createAssetFromPath($path, $masks[$key]));
68
        }
69 2
        return $assets;
70
    }
71
72
    /**
73
     * @param string $path
74
     * @param string $mask
75
     * @return \Aoe\Asdis\Domain\Model\Asset
76
     */
77 2
    protected function createAssetFromPath($path, $mask)
78
    {
79 2
        $asset = $this->createAsset();
80 2
        $asset->setOriginalPath($path);
81 2
        $asset->setMask($mask);
82 2
        $asset->setNormalizedPath($this->getNormalizedPath($path));
83 2
        return $asset;
84
    }
85
86
    /**
87
     * @return \Aoe\Asdis\Domain\Model\Asset
88
     */
89
    protected function createAsset() {
90
        return $this->objectManager->get(Asset::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

90
        return /** @scrutinizer ignore-deprecated */ $this->objectManager->get(Asset::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
91
    }
92
93
    /**
94
     * @return \Aoe\Asdis\Domain\Model\Asset\Collection
95
     */
96
    protected function createAssetCollection() {
97
        return $this->objectManager->get(AssetCollection::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

97
        return /** @scrutinizer ignore-deprecated */ $this->objectManager->get(AssetCollection::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
98
    }
99
100
    /**
101
     * @param string $originalPath
102
     * @return string
103
     */
104 2
    private function getNormalizedPath($originalPath) {
105 2
        return $this->uriNormalizer->normalizePath($originalPath);
106
    }
107
}