GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#58)
by joseph
18:31
created

Schema::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 11
cp 0
cc 2
eloc 10
nc 2
nop 0
crap 6
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Schema;
4
5
use Doctrine\DBAL\DBALException;
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\Tools\SchemaTool;
8
use Doctrine\ORM\Tools\SchemaValidator;
9
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
10
11
class Schema
12
{
13
14
    /**
15
     * @var EntityManager
16
     */
17
    protected $entityManager;
18
19
    /**
20
     * @var SchemaTool
21
     */
22
    protected $schemaTool;
23
24
    /**
25
     * @var SchemaValidator
26
     */
27
    protected $schemaValidator;
28
29
    /**
30
     * SchemaBuilder constructor.
31
     *
32
     * @param EntityManager   $entityManager
33
     * @param SchemaTool      $schemaTool
34
     * @param SchemaValidator $schemaValidator
35
     */
36 40
    public function __construct(EntityManager $entityManager, SchemaTool $schemaTool, SchemaValidator $schemaValidator)
37
    {
38 40
        $this->entityManager   = $entityManager;
39 40
        $this->schemaTool      = $schemaTool;
40 40
        $this->schemaValidator = $schemaValidator;
41 40
    }
42
43
    /**
44
     * Remove all tables
45
     *
46
     * @return Schema
47
     */
48
    public function reset(): Schema
49
    {
50
        $this->schemaTool->dropDatabase();
51
52
        return $this;
53
    }
54
55
    /**
56
     * Create all tables
57
     *
58
     * @return Schema
59
     * @throws \RuntimeException
60
     * @throws DoctrineStaticMetaException
61
     */
62 39
    public function create(): Schema
63
    {
64 39
        return $this->update();
65
    }
66
67
    /**
68
     * Update or Create all tables
69
     *
70
     * @return Schema
71
     * @throws \RuntimeException
72
     * @throws DoctrineStaticMetaException
73
     */
74 39
    public function update(): Schema
75
    {
76 39
        if ('cli' !== PHP_SAPI) {
77
            throw new \RuntimeException('This should only be called from the command line');
78
        }
79 39
        $metadata        = $this->getAllMetaData();
80 39
        $schemaUpdateSql = $this->schemaTool->getUpdateSchemaSql($metadata);
81 39
        if (0 !== count($schemaUpdateSql)) {
82 39
            $connection = $this->entityManager->getConnection();
83 39
            foreach ($schemaUpdateSql as $sql) {
84
                try {
85 39
                    $connection->executeQuery($sql);
86
                } catch (DBALException $e) {
87
                    throw new DoctrineStaticMetaException(
88
                        "exception running update sql:\n$sql\n",
89
                        $e->getCode(),
90 39
                        $e
91
                    );
92
                }
93
            }
94
        }
95 39
        $this->generateProxies();
96
97 39
        return $this;
98
    }
99
100
    /**
101
     * @return Schema
102
     */
103 39
    public function generateProxies(): Schema
104
    {
105 39
        $metadata = $this->getAllMetaData();
106 39
        $this->entityManager->getProxyFactory()->generateProxyClasses($metadata);
107
108 39
        return $this;
109
    }
110
111
    /**
112
     * Get the Entity Configuration Meta Data
113
     *
114
     * @return array
115
     */
116 39
    protected function getAllMetaData(): array
117
    {
118 39
        return $this->entityManager->getMetadataFactory()->getAllMetadata();
119
    }
120
121
    /**
122
     * Validate the configured mapping metadata
123
     *
124
     * @return Schema
125
     * @throws DoctrineStaticMetaException
126
     */
127
    public function validate(): Schema
128
    {
129
        $errors = $this->schemaValidator->validateMapping();
130
        if (!empty($errors)) {
131
            $allMetaData = $this->getAllMetaData();
132
            $mappingPath = __DIR__.'/../../var/doctrineMapping.ser';
133
            file_put_contents($mappingPath, print_r($allMetaData, true));
134
            throw new DoctrineStaticMetaException(
135
                'Found errors in Doctrine mapping, mapping has been dumped to '.$mappingPath."\n\n".print_r(
136
                    $errors,
137
                    true
138
                )
139
            );
140
        }
141
142
        return $this;
143
    }
144
}
145