Passed
Push — master ( 229462...d74785 )
by Andreas
02:20 queued 14s
created

LazySchemaDiffProvider::createFromSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Provider;
6
7
use Doctrine\DBAL\Schema\Schema;
8
use ProxyManager\Configuration;
9
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
10
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
11
use ProxyManager\Proxy\LazyLoadingInterface;
12
13
/**
14
 * The LazySchemaDiffProvider is responsible for lazily generating the from schema when diffing two schemas
15
 * to produce a migration.
16
 *
17
 * @internal
18
 */
19
class LazySchemaDiffProvider implements SchemaDiffProvider
20
{
21
    /** @var LazyLoadingValueHolderFactory */
22
    private $proxyFactory;
23
24
    /** @var SchemaDiffProvider */
25
    private $originalSchemaManipulator;
26
27
    public function __construct(
28
        LazyLoadingValueHolderFactory $proxyFactory,
29
        SchemaDiffProvider $originalSchemaManipulator
30
    ) {
31
        $this->proxyFactory              = $proxyFactory;
32
        $this->originalSchemaManipulator = $originalSchemaManipulator;
33
    }
34
35
    public static function fromDefaultProxyFactoryConfiguration(
36
        SchemaDiffProvider $originalSchemaManipulator
37
    ) : LazySchemaDiffProvider {
38
        $proxyConfig = new Configuration();
39
        $proxyConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
40
        $proxyFactory = new LazyLoadingValueHolderFactory($proxyConfig);
41
42
        return new LazySchemaDiffProvider($proxyFactory, $originalSchemaManipulator);
43
    }
44
45
    public function createFromSchema() : Schema
46
    {
47
        $originalSchemaManipulator = $this->originalSchemaManipulator;
48
49
        return $this->proxyFactory->createProxy(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxyFacto...ion(...) { /* ... */ }) returns the type ProxyManager\Proxy\VirtualProxyInterface which is incompatible with the type-hinted return Doctrine\DBAL\Schema\Schema.
Loading history...
50
            Schema::class,
51
            static function (&$wrappedObject, $proxy, $method, array $parameters, &$initializer) use ($originalSchemaManipulator) {
52
                $initializer = null;
53
54
                $wrappedObject = $originalSchemaManipulator->createFromSchema();
55
56
                return true;
57
            }
58
        );
59
    }
60
61
    public function createToSchema(Schema $fromSchema) : Schema
62
    {
63
        $originalSchemaManipulator = $this->originalSchemaManipulator;
64
65
        if ($fromSchema instanceof LazyLoadingInterface && ! $fromSchema->isProxyInitialized()) {
66
            return $this->proxyFactory->createProxy(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxyFacto...ion(...) { /* ... */ }) returns the type ProxyManager\Proxy\VirtualProxyInterface which is incompatible with the type-hinted return Doctrine\DBAL\Schema\Schema.
Loading history...
67
                Schema::class,
68
                static function (&$wrappedObject, $proxy, $method, array $parameters, &$initializer) use ($originalSchemaManipulator, $fromSchema) {
69
                    $initializer = null;
70
71
                    $wrappedObject = $originalSchemaManipulator->createToSchema($fromSchema);
72
73
                    return true;
74
                }
75
            );
76
        }
77
78
        return $this->originalSchemaManipulator->createToSchema($fromSchema);
79
    }
80
81
    /** @return string[] */
82
    public function getSqlDiffToMigrate(Schema $fromSchema, Schema $toSchema) : array
83
    {
84
        if ($toSchema instanceof LazyLoadingInterface
85
            && ! $toSchema->isProxyInitialized()) {
86
            return [];
87
        }
88
89
        return $this->originalSchemaManipulator->getSqlDiffToMigrate($fromSchema, $toSchema);
90
    }
91
}
92