Passed
Pull Request — main (#502)
by Johan
04:27 queued 02:08
created

DatabaseSchemaService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * Copyright 2025 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupGateway\Behat\Service;
20
21
use Doctrine\ORM\EntityManagerInterface;
22
use Doctrine\ORM\Tools\SchemaTool;
23
use PDO;
24
use Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory;
0 ignored issues
show
Bug introduced by
The type Surfnet\StepupGateway\Be...ect\SmoketestPdoFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
/**
27
 * Manages the database schema for smoketests.
28
 * Uses Doctrine's schema tools to generate SQL from entity mappings,
29
 * but executes via PDO with elevated privileges (deploy user).
30
 */
31
class DatabaseSchemaService
32
{
33
    private readonly PDO $connection;
34
35
    public function __construct(
36
        private readonly EntityManagerInterface $entityManager,
37
        SmoketestPdoFactory $pdoFactory,
38
    ) {
39
        $this->connection = $pdoFactory->createConnection();
0 ignored issues
show
Bug introduced by
The property connection is declared read-only in Surfnet\StepupGateway\Be...e\DatabaseSchemaService.
Loading history...
40
    }
41
42
    public function dropSchema(): void
43
    {
44
        $this->connection->exec('SET FOREIGN_KEY_CHECKS = 0');
45
46
        $stmt = $this->connection->query('SHOW TABLES');
47
        $tables = $stmt->fetchAll(PDO::FETCH_COLUMN);
48
49
        foreach ($tables as $table) {
50
            $this->connection->exec("DROP TABLE IF EXISTS `$table`");
51
        }
52
53
        $this->connection->exec('SET FOREIGN_KEY_CHECKS = 1');
54
    }
55
56
    public function createSchema(): void
57
    {
58
        $schemaTool = new SchemaTool($this->entityManager);
59
        $metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();
60
61
        $createSql = $schemaTool->getCreateSchemaSql($metadata);
62
63
        foreach ($createSql as $sql) {
64
            $this->connection->exec($sql);
65
        }
66
    }
67
68
    public function resetSchema(): void
69
    {
70
        echo "Preparing test schemas\n";
71
        $this->dropSchema();
72
        $this->createSchema();
73
    }
74
}
75
76