Passed
Push — main ( 764702...024f60 )
by Daniel
04:19
created

DatabaseValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 1
b 0
f 0
dl 0
loc 24
ccs 0
cts 8
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 17 3
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Setup\Validator;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Psr\Container\ContainerInterface;
9
use Throwable;
10
use Uxmp\Core\Component\Setup\Validator\Exception\EnvironmentValidationException;
11
12
final class DatabaseValidator implements ValidatorInterface
13
{
14
    public function __construct(
15
        private ContainerInterface $dic,
16
    ) {
17
    }
18
19
    public function validate(): void
20
    {
21
        $dsn = $_ENV['DATABASE_DSN'] ?? '';
22
23
        if ($dsn === '') {
24
            throw new EnvironmentValidationException(
25
                'Database DSN is not set in config'
26
            );
27
        }
28
29
        try {
30
            $this->dic->get(EntityManagerInterface::class)->getConnection()->connect();
31
        } catch (Throwable $e) {
32
            throw new EnvironmentValidationException(
33
                'Database connection could not be established',
34
                0,
35
                $e
36
            );
37
        }
38
    }
39
}
40