Completed
Push — master ( 5d80e8...ecedb8 )
by Jeff
02:20
created

onPreUpdate()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the mailserver-admin package.
6
 * (c) Jeffrey Boehm <https://github.com/jeboehm/mailserver-admin>
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace App\Subscriber\DKIM;
12
13
use App\Entity\Domain;
14
use App\Service\DKIM\KeyGenerationService;
15
use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Symfony\Component\EventDispatcher\GenericEvent;
18
19
class CreatePrivateKeyOnActivationSubscriber implements EventSubscriberInterface
20
{
21
    private KeyGenerationService $keyGenerationService;
22
23
    public function __construct(KeyGenerationService $keyGenerationService)
24
    {
25
        $this->keyGenerationService = $keyGenerationService;
26
    }
27
28
    public static function getSubscribedEvents(): array
29
    {
30
        return [
31
            EasyAdminEvents::PRE_UPDATE => 'onPreUpdate',
32
        ];
33
    }
34
35
    public function onPreUpdate(GenericEvent $event): void
36
    {
37
        $entity = $event->getArgument('entity');
38
39
        if (!($entity instanceof Domain)) {
40
            return;
41
        }
42
43
        if ('' === $entity->getDkimPrivateKey()) {
44
            $entity->setDkimPrivateKey($this->keyGenerationService->createKeyPair()->getPrivate());
45
        }
46
47
        if ('' === $entity->getDkimSelector()) {
48
            $entity->setDkimSelector(date('Y'));
49
        }
50
    }
51
}
52