Passed
Push — master ( 4ae095...46ef9e )
by Alexander
02:10 queued 13s
created

CycleOrmHelper::getEntityClassLocator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 11
ccs 0
cts 10
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Yiisoft\Yii\Cycle\Helper;
4
5
use Cycle\Migrations\GenerateMigrations;
6
use Cycle\Schema\Compiler;
7
use Cycle\Schema\Generator;
8
use Cycle\Schema\Registry;
9
use Spiral\Database\DatabaseManager;
10
use Spiral\Migrations\Config\MigrationConfig;
11
use Spiral\Migrations\Migrator;
12
use Psr\SimpleCache\CacheInterface;
0 ignored issues
show
Bug introduced by
The type Psr\SimpleCache\CacheInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Yiisoft\Yii\Cycle\SchemaConveyorInterface;
14
15
class CycleOrmHelper
16
{
17
    /** @var DatabaseManager $dbal */
18
    private $dbal;
19
20
    /** @var CacheInterface */
21
    private $cache;
22
23
    /** @var string */
24
    private $cacheKey = 'Cycle-ORM-Schema';
25
26
    /** @var SchemaConveyorInterface */
27
    private $schemaConveyor;
28
29
    public function __construct(
30
        DatabaseManager $dbal,
31
        CacheInterface $cache,
32
        SchemaConveyorInterface $schemaConveyor
33
    ) {
34
        $this->dbal = $dbal;
35
        $this->cache = $cache;
36
        $this->schemaConveyor = $schemaConveyor;
37
    }
38
39
    public function dropCurrentSchemaCache(): void
40
    {
41
        $this->cache->delete($this->cacheKey);
42
    }
43
44
    public function generateMigrations(Migrator $migrator, MigrationConfig $config, array $generators = []): void
45
    {
46
        // add migrations generator
47
        $migrate = new GenerateMigrations($migrator->getRepository(), $config);
48
        $this->schemaConveyor->addGenerator($this->schemaConveyor::STAGE_USERLAND, $migrate);
49
        // add custom generators
50
        foreach ($generators as $generator) {
51
            $this->schemaConveyor->addGenerator($this->schemaConveyor::STAGE_USERLAND, $generator);
52
        }
53
54
        $conveyor = $this->schemaConveyor->getGenerators();
55
56
        (new Compiler())->compile(new Registry($this->dbal), $conveyor);
57
    }
58
59
    public function getCurrentSchemaArray($fromCache = true): array
60
    {
61
        if ($fromCache) {
62
            $schema = $this->cache->get($this->cacheKey);
63
            if (is_array($schema)) {
64
                return $schema;
65
            }
66
        }
67
        // sync table changes to database
68
        $this->schemaConveyor->addGenerator($this->schemaConveyor::STAGE_RENDER, Generator\SyncTables::class);
69
        // compile schema array
70
        $conveyor = $this->schemaConveyor->getGenerators();
71
        $schema = (new Compiler())->compile(new Registry($this->dbal), $conveyor);
72
73
        $this->cache->set($this->cacheKey, $schema);
74
        return $schema;
75
    }
76
}
77