Passed
Push — master ( c79df9...45a681 )
by Enjoys
49s queued 13s
created

ManyFilesStrategy::getResult()   B

Complexity

Conditions 8
Paths 17

Size

Total Lines 63
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 8.0314

Importance

Changes 4
Bugs 2 Features 0
Metric Value
cc 8
eloc 33
c 4
b 2
f 0
nc 17
nop 0
dl 0
loc 63
ccs 35
cts 38
cp 0.9211
crap 8.0314
rs 8.1475

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Enjoys\AssetsCollector\CollectStrategy\Strategy;
4
5
use Enjoys\AssetsCollector\AssetOption;
6
use Enjoys\AssetsCollector\CollectStrategy\StrategyAbstract;
7
use Enjoys\AssetsCollector\Content\Reader;
8
use Enjoys\AssetsCollector\Helpers;
9
10
class ManyFilesStrategy extends StrategyAbstract
11
{
12
13
14
15
    /**
16
     * @return array<string, array|null>
17
     * @throws \Exception
18
     */
19 16
    public function getResult(): array
20
    {
21 16
        $cacheDir = $this->environment->getCompileDir().'/.cache';
22
23 16
        $result = [];
24
25 16
        foreach ($this->assets as $asset) {
26 14
            if (false === $path = $asset->getPath()) {
27
                continue;
28
            }
29
30 14
            if ($asset->isUrl()) {
31 7
                $result[$path] = $asset->getOptions()->getAttributes();
32 7
                continue;
33
            }
34
35 9
            $link = str_replace(
36 9
                [
37 9
                    $this->environment->getCompileDir(),
38 9
                    $this->environment->getProjectDir()
39 9
                ],
40 9
                '',
41 9
                $path
42 9
            );
43
44
            /**
45
             * TODO
46
             * @psalm-suppress PossiblyNullOperand
47
             */
48 9
            $cacheFile = $cacheDir.'/'.$asset->getId();
49 9
            if(!file_exists($cacheFile) || (filemtime($cacheFile) + $this->environment->getCacheTime()) < time()){
50 9
                (new Reader($asset, $this->environment, $this->logger))->replaceRelativeUrlsAndCreatedSymlinks();
51 9
                Helpers::createEmptyFile($cacheFile, $this->logger);
52
            }
53
54
            try {
55 9
                $asset->getOptions()->setOption(
56 9
                    AssetOption::SYMLINKS,
57 9
                    array_merge(
58 9
                        [$this->environment->getCompileDir() . $link => $path],
59 9
                        $asset->getOptions()->getSymlinks()
60 9
                    )
61 9
                );
62
63 9
                foreach ($asset->getOptions()->getSymlinks() as $optLink => $optTarget) {
64 9
                    Helpers::createSymlink($optLink, $optTarget, $this->logger);
65
                }
66
67
68
69
            } catch (\Exception  $e) {
70
                $this->logger->error($e->getMessage());
71
            }
72
73
74 9
            $result[$this->environment->getBaseUrl() . str_replace(
75 9
                DIRECTORY_SEPARATOR,
76 9
                '/',
77 9
                $link
78 9
            )] = $asset->getOptions()->getAttributes();
79
        }
80
81 16
        return $result;
82
    }
83
}
84