Completed
Pull Request — master (#141)
by Evgenij
06:31
created

DoctrineMigration::getDiagnostics()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 37
rs 8.8571
cc 3
eloc 20
nc 3
nop 0
1
<?php
2
3
namespace Liip\MonitorBundle\Check;
4
5
use Doctrine\Bundle\MigrationsBundle\Command\DoctrineCommand;
6
use Doctrine\Common\Persistence\ConnectionRegistry;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Migrations\Configuration\AbstractFileConfiguration;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use ZendDiagnostics\Check\AbstractCheck;
11
use ZendDiagnostics\Check\CheckInterface;
12
use ZendDiagnostics\Check\DoctrineMigration as ZendDoctrineMigration;
13
use ZendDiagnostics\Result\Failure;
14
15
/**
16
 * Class DoctrineMigration
17
 */
18
class DoctrineMigration extends AbstractCheck
19
{
20
    /**
21
     * Migration file
22
     *
23
     * @var string
24
     */
25
    private $file;
26
27
    /**
28
     * DB connection for migrations
29
     *
30
     * @var Connection
31
     */
32
    private $connectionName;
33
34
    /**
35
     * Original checker
36
     *
37
     * @var CheckInterface
38
     */
39
    private $checker;
40
41
    /**
42
     * ConnectionRegistry
43
     *
44
     * @var ConnectionRegistry
45
     */
46
    private $connectionRegistry;
47
48
    /**
49
     * DI container
50
     *
51
     * @var ContainerInterface
52
     */
53
    private $container;
54
55
    /**
56
     * DoctrineMigration constructor.
57
     *
58
     * @param ContainerInterface $container          Symfony DI container
59
     * @param ConnectionRegistry $connectionRegistry Doctrine connection registry
60
     * @param                    $connectionName     Connection name from doctrine config
61
     * @param string             $file Absolute path to migration file
62
     */
63
    public function __construct(
64
        ContainerInterface $container,
65
        ConnectionRegistry $connectionRegistry,
66
        $connectionName,
67
        $file
68
    ) {
69
        $this->container          = $container;
70
        $this->connectionRegistry = $connectionRegistry;
71
        $this->connectionName     = $connectionName;
72
        $this->file               = $file;
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    public function check()
79
    {
80
        try {
81
            return $this->getDiagnostics()->check();
82
        } catch (\Exception $e) {
83
            return new Failure($e->getMessage());
84
        } catch (\Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
85
            return new Failure($e->getMessage());
86
        }
87
    }
88
89
    /**
90
     * Return Zend diagnostics object
91
     *
92
     * @return CheckInterface
93
     */
94
    private function getDiagnostics()
95
    {
96
        if (!$this->checker) {
97
            /** @var Connection $connection */
98
            $connection = $this->connectionRegistry->getConnection($this->connectionName);
0 ignored issues
show
Documentation introduced by
$this->connectionName is of type object<Doctrine\DBAL\Connection>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
99
100
            // -------
101
            // This part must be in sync with Doctrine\DBAL\Migrations\Tools\Console\Helper\ConfigurationHelper::loadConfig
102
            $map = array(
103
                'xml'   => '\XmlConfiguration',
104
                'yaml'  => '\YamlConfiguration',
105
                'yml'   => '\YamlConfiguration',
106
                'php'   => '\ArrayConfiguration',
107
                'json'  => '\JsonConfiguration'
108
            );
109
110
            $info = pathinfo($this->file);
111
            // check we can support this file type
112
            if (empty($map[$info['extension']])) {
113
                throw new \InvalidArgumentException('Given config file type is not supported');
114
            }
115
116
            $class         = 'Doctrine\DBAL\Migrations\Configuration';
117
            $class        .= $map[$info['extension']];
118
            /** @var AbstractFileConfiguration $configuration */
119
            $configuration = new $class($connection);
120
            $configuration->load($this->file);
121
            DoctrineCommand::configureMigrations($this->container, $configuration);
122
            // -------
123
124
            $configuration->validate();
125
126
            $this->checker = new ZendDoctrineMigration($configuration);
127
        }
128
129
        return $this->checker;
130
    }
131
}
132