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

DKIMController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 36
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A recreateAction() 0 23 2
A __construct() 0 3 1
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