Completed
Push — master ( 6e739b...0f594c )
by Asmir
22s queued 12s
created

LazySchemaDiffProvider::createFromSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.0787

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 12
ccs 4
cts 7
cp 0.5714
crap 1.0787
rs 10
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 1
    public function __construct(
28
        LazyLoadingValueHolderFactory $proxyFactory,
29
        SchemaDiffProvider $originalSchemaManipulator
30
    ) {
31 1
        $this->proxyFactory              = $proxyFactory;
32 1
        $this->originalSchemaManipulator = $originalSchemaManipulator;
33 1
    }
34
35 1
    public static function fromDefaultProxyFactoryConfiguration(
36
        SchemaDiffProvider $originalSchemaManipulator
37
    ) : LazySchemaDiffProvider {
38 1
        $proxyConfig = new Configuration();
39 1
        $proxyConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
40 1
        $proxyFactory = new LazyLoadingValueHolderFactory($proxyConfig);
41
42 1
        return new LazySchemaDiffProvider($proxyFactory, $originalSchemaManipulator);
43
    }
44
45 1
    public function createFromSchema() : Schema
46
    {
47 1
        $originalSchemaManipulator = $this->originalSchemaManipulator;
48
49 1
        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 1
            Schema::class,
51
            static function (&$wrappedObject, $proxy, $method, array $parameters, &$initializer) use ($originalSchemaManipulator) : bool {
52
                $initializer = null;
53
54
                $wrappedObject = $originalSchemaManipulator->createFromSchema();
55
56
                return true;
57 1
            }
58
        );
59
    }
60
61 1
    public function createToSchema(Schema $fromSchema) : Schema
62
    {
63 1
        $originalSchemaManipulator = $this->originalSchemaManipulator;
64
65 1
        if ($fromSchema instanceof LazyLoadingInterface && ! $fromSchema->isProxyInitialized()) {
66 1
            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 1
                Schema::class,
68
                static function (&$wrappedObject, $proxy, $method, array $parameters, &$initializer) use ($originalSchemaManipulator, $fromSchema) : bool {
69
                    $initializer = null;
70
71
                    $wrappedObject = $originalSchemaManipulator->createToSchema($fromSchema);
72
73
                    return true;
74 1
                }
75
            );
76
        }
77
78
        return $this->originalSchemaManipulator->createToSchema($fromSchema);
79
    }
80
81
    /** @return string[] */
82 1
    public function getSqlDiffToMigrate(Schema $fromSchema, Schema $toSchema) : array
83
    {
84 1
        if ($toSchema instanceof LazyLoadingInterface
85 1
            && ! $toSchema->isProxyInitialized()) {
86 1
            return [];
87
        }
88
89
        return $this->originalSchemaManipulator->getSqlDiffToMigrate($fromSchema, $toSchema);
90
    }
91
}
92