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

DomainInfoSubscriber::onPostList()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 17
rs 9.9666
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\DKIMStatusService;
15
use App\Service\DKIM\FormatterService;
16
use App\Service\DKIM\KeyGenerationService;
17
use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
18
use Pagerfanta\Pagerfanta;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use Symfony\Component\EventDispatcher\GenericEvent;
21
use Symfony\Component\HttpFoundation\Request;
22
23
class DomainInfoSubscriber implements EventSubscriberInterface
24
{
25
    private DKIMStatusService $dkimStatusService;
26
    private FormatterService $formatterService;
27
    private KeyGenerationService $keyGenerationService;
28
29
    public function __construct(DKIMStatusService $dkimStatusService, FormatterService $formatterService, KeyGenerationService $keyGenerationService)
30
    {
31
        $this->dkimStatusService = $dkimStatusService;
32
        $this->formatterService = $formatterService;
33
        $this->keyGenerationService = $keyGenerationService;
34
    }
35
36
    public static function getSubscribedEvents(): array
37
    {
38
        return [
39
            EasyAdminEvents::POST_LIST => 'onPostList',
40
            EasyAdminEvents::POST_EDIT => 'onPostEdit',
41
        ];
42
    }
43
44
    public function onPostList(GenericEvent $event): void
45
    {
46
        if ('DKIM' !== $event->getArgument('entity')['name']) {
47
            return;
48
        }
49
50
        /** @var Pagerfanta $paginator */
51
        $paginator = $event->getArgument('paginator');
52
        $entities = $paginator->getIterator();
53
54
        foreach ($entities as $entity) {
55
            if (!($entity instanceof Domain)) {
56
                throw new \DomainException('Wrong entity type');
57
            }
58
59
            $status = $this->dkimStatusService->getStatus($entity);
60
            $entity->setDkimStatus($status);
61
        }
62
    }
63
64
    public function onPostEdit(GenericEvent $event): void
65
    {
66
        if ('DKIM' !== $event->getArgument('entity')['name']) {
67
            return;
68
        }
69
70
        /** @var Request $request */
71
        $request = $event->getArgument('request');
72
        $item = $request->attributes->get('easyadmin')['item'];
73
74
        if (!($item instanceof Domain)) {
75
            return;
76
        }
77
78
        $status = $this->dkimStatusService->getStatus($item);
79
        $item->setDkimStatus($status);
80
81
        if ('' !== $item->getDkimPrivateKey()) {
82
            $expectedRecord = $this->formatterService->getTXTRecord(
83
                $this->keyGenerationService->extractPublicKey($item->getDkimPrivateKey()),
84
                KeyGenerationService::DIGEST_ALGORITHM
85
            );
86
            $item->setExpectedDnsRecord(wordwrap($expectedRecord, 40, "\n", true));
87
            $item->setCurrentDnsRecord(wordwrap($status->getCurrentRecord(), 40, "\n", true));
88
        }
89
    }
90
}
91