Passed
Push — master ( 06873a...27252d )
by Filipe
02:08 queued 13s
created

OrmModule::description()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
nc 1
nop 0
dl 0
loc 4
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
/**
4
 * This file is part of orm
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\Orm;
13
14
use Dotenv\Dotenv;
15
use JsonException;
16
use Slick\ModuleApi\Infrastructure\AbstractModule;
17
use Slick\ModuleApi\Infrastructure\Console\ConsoleModuleInterface;
18
use Slick\ModuleApi\Infrastructure\FrontController\MiddlewareHandler;
19
use Slick\ModuleApi\Infrastructure\FrontController\MiddlewareHandlerInterface;
20
use Slick\ModuleApi\Infrastructure\FrontController\MiddlewarePosition;
0 ignored issues
show
Bug introduced by
The type Slick\ModuleApi\Infrastr...ller\MiddlewarePosition 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...
21
use Slick\ModuleApi\Infrastructure\FrontController\Position;
22
use Slick\ModuleApi\Infrastructure\FrontController\WebModuleInterface;
23
use Slick\Orm\Infrastructure\Http\EntityManagerFlushMiddleware;
0 ignored issues
show
Bug introduced by
The type Slick\Orm\Infrastructure...yManagerFlushMiddleware 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...
24
use Slick\WebStack\Infrastructure\ComposerParser;
25
use Symfony\Component\Console\Application;
26
use function Slick\WebStack\importSettingsFile;
27
28
/**
29
 * OrmModule
30
 *
31
 * @package Slick\Orm
32
 *
33
 */
34
final class OrmModule extends AbstractModule implements ConsoleModuleInterface, WebModuleInterface
35
{
36
37
    /** @var array<string, mixed>  */
38
    private static array $defaultSettings = [
39
        'table_storage' => [
40
            'table_name' => 'doctrine_migration_versions',
41
            'version_column_name' => 'version',
42
            'version_column_length' => 192,
43
            'executed_at_column_name' => 'executed_at',
44
            'execution_time_column_name' => 'execution_time',
45
        ],
46
47
        'migrations_paths' => null,
48
49
        'all_or_nothing' => true,
50
        'transactional' => true,
51
        'check_database_platform' => true,
52
        'organize_migrations' => 'none',
53
        'connection' => null,
54
        'em' => null,
55
    ];
56
57
    private static string $appConfig = APP_ROOT . '/config';
58
    private static string $migrationCnfFile = APP_ROOT . '/config/migrations.json';
59
60
    private ComposerParser $composerParser;
61
62
    /**
63
     * @throws JsonException
64
     */
65
    public function __construct()
66
    {
67
        $this->composerParser = new ComposerParser(APP_ROOT . "/composer.json");
68
    }
69
70
    public function description(): ?string
71
    {
72
        return "This module offers Migrations, Database Abstraction (DBA), and Object-Relational Mapping (ORM) ".
73
            "features utilizing the doctrine/migrations and doctrine/orm packages.";
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function onEnable(array $context = []): void
80
    {
81
        if (is_file(self::$migrationCnfFile)) {
82
            return;
83
        }
84
85
        $settings = self::$defaultSettings;
86
        $namespace = '';
87
        $namespaces = $this->composerParser->psr4Namespaces();
88
        if (!empty($namespaces)) {
89
            $namespace = reset($namespaces);
90
        }
91
92
        $settings['migrations_paths'] = (object) ["{$namespace}Migrations" => '../lib/Migrations'];
93
        if (!is_dir(self::$appConfig)) {
94
            mkdir(self::$appConfig, 0755, true);
95
        }
96
97
        if (!is_dir(APP_ROOT . '/lib/Migrations')) {
98
            mkdir(APP_ROOT . '/lib/Migrations', 0755, true);
99
        }
100
101
        file_put_contents(self::$migrationCnfFile, json_encode($settings, JSON_PRETTY_PRINT));
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function onDisable(array $context = []): void
108
    {
109
        if (!$context['purge']) {
110
            return;
111
        }
112
113
        if (is_file(self::$migrationCnfFile)) {
114
            unlink(self::$migrationCnfFile);
115
        }
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121
    public function settings(Dotenv $dotenv): array
122
    {
123
        $settingsFile = APP_ROOT .'/config/modules/orm.php';
124
        $defaultSettings = [
125
            'databases' => [
126
                'default' => [
127
                    'url' => 'pdo-sqlite:///:memory:'
128
                ]
129
            ]
130
        ];
131
        return importSettingsFile($settingsFile, $defaultSettings);
132
    }
133
134
    public function services(): array
135
    {
136
        return importSettingsFile(dirname(__DIR__) . '/config/services.php');
137
    }
138
139
    /**
140
     * @inheritdoc
141
     * @SuppressWarnings(PHPMD.StaticAccess)
142
     */
143
    public function configureConsole(Application $cli): void
144
    {
145
        $configure = require dirname(__DIR__) . '/config/console.php';
146
        $configure($cli, self::$migrationCnfFile);
147
    }
148
149
150
    /**
151
     * Retrieve the middleware handlers for the application.
152
     *
153
     * @return array<MiddlewareHandlerInterface> The middleware handlers.
154
     */
155
    public function middlewareHandlers(): array
156
    {
157
        return [
158
            new MiddlewareHandler(
159
                'orm-flush',
160
                new MiddlewarePosition(Position::Top),
161
                EntityManagerFlushMiddleware::class
162
            )
163
        ];
164
    }
165
}
166