CommentController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 93
Duplicated Lines 18.28 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 12
dl 17
loc 93
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 17 17 1
A post() 0 21 2
A edit() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Presentation\Web\Core\Component\Blog\User\Comment;
16
17
use Acme\App\Core\Component\Blog\Application\Repository\PostRepositoryInterface;
18
use Acme\App\Core\Component\Blog\Application\Service\CommentService;
19
use Acme\App\Core\Component\Blog\Domain\Post\Comment\Comment;
20
use Acme\App\Core\Component\Blog\Domain\Post\PostId;
21
use Acme\App\Core\Port\Auth\Authentication\AuthenticationServiceInterface;
22
use Acme\App\Core\Port\Auth\Authorization\AuthorizationServiceInterface;
23
use Acme\App\Core\Port\TemplateEngine\TemplateEngineInterface;
24
use Acme\App\Presentation\Web\Core\Port\Form\FormFactoryInterface;
25
use Acme\App\Presentation\Web\Core\Port\Response\ResponseFactoryInterface;
26
use Psr\Http\Message\ResponseInterface;
27
use Psr\Http\Message\ServerRequestInterface;
28
29
/**
30
 * Controller used to manage blog contents in the public part of the site.
31
 *
32
 * @author Ryan Weaver <[email protected]>
33
 * @author Javier Eguiluz <[email protected]>
34
 * @author Herberto Graca <[email protected]>
35
 */
36
class CommentController
37
{
38
    /**
39
     * @var CommentService
40
     */
41
    private $commentService;
42
43
    /**
44
     * @var TemplateEngineInterface
45
     */
46
    private $templateEngine;
47
48
    /**
49
     * @var ResponseFactoryInterface
50
     */
51
    private $responseFactory;
52
53
    /**
54
     * @var FormFactoryInterface
55
     */
56
    private $formFactory;
57
58
    /**
59
     * @var PostRepositoryInterface
60
     */
61
    private $postRepository;
62
63
    /**
64
     * @var AuthorizationServiceInterface
65
     */
66
    private $authorizationService;
67
68
    /**
69
     * @var AuthenticationServiceInterface
70
     */
71
    private $authenticationService;
72
73 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
        CommentService $commentService,
75
        TemplateEngineInterface $templateEngine,
76
        ResponseFactoryInterface $responseFactory,
77
        FormFactoryInterface $formFactory,
78
        PostRepositoryInterface $postRepository,
79
        AuthorizationServiceInterface $authorizationService,
80
        AuthenticationServiceInterface $authenticationService
81
    ) {
82
        $this->commentService = $commentService;
83
        $this->templateEngine = $templateEngine;
84
        $this->responseFactory = $responseFactory;
85
        $this->formFactory = $formFactory;
86
        $this->postRepository = $postRepository;
87
        $this->authorizationService = $authorizationService;
88
        $this->authenticationService = $authenticationService;
89
    }
90
91
    public function post(ServerRequestInterface $request): ResponseInterface
92
    {
93
        $this->authorizationService->denyAccessUnlessGranted([AuthorizationServiceInterface::ROLE_AUTHENTICATED]);
94
95
        $post = $this->postRepository->findBySlug($request->getAttribute('postSlug'));
96
        $comment = new Comment();
97
98
        $form = $this->formFactory->createCommentForm($comment);
99
        $form->handleRequest($request);
100
101
        if (!$form->shouldBeProcessed()) {
102
            return $this->templateEngine->renderResponse(
103
                '@Blog/User/Comment/edit_error.html.twig',
104
                EditViewModel::fromPostAndForm($post, $form)
105
            );
106
        }
107
108
        $this->commentService->create($post, $comment, $this->authenticationService->getLoggedInUserId());
109
110
        return $this->responseFactory->redirectToRoute('post', ['slug' => $post->getSlug()]);
111
    }
112
113
    /**
114
     * This controller is called directly via the render() function in the
115
     * Blog/Anonymous/Post/get.html.twig template. That's why it's not needed to define
116
     * a route name for it.
117
     */
118
    public function edit(PostId $postId): ResponseInterface
119
    {
120
        return $this->templateEngine->renderResponse(
121
            '@Blog/User/Comment/edit.html.twig',
122
            EditViewModel::fromPostAndForm(
123
                $this->postRepository->find($postId),
124
                $this->formFactory->createCommentForm()
125
            )
126
        );
127
    }
128
}
129