Completed
Push — master ( 5429ef...771e5d )
by Jeff
04:44 queued 02:38
created

DKIMController::editAction()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 4
nop 2
dl 0
loc 29
rs 9.0777
c 0
b 0
f 0
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\Controller;
12
13
use App\Entity\Domain;
14
use App\Form\DKIM\DKIMDefaultType;
15
use App\Form\DKIM\DKIMKeyGenerationType;
16
use App\Repository\DomainRepository;
17
use App\Service\DKIM\DKIMStatusService;
18
use App\Service\DKIM\FormatterService;
19
use App\Service\DKIM\KeyGenerationService;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
21
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\Routing\Annotation\Route;
24
25
/**
26
 * @Route("/DKIM")
27
 */
28
class DKIMController extends AbstractController
29
{
30
    /**
31
     * @var DKIMStatusService
32
     */
33
    private $dkimStatusService;
34
35
    /**
36
     * @var KeyGenerationService
37
     */
38
    private $keyGenerationService;
39
40
    /**
41
     * @var FormatterService
42
     */
43
    private $formatterService;
44
45
    public function __construct(
46
        DKIMStatusService $dkimStatusService,
47
        KeyGenerationService $keyGenerationService,
48
        FormatterService $formatterService
49
    ) {
50
        $this->dkimStatusService = $dkimStatusService;
51
        $this->keyGenerationService = $keyGenerationService;
52
        $this->formatterService = $formatterService;
53
    }
54
55
    /**
56
     * @Route("/", name="app_dkim_index")
57
     * @Template()
58
     */
59
    public function indexAction(DomainRepository $domainRepository): array
60
    {
61
        $domains = $domainRepository->findBy([], ['name' => 'asc']);
62
        $output = [];
63
64
        foreach ($domains as $domain) {
65
            $output[] = $this->prepareDomainView($domain);
66
        }
67
68
        return ['domains' => $output];
69
    }
70
71
    /**
72
     * @Route("/edit/{domain}", name="app_dkim_edit")
73
     * @Template()
74
     */
75
    public function editAction(Request $request, Domain $domain)
76
    {
77
        $formType = $this->getFormTypeForDomain($domain);
78
        $form = $this->createForm($formType, $domain);
79
80
        $form->handleRequest($request);
81
82
        if ($form->isSubmitted() && $form->isValid()) {
83
            if ($form->has('create_private_key') && $form->get('create_private_key')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not exist on Symfony\Component\Form\FormInterface. It seems like you code against a sub-type of Symfony\Component\Form\FormInterface such as Symfony\Component\Form\SubmitButton. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
            if ($form->has('create_private_key') && $form->get('create_private_key')->/** @scrutinizer ignore-call */ isClicked()) {
Loading history...
84
                $keyPair = $this->keyGenerationService->createKeyPair();
85
                $domain->setDkimPrivateKey($keyPair->getPrivate());
86
            }
87
88
            $this->getDoctrine()->getManager()->flush();
89
90
            return $this->redirectToRoute('app_dkim_edit', ['domain' => $domain->getId()]);
91
        }
92
93
        if (!empty($domain->getDkimPrivateKey())) {
94
            $expectedDnsRecord = $this->formatterService->getTXTRecord(
95
                $this->keyGenerationService->extractPublicKey($domain->getDkimPrivateKey()),
96
                KeyGenerationService::DIGEST_ALGORITHM
97
            );
98
        }
99
100
        return [
101
            'domain' => $this->prepareDomainView($domain),
102
            'form' => $form->createView(),
103
            'expectedDnsRecord' => $expectedDnsRecord ?? null,
104
        ];
105
    }
106
107
    private function prepareDomainView(Domain $domain): array
108
    {
109
        return [
110
            'entity' => $domain,
111
            'status' => $this->dkimStatusService->getStatus($domain),
112
        ];
113
    }
114
115
    private function getFormTypeForDomain(Domain $domain): string
116
    {
117
        $type = DKIMDefaultType::class;
118
119
        if (empty($domain->getDkimSelector()) || empty($domain->getDkimPrivateKey())) {
120
            $type = DKIMKeyGenerationType::class;
121
        }
122
123
        return $type;
124
    }
125
}
126