Completed
Branch dev (a083dd)
by Arnaud
03:08
created

MessageHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 35.62 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 61.9%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 4
dl 26
loc 73
ccs 13
cts 21
cp 0.619
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handleError() 13 13 2
A handleSuccess() 13 13 2

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
namespace LAG\AdminBundle\Message;
4
5
use Psr\Log\LoggerInterface;
6
use Symfony\Component\HttpFoundation\Session\Session;
7
use Symfony\Component\Translation\TranslatorInterface;
8
9
class MessageHandler implements MessageHandlerInterface
10
{
11
    /**
12
     * @var LoggerInterface
13
     */
14
    protected $logger;
15
16
    /**
17
     * @var Session
18
     */
19
    protected $session;
20
21
    /**
22
     * @var TranslatorInterface
23
     */
24
    protected $translator;
25
26
    /**
27
     * ErrorHandler constructor
28
     *
29
     * @param LoggerInterface $logger
30
     * @param Session $session
31
     * @param TranslatorInterface $translator
32
     */
33 1
    public function __construct(LoggerInterface $logger, Session $session, TranslatorInterface $translator)
34
    {
35 1
        $this->logger = $logger;
36 1
        $this->session = $session;
37 1
        $this->translator = $translator;
38 1
    }
39
40
    /**
41
     * Create an flash message into the session. It will be translated. If $logMessage is defined, it will be logged
42
     * using the logger using the error channel
43
     *
44
     * @param string $flashMessage
45
     * @param string|null $logMessage
46
     */
47 1 View Code Duplication
    public function handleError($flashMessage, $logMessage = null)
48
    {
49
        $this
50 1
            ->session
51 1
            ->getFlashBag()
52 1
            ->add('error', $this->translator->trans($flashMessage));
53
54 1
        if ($logMessage) {
55
            $this
56 1
                ->logger
57 1
                ->error($logMessage);
58
        }
59 1
    }
60
61
    /**
62
     * Create an flash message into the session. It will be translated. If $logMessage is defined, it will be logged
63
     * using the logger using info channel
64
     *
65
     * @param $flashMessage
66
     * @param null $logMessage
67
     */
68 View Code Duplication
    public function handleSuccess($flashMessage, $logMessage = null)
69
    {
70
        $this
71
            ->session
72
            ->getFlashBag()
73
            ->add('info', $this->translator->trans($flashMessage));
74
75
        if ($logMessage) {
76
            $this
77
                ->logger
78
                ->info($logMessage);
79
        }
80
    }
81
}
82