Notification::addData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Level51\OneSignal;
4
5
class Notification
6
{
7
8
    const DEFAULT_SEGMENT = 'All';
9
10
    /**
11
     * @see https://documentation.onesignal.com/docs/language-localization#what-languages-are-supported
12
     */
13
    const SUPPORTED_LOCALES = [
14
        'en',
15
        'ar',
16
        'ca',
17
        'zh-Hans',
18
        'zh-Hant',
19
        'hr',
20
        'cs',
21
        'da',
22
        'nl',
23
        'et',
24
        'fi',
25
        'fr',
26
        'ka',
27
        'bg',
28
        'de',
29
        'el',
30
        'hi',
31
        'he',
32
        'hu',
33
        'id',
34
        'it',
35
        'ja',
36
        'ko',
37
        'lv',
38
        'lt',
39
        'ms',
40
        'nb',
41
        'fa',
42
        'pl',
43
        'pt',
44
        'ro',
45
        'ru',
46
        'sr',
47
        'sk',
48
        'es',
49
        'sv',
50
        'th',
51
        'tr',
52
        'uk',
53
        'vi'
54
    ];
55
56
    private $segments;
57
58
    private $contents;
59
60
    private $headings;
61
62
    private $data;
63
64 9
    public function __construct()
65
    {
66 9
        $this->segments = [self::DEFAULT_SEGMENT];
67 9
        $this->contents = [];
68 9
        $this->headings = [];
69 9
        $this->data = [];
70 9
    }
71
72 9
    public static function create()
73
    {
74 9
        return new self();
75
    }
76
77 4
    private function isSupportedLocale($locale)
78
    {
79 4
        return in_array($locale, self::SUPPORTED_LOCALES);
80
    }
81
82
    /**
83
     * @param $locale
84
     * @param $content
85
     *
86
     * @return $this
87
     * @throws OneSignalException
88
     */
89 2
    public function addContent($locale, $content)
90
    {
91 2
        if (!$this->isSupportedLocale($locale)) {
92 1
            throw new OneSignalException("'$locale' is not a supported locale.");
93
        }
94
95 1
        $this->contents[$locale] = $content;
96
97 1
        return $this;
98
    }
99
100
    /**
101
     * @param $locale
102
     * @param $heading
103
     *
104
     * @return $this
105
     * @throws OneSignalException
106
     */
107 2
    public function addHeading($locale, $heading)
108
    {
109 2
        if (!$this->isSupportedLocale($locale)) {
110 1
            throw new OneSignalException("'$locale' is not a supported locale.");
111
        }
112
113 1
        $this->headings[$locale] = $heading;
114
115 1
        return $this;
116
    }
117
118 1
    public function addData($key, $value)
119
    {
120 1
        $this->data[$key] = $value;
121
122 1
        return $this;
123
    }
124
125 1
    public function forSegment($segment)
126
    {
127 1
        $this->segments = [$segment];
128
129 1
        return $this;
130
    }
131
132 6
    public function toArray()
133
    {
134
        return [
135 6
            'included_segments' => $this->segments,
136 6
            'contents'          => $this->contents,
137 6
            'headings'          => $this->headings,
138 6
            'data'              => $this->data
139
        ];
140
    }
141
}
142