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

ManagerSettings::proxiesNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
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\Infrastructure\Persistence;
13
14
use Doctrine\DBAL\Tools\DsnParser;
15
use Doctrine\ORM\Proxy\ProxyFactory;
16
17
/**
18
 * ManagerSettings
19
 *
20
 * @package Slick\Orm\Infrastructure\Persistence
21
 */
22
final class ManagerSettings
23
{
24
25
    public const ATTRIBUTE_DRIVER_IMPL = 'attribute';
26
    public const XML_DRIVER_IMPL = 'xml';
27
    private const AUTOGENERATE_DEV = 99;
28
29
    /** @var array<string, mixed> */
30
    private static array $defaultSettings = [
31
        'url' => 'sqlite3:///:memory:?cache=shared',
32
        'entityPaths' => ["/src/Domain"],
33
        'proxiesDir' => "/tmp/Proxies",
34
        'proxiesNamespace' => 'App\Persistence\Proxies',
35
        'devMode' => true,
36
        'cache' => null,
37
        'SQLLogger' => null,
38
        'autoGenerateProxiesMode' => self::AUTOGENERATE_DEV,
39
        'implDriver' => self::ATTRIBUTE_DRIVER_IMPL,
40
        'autoGenerateProxyClasses' => true,
41
    ];
42
43
    /** @var array<string, mixed>  */
44
    private array $settings;
45
46
    /**
47
     * @var array<string, mixed>
48
     */
49
    private array $connectionSettings;
50
51
    private string $proxiesDir;
52
53
    private string $proxiesNamespace;
54
55
    private bool $inDevMode;
56
57
    private ?string $cacheContainerId;
58
59
    private ?string $sqlLoggerContainerId;
60
61
    private int $generateProxiesMode;
62
63
    private string $implDriver;
64
65
    private bool $generateProxyClasses;
66
67
    /**
68
     * Constructor for the class.
69
     *
70
     * @param array<string, mixed> $settings An array of settings.
71
     */
72
    public function __construct(array $settings = [])
73
    {
74
        $this->settings = [...self::$defaultSettings, ...$settings];
75
        $dsnParser = new DsnParser();
76
        $this->connectionSettings = $dsnParser->parse($this->settings['url']);
77
        $this->proxiesDir = $this->settings['proxiesDir'];
78
        $this->proxiesNamespace = $this->settings['proxiesNamespace'];
79
        $this->inDevMode = (bool) $this->settings['devMode'];
80
        $this->cacheContainerId = $this->settings['cache'];
81
        $this->sqlLoggerContainerId = $this->settings['SQLLogger'];
82
        $this->setProxyGenerationMode();
83
84
        $this->implDriver = $this->settings['implDriver'];
85
        $this->generateProxyClasses = (bool) $this->settings['autoGenerateProxyClasses'];
86
    }
87
88
    /**
89
     * Get the entity paths to search entities for.
90
     *
91
     * @return array<string> An array of entity paths.
92
     */
93
    public function entityPaths(): array
94
    {
95
        return $this->settings['entityPaths'];
96
    }
97
98
    /**
99
     * Returns the database connection settings.
100
     *
101
     * @return array<string, mixed> The connection settings.
102
     */
103
    public function connectionSettings(): array
104
    {
105
        return $this->connectionSettings;
106
    }
107
108
    /**
109
     * Get the directory for storing proxy classes.
110
     *
111
     * @return string The directory path for proxy classes.
112
     */
113
    public function proxiesDir(): string
114
    {
115
        return $this->proxiesDir;
116
    }
117
118
    /**
119
     * Retrieves the proxies namespace.
120
     *
121
     * @return string The proxies' namespace.
122
     */
123
    public function proxiesNamespace(): string
124
    {
125
        return $this->proxiesNamespace;
126
    }
127
128
    /**
129
     * Check if the application is in development mode.
130
     *
131
     * @return bool Returns true if the application is in development mode, false otherwise.
132
     */
133
    public function isInDevMode(): bool
134
    {
135
        return $this->inDevMode;
136
    }
137
138
    /**
139
     * Returns the cache container ID.
140
     *
141
     * @return string|null The cache container ID.
142
     */
143
    public function cacheContainerId(): ?string
144
    {
145
        return $this->cacheContainerId;
146
    }
147
148
    /**
149
     * Returns the SQL Logger container ID.
150
     *
151
     * @return string|null The SQL Logger container ID.
152
     */
153
    public function sqlLoggerContainerId(): ?string
154
    {
155
        return $this->sqlLoggerContainerId;
156
    }
157
158
    /**
159
     * Returns the auto-generate proxies mode.
160
     *
161
     * @return bool|int The auto-generate proxies mode.
162
     */
163
    public function proxyGenerationMode(): bool|int
164
    {
165
        return $this->generateProxiesMode;
166
    }
167
168
    /**
169
     * Returns the implementation driver.
170
     *
171
     * @return string The implementation driver.
172
     */
173
    public function implDriver(): string
174
    {
175
        return $this->implDriver;
176
    }
177
178
    /**
179
     * Returns the flag indicating whether to auto generate proxy classes.
180
     *
181
     * @return bool The flag indicating whether to auto generate proxy classes.
182
     */
183
    public function generateProxyClasses(): bool
184
    {
185
        return $this->generateProxyClasses;
186
    }
187
188
    /**
189
     * Sets the proxy generation mode based on the application settings.
190
     */
191
    private function setProxyGenerationMode(): void
192
    {
193
        $this->generateProxiesMode = $this->settings['autoGenerateProxiesMode'];
194
        if ($this->settings['autoGenerateProxiesMode'] === self::AUTOGENERATE_DEV) {
195
            $this->generateProxiesMode = $this->inDevMode
196
                ? ProxyFactory::AUTOGENERATE_EVAL
197
                : ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS;
198
        }
199
    }
200
}
201