Passed
Branch entity_code_generation (017b91)
by Gerrit
14:35
created

RDMBlackMagickCacheWarmer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 0
c 1
b 1
f 1
nc 1
nop 4
dl 0
loc 6
rs 10
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
    ) {
33
    }
34
    
35
    public function warmUp($cacheDirectory): array
36
    {
37
        /** @var mixed $entitiesFolder */
38
        $entitiesFolder = sprintf(
39
            '%s/%s',
40
            $cacheDirectory,
41
            $this->folderNameInCache
42
        );
43
        
44
        $codeGenerator = new BlackMagicEntityCodeGenerator(
45
            $entitiesFolder,
46
            $this->dataLoader
47
        );
48
        
49
        /** @var array<int, array{0:string, 1:string> $classmap */
50
        $classmap = array();
51
        
52
        foreach ($this->doctrineMappingDriver->getAllClassNames() as $entityClass) {
53
            
54
            /** @var EntityMappingInterface|null $mapping */
55
            $mapping = $this->rdmMappingDriver->loadRDMMetadataForClass($entityClass);
56
            
57
            if (is_object($mapping)) {
58
                /** @var string|null $processedEntityFilePath */
59
                $processedEntityFilePath = $codeGenerator->processMapping(
60
                    $mapping, 
61
                    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

61
                    /** @scrutinizer ignore-type */ AddiksRDMBundle::classLoader()
Loading history...
62
                );
63
                
64
                if (is_string($processedEntityFilePath)) {
65
                    $classmap[] = [$entityClass, $processedEntityFilePath];
66
                }
67
            }
68
        }
69
        
70
        if (!is_dir($entitiesFolder)) {
71
            mkdir($entitiesFolder, 0777, true);
72
        }
73
        
74
        file_put_contents(
75
            $entitiesFolder . '/classmap',
76
            implode("\n", array_map(
77
                fn ($l) => $l[0] . ':' . $l[1],
78
                $classmap
79
            ))
80
        );
81
        
82
        return []; # Files to preload
83
    }
84
    
85
    public function isOptional(): bool
86
    {
87
        return false;
88
    }
89
90
}
91