Failed Conditions
Pull Request — master (#617)
by Jonathan
03:01
created

fromDefaultProxyFacyoryConfiguration()   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
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A LazySchemaDiffProvider::fromDefaultProxyFactoryConfiguration() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\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
class LazySchemaDiffProvider implements SchemaDiffProviderInterface
14
{
15
    /** @var LazyLoadingValueHolderFactory */
16
    private $proxyFactory;
17
18
    /** @var SchemaDiffProviderInterface */
19
    private $originalSchemaManipulator;
20
21 121
    public function __construct(
22
        LazyLoadingValueHolderFactory $proxyFactory,
23
        SchemaDiffProviderInterface $originalSchemaManipulator
24
    ) {
25 121
        $this->proxyFactory              = $proxyFactory;
26 121
        $this->originalSchemaManipulator = $originalSchemaManipulator;
27 121
    }
28
29 121
    public static function fromDefaultProxyFactoryConfiguration(
30
        SchemaDiffProviderInterface $originalSchemaManipulator
31
    ) : LazySchemaDiffProvider {
32 121
        $proxyConfig = new Configuration();
33 121
        $proxyConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
34 121
        $proxyFactory = new LazyLoadingValueHolderFactory($proxyConfig);
35
36 121
        return new LazySchemaDiffProvider($proxyFactory, $originalSchemaManipulator);
37
    }
38
39 49
    public function createFromSchema() : Schema
40
    {
41 49
        $originalSchemaManipulator = $this->originalSchemaManipulator;
42
43 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...
44 49
            Schema::class,
45
            function (&$wrappedObject, $proxy, $method, array $parameters, &$initializer) use ($originalSchemaManipulator) {
46 13
                $initializer   = null;
47 13
                $wrappedObject = $originalSchemaManipulator->createFromSchema();
48
49 13
                return true;
50 49
            }
51
        );
52
    }
53
54 47
    public function createToSchema(Schema $fromSchema) : Schema
55
    {
56 47
        $originalSchemaManipulator = $this->originalSchemaManipulator;
57
58 47
        if ($fromSchema instanceof LazyLoadingInterface && ! $fromSchema->isProxyInitialized()) {
59 46
            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...
60 46
                Schema::class,
61
                function (& $wrappedObject, $proxy, $method, array $parameters, & $initializer) use ($originalSchemaManipulator, $fromSchema) {
62 12
                    $initializer   = null;
63 12
                    $wrappedObject = $originalSchemaManipulator->createToSchema($fromSchema);
64
65 12
                    return true;
66 46
                }
67
            );
68
        }
69
70 1
        return $this->originalSchemaManipulator->createToSchema($fromSchema);
71
    }
72
73
    /** @return string[] */
74 46
    public function getSqlDiffToMigrate(Schema $fromSchema, Schema $toSchema) : array
75
    {
76 46
        if ($toSchema instanceof LazyLoadingInterface
77 46
            && ! $toSchema->isProxyInitialized()) {
78 38
            return [];
79
        }
80
81 13
        return $this->originalSchemaManipulator->getSqlDiffToMigrate($fromSchema, $toSchema);
82
    }
83
}
84