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

MessageHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
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