Completed
Push — master ( 23e112...e26dcf )
by Roj
10:26
created

SessionTransport::currentKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Rojtjo\Notifier\Transports;
4
5
use Rojtjo\Notifier\Notification;
6
use Rojtjo\Notifier\Notifications;
7
use Rojtjo\Notifier\SessionStore;
8
use Rojtjo\Notifier\Transport;
9
10
class SessionTransport implements Transport
11
{
12
    /**
13
     * @var SessionStore
14
     */
15
    private $session;
16
17
    /**
18
     * @var string
19
     */
20
    private $key;
21
22
    /**
23
     * @param SessionStore $session
24
     * @param string $key
25
     */
26 12
    public function __construct(SessionStore $session, $key = 'notifications')
27
    {
28 12
        $this->session = $session;
29 12
        $this->key = $key;
30
31 12
        if ($this->session->has($this->newKey())) {
32 3
            $this->moveNewNotificationsToCurrent();
33 3
        }
34 12
    }
35
36
    /**
37
     * @return void
38
     */
39 3
    private function moveNewNotificationsToCurrent()
40
    {
41 3
        $notifications = $this->session->get($this->newKey());
42 3
        $this->session->put($this->currentKey(), $notifications);
43 3
        $this->session->put($this->newKey(), []);
44 3
    }
45
46
    /**
47
     * @param Notification $notification
48
     * @return void
49
     */
50 3
    public function send(Notification $notification)
51
    {
52 3
        $this->session->push($this->newKey(), [
53 3
            'type' => $notification->getType(),
54 3
            'message' => $notification->getMessage(),
55 3
        ]);
56 3
    }
57
58
    /**
59
     * @return Notifications
60
     */
61 6
    public function getCurrentNotifications()
62
    {
63 6
        $notifications = $this->session->get($this->currentKey());
64
65 6
        return Notifications::mapFromArray($notifications);
66
    }
67
68
    /**
69
     * @return string
70
     */
71 12
    private function newKey()
72
    {
73 12
        return $this->key . '.new';
74
    }
75
76
    /**
77
     * @return string
78
     */
79 6
    private function currentKey()
80
    {
81 6
        return $this->key . '.current';
82
    }
83
}
84