FlashService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Session;
6
7
use AbterPhp\Framework\I18n\ITranslator;
8
use AbterPhp\Framework\Session\Helper\ArrayHelper;
9
use Opulence\Sessions\ISession;
10
11
class FlashService
12
{
13
    protected const ERROR   = 'error';
14
    protected const SUCCESS = 'success';
15
16
    protected ISession $session;
17
18
    protected ?ITranslator $translator;
19
20
    /**
21
     * Helper constructor.
22
     *
23
     * @param ISession         $session
24
     * @param ITranslator|null $translator
25
     */
26
    public function __construct(ISession $session, ?ITranslator $translator)
27
    {
28
        $this->session    = $session;
29
        $this->translator = $translator;
30
    }
31
32
    /**
33
     * @param string[] $messages
34
     */
35
    public function mergeSuccessMessages(array $messages): void
36
    {
37
        $currentMessages = (array)$this->session->get(static::SUCCESS);
38
39
        $newMessages = array_merge($currentMessages, $messages);
40
41
        $this->session->flash(static::SUCCESS, $newMessages);
42
    }
43
44
    /**
45
     * @param array $messages
46
     */
47
    public function mergeErrorMessages(array $messages): void
48
    {
49
        $messages = ArrayHelper::flatten($messages);
50
51
        $currentMessages = (array)$this->session->get(static::ERROR);
52
53
        $newMessages = array_merge($currentMessages, $messages);
54
55
        $this->session->flash(static::ERROR, $newMessages);
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function retrieveSuccessMessages(): array
62
    {
63
        return (array)$this->session->get(static::SUCCESS);
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function retrieveErrorMessages(): array
70
    {
71
        return (array)$this->session->get(static::ERROR);
72
    }
73
}
74