Completed
Push — master ( 088dd2...f9ebf5 )
by Oleg
107:14
created

Validation::preUpdate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Db\Doctrine\Subscriber;
5
6
use Doctrine\Common\EventSubscriber;
7
use Doctrine\ORM\Event\LifecycleEventArgs;
8
use Doctrine\ORM\Event\PreUpdateEventArgs;
9
use Doctrine\ORM\Events;
10
use SlayerBirden\DataFlowServer\Db\Entities\DbConfiguration;
11
use SlayerBirden\DataFlowServer\Validation\Exception\ValidationException;
12
13
final class Validation implements EventSubscriber
14
{
15
    /**
16
     * @inheritDoc
17
     */
18 138
    public function getSubscribedEvents(): array
19
    {
20
        return [
21 138
            Events::preUpdate,
22 138
            Events::prePersist,
23
        ];
24
    }
25
26
    /**
27
     * @param PreUpdateEventArgs $args
28
     * @throws ValidationException
29
     */
30 18
    public function preUpdate(PreUpdateEventArgs $args): void
31
    {
32 18
        $entity = $args->getObject();
33 18
        if ($this->shouldHandle($entity)) {
34 4
            $this->validate($entity);
35
        }
36 18
    }
37
38
    /**
39
     * @param LifecycleEventArgs $args
40
     * @throws ValidationException
41
     */
42 38
    public function prePersist(LifecycleEventArgs $args): void
43
    {
44 38
        $entity = $args->getObject();
45 38
        if ($this->shouldHandle($entity)) {
46 8
            $this->validate($entity);
47
        }
48 36
    }
49
50
    /**
51
     * Check if object should be handled
52
     *
53
     * @param object $entity
54
     * @return bool
55
     */
56 54
    private function shouldHandle($entity): bool
57
    {
58 54
        return $entity instanceof DbConfiguration;
59
    }
60
61
    /**
62
     * @param DbConfiguration $configuration
63
     * @throws ValidationException
64
     */
65 12
    private function validate(DbConfiguration $configuration): void
66
    {
67 12
        $msg = '%s is required field if "url" is not set.';
68 12
        if (!empty($configuration->getUrl())) {
69 8
            return;
70
        }
71
72 4
        if (empty($configuration->getDbname())) {
73
            throw new ValidationException(sprintf($msg, 'dbname'));
74
        }
75 4
        if (empty($configuration->getDriver())) {
76 2
            throw new ValidationException(sprintf($msg, 'driver'));
77
        }
78 2
        if (empty($configuration->getHost())) {
79
            throw new ValidationException(sprintf($msg, 'host'));
80
        }
81 2
        if (empty($configuration->getUser())) {
82
            throw new ValidationException(sprintf($msg, 'user'));
83
        }
84 2
        if (empty($configuration->getPort())) {
85
            throw new ValidationException(sprintf($msg, 'port'));
86
        }
87
88 2
        if (empty($configuration->getTitle())) {
89
            throw new ValidationException('Title is empty.');
90
        }
91 2
    }
92
}
93