Passed
Push — master ( cedee4...842d19 )
by Gerrit
02:42
created

RDMBlackMagickCacheWarmer::warmUp()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 59
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 7
eloc 30
c 2
b 1
f 1
nc 16
nop 1
dl 0
loc 59
rs 8.5066

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
 * Copyright (C) 2019 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\RDMBundle\Symfony;
14
15
use Addiks\RDMBundle\DataLoader\BlackMagic\BlackMagicEntityCodeGenerator;
16
use Doctrine\Persistence\Mapping\Driver\MappingDriver as DoctrineMappingDriver;
17
use Composer\Autoload\ClassLoader;
18
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface as RDMMappingDriver;
19
use Addiks\RDMBundle\Mapping\EntityMappingInterface;
20
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
21
use Addiks\RDMBundle\AddiksRDMBundle;
22
use Addiks\RDMBundle\DataLoader\BlackMagic\BlackMagicDataLoader;
23
24
final class RDMBlackMagickCacheWarmer implements CacheWarmerInterface
25
{
26
27
    public function __construct(
28
        private DoctrineMappingDriver $doctrineMappingDriver,
29
        private RDMMappingDriver $rdmMappingDriver,
30
        private BlackMagicDataLoader $dataLoader,
31
        public readonly string $folderNameInCache = 'symfony_rdm_entities',
32
        public readonly string|null $projectRootFolder = null
33
    ) {
34
        if (!empty($this->projectRootFolder)) {
35
            $this->projectRootFolder = realpath($projectRootFolder) . '/';
0 ignored issues
show
Bug introduced by
It seems like $projectRootFolder can also be of type null; however, parameter $path of realpath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

35
            $this->projectRootFolder = realpath(/** @scrutinizer ignore-type */ $projectRootFolder) . '/';
Loading history...
Bug introduced by
The property projectRootFolder is declared read-only in Addiks\RDMBundle\Symfony\RDMBlackMagickCacheWarmer.
Loading history...
36
        }
37
    }
38
    
39
    public function warmUp($cacheDirectory): array
40
    {
41
        /** @var mixed $entitiesFolder */
42
        $entitiesFolder = sprintf(
43
            '%s/%s',
44
            $cacheDirectory,
45
            $this->folderNameInCache
46
        );
47
        
48
        $codeGenerator = new BlackMagicEntityCodeGenerator(
49
            $entitiesFolder,
50
            $this->dataLoader
51
        );
52
        
53
        /** @var array<int, array{0:string, 1:string> $classmap */
54
        $classmap = array();
55
        
56
        foreach ($this->doctrineMappingDriver->getAllClassNames() as $entityClass) {
57
            
58
            /** @var EntityMappingInterface|null $mapping */
59
            $mapping = $this->rdmMappingDriver->loadRDMMetadataForClass($entityClass);
60
            
61
            if (is_object($mapping)) {
62
                /** @var string|null $processedEntityFilePath */
63
                $processedEntityFilePath = $codeGenerator->processMapping(
64
                    $mapping, 
65
                    AddiksRDMBundle::classLoader()
0 ignored issues
show
Bug introduced by
It seems like Addiks\RDMBundle\AddiksRDMBundle::classLoader() can also be of type null; however, parameter $loader of Addiks\RDMBundle\DataLoa...rator::processMapping() does only seem to accept Composer\Autoload\ClassLoader, maybe add an additional type check? ( Ignorable by Annotation )

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

65
                    /** @scrutinizer ignore-type */ AddiksRDMBundle::classLoader()
Loading history...
66
                );
67
68
                if (!empty($this->projectRootFolder)) {
69
                    $processedEntityFilePath = realpath($processedEntityFilePath);
0 ignored issues
show
Bug introduced by
It seems like $processedEntityFilePath can also be of type null; however, parameter $path of realpath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

69
                    $processedEntityFilePath = realpath(/** @scrutinizer ignore-type */ $processedEntityFilePath);
Loading history...
70
71
                    if (str_starts_with($processedEntityFilePath, $this->projectRootFolder)) {
72
                        $processedEntityFilePath = substr(
73
                            $processedEntityFilePath,
74
                            strlen($this->projectRootFolder)
75
                        );
76
                    }
77
                }
78
79
                if (is_string($processedEntityFilePath)) {
80
                    $classmap[] = [$entityClass, $processedEntityFilePath];
81
                }
82
            }
83
        }
84
        
85
        if (!is_dir($entitiesFolder)) {
86
            mkdir($entitiesFolder, 0777, true);
87
        }
88
        
89
        file_put_contents(
90
            $entitiesFolder . '/classmap',
91
            implode("\n", array_map(
92
                fn ($l) => $l[0] . ':' . $l[1],
93
                $classmap
94
            ))
95
        );
96
        
97
        return []; # Files to preload
98
    }
99
    
100
    public function isOptional(): bool
101
    {
102
        return false;
103
    }
104
105
}
106