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
Push — master ( 647edc...edf045 )
by joseph
18s queued 16s
created

Schema::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
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 22
    public function __construct(EntityManager $entityManager, SchemaTool $schemaTool, SchemaValidator $schemaValidator)
37
    {
38 22
        $this->entityManager   = $entityManager;
39 22
        $this->schemaTool      = $schemaTool;
40 22
        $this->schemaValidator = $schemaValidator;
41 22
    }
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 20
    public function create(): Schema
63
    {
64 20
        return $this->update();
65
    }
66
67
    /**
68
     * Update or Create all tables
69
     *
70
     * @return Schema
71
     * @throws \RuntimeException
72
     * @throws DoctrineStaticMetaException
73
     */
74 20
    public function update(): Schema
75
    {
76 20
        if ('cli' !== PHP_SAPI) {
77
            throw new \RuntimeException('This should only be called from the command line');
78
        }
79 20
        $metadata        = $this->getAllMetaData();
80 20
        $schemaUpdateSql = $this->schemaTool->getUpdateSchemaSql($metadata);
81 20
        if (0 !== count($schemaUpdateSql)) {
82 20
            $connection = $this->entityManager->getConnection();
83 20
            foreach ($schemaUpdateSql as $sql) {
84
                try {
85 20
                    $connection->executeQuery($sql);
86
                } catch (DBALException $e) {
87
                    throw new DoctrineStaticMetaException(
88
                        "exception running update sql:\n$sql\n",
89
                        $e->getCode(),
90 20
                        $e
91
                    );
92
                }
93
            }
94
        }
95 20
        $this->generateProxies();
96
97 20
        return $this;
98
    }
99
100
    /**
101
     * @return Schema
102
     */
103 20
    public function generateProxies(): Schema
104
    {
105 20
        $metadata = $this->getAllMetaData();
106 20
        $this->entityManager->getProxyFactory()->generateProxyClasses($metadata);
107
108 20
        return $this;
109
    }
110
111
    /**
112
     * Get the Entity Configuration Meta Data
113
     *
114
     * @return array
115
     */
116 20
    protected function getAllMetaData(): array
117
    {
118 20
        return $this->entityManager->getMetadataFactory()->getAllMetadata();
119
    }
120
121
    /**
122
     * Validate the configured mapping metadata
123
     *
124
     * @return Schema
125
     * @throws DoctrineStaticMetaException
126
     */
127 1
    public function validate(): Schema
128
    {
129 1
        $errors = $this->schemaValidator->validateMapping();
130 1
        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 1
        return $this;
143
    }
144
}
145