Passed
Pull Request — master (#7)
by Jeff
03:29
created

DKIMController::recreateAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 23
rs 9.7998
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\Service\DKIM\KeyGenerationService;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
16
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\Routing\Annotation\Route;
20
21
/**
22
 * @Route("/dkim")
23
 */
24
class DKIMController extends AbstractController
25
{
26
    private KeyGenerationService $keyGenerationService;
27
28
    public function __construct(KeyGenerationService $keyGenerationService)
29
    {
30
        $this->keyGenerationService = $keyGenerationService;
31
    }
32
33
    /**
34
     * @Route("/recreate", name="app_dkim_recreate")
35
     * @Security("has_role('ROLE_ADMIN')")
36
     */
37
    public function recreateAction(Request $request): Response
38
    {
39
        $em = $this->getDoctrine()->getManager();
40
        $repository = $this->getDoctrine()->getRepository(Domain::class);
41
42
        $id = $request->query->get('id');
43
        $domain = $repository->find($id);
44
45
        if (null === $domain) {
46
            $this->createNotFoundException(sprintf('Domain %d not found', $id));
47
        }
48
49
        $keyPair = $this->keyGenerationService->createKeyPair();
50
        $domain->setDkimPrivateKey($keyPair->getPrivate());
51
52
        $em->flush();
53
54
        $this->addFlash('info', 'Private key successfully recreated. You need to update your DNS zone now.');
55
56
        return $this->redirectToRoute('easyadmin', [
57
            'action' => 'edit',
58
            'id' => $id,
59
            'entity' => $request->query->get('entity'),
60
        ]);
61
    }
62
}
63