FlashMessage::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Session;
6
7
final class FlashMessage implements \JsonSerializable
8
{
9
    /**
10
     * @var string
11
     */
12
    private $type;
13
14
    const TYPE_PRIMARY = 'p';
15
    const TYPE_SUCCESS = 's';
16
    const TYPE_INFO = 'i';
17
    const TYPE_WARNING = 'w';
18
    const TYPE_DANGER = 'd';
19
20
    /**
21
     * @var string
22
     */
23
    private $longType;
24
25
    /**
26
     * @var string
27
     */
28
    private $message;
29
30
    const JSON_KEY_TYPE = 't';
31
    const JSON_KEY_MESSAGE = 'm';
32
33
    /**
34
     * @param string $type
35
     * @param string $message
36
     */
37 6
    public function __construct(string $type, string $message)
38
    {
39 6
        $this->type = $type;
40 6
        $this->longType = $this->getLongTypeFromType($type);
41 6
        $this->message = $message;
42 6
    }
43
44
    /**
45
     * @param string $type
46
     *
47
     * @return string
48
     */
49 6
    private function getLongTypeFromType(string $type): string
50
    {
51 6
        $reflection = new \ReflectionObject($this);
52 6
        foreach ($reflection->getConstants() as $const => $value) {
53 6
            if (0 === strpos($const, 'TYPE_')) {
54 6
                if ($type === $value) {
55 6
                    return strtolower(substr($const, 5));
56
                }
57
            }
58
        }
59
60 1
        return 'info';
61
    }
62
63
    /**
64
     * @return string
65
     */
66 6
    public function getType(): string
67
    {
68 6
        return $this->type;
69
    }
70
71
    /**
72
     * @return string
73
     */
74 6
    public function getLongType(): string
75
    {
76 6
        return $this->longType;
77
    }
78
79
    /**
80
     * @return string
81
     */
82 6
    public function getMessage(): string
83
    {
84 6
        return $this->message;
85
    }
86
87
    /**
88
     * @return array
89
     */
90 6
    public function jsonSerialize()
91
    {
92
        return [
93 6
            self::JSON_KEY_TYPE => $this->type,
94 6
            self::JSON_KEY_MESSAGE => $this->message,
95
        ];
96
    }
97
}
98