UrbanWordsManager   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 10
Bugs 6 Features 4
Metric Value
wmc 23
c 10
b 6
f 4
lcom 1
cbo 2
dl 0
loc 193
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getWords() 0 4 1
B addWord() 0 29 6
B readWord() 0 36 5
B updateWord() 0 22 5
B deleteWord() 0 36 5
1
<?php
2
3
namespace John\Cp;
4
5
use John\Exceptions\UrbanWordException;
6
use John\Exceptions\WordManagerException;
7
8
/**
9
 * Class handles the CRUD methods on the static $data array defined in UrbanWordsDataStore class
10
 * Class UrbanWordsManager.
11
 */
12
class UrbanWordsManager
13
{
14
    private $words;
15
    private $slang;
16
    private $desc;
17
    private $sentence;
18
19
    /**
20
     * UrbanWordsCRUD constructor.
21
     */
22
    public function __construct()
23
    {
24
        $this->words = UrbanWordsDataStore::$data;
25
    }
26
27
    /**
28
     * return Urban array of words from John\Cp\UrbanWords.
29
     *
30
     * @return array
31
     */
32
    public function getWords()
33
    {
34
        return $this->words;
35
    }
36
37
    /**
38
     * Add new word into the Urban Word dictionary
39
     *
40
     * @param string $slang
41
     * @param string $desc
42
     * @param string $sentence
43
     *
44
     * @return bool
45
     *
46
     * @throws \John\Exceptions\WordManagerException
47
     */
48
    public function addWord($slang = '', $desc = '', $sentence = '')
49
    {
50
        $this->slang = $slang;
51
        $this->desc = $desc;
52
        $this->sentence = $sentence;
53
54
        if (! empty($this->slang) && ! empty($this->desc) && ! empty($this->sentence)) {
55
56
            foreach ($this->words as $urbanWord) {
57
58
                if (strtolower($urbanWord['slang']) === strtolower($this->slang)) {
59
60
                    throw new WordManagerException('Urban word already exists.');
61
                }
62
            }
63
64
            $newWord = [
65
                'slang' => $this->slang,
66
                'description' => $this->desc,
67
                'sample-sentence' => $this->sentence,
68
            ];
69
70
            array_push($this->words, $newWord);
71
72
            return $newWord;
73
        }
74
75
        throw new WordManagerException('Urban word detail omitted.');
76
    }
77
78
    /**
79
     * Read words from the Urban Words Dictionary
80
     *
81
     * @param string $slang
82
     *
83
     * @return bool
84
     *
85
     * @throws \John\Exceptions\WordManagerException
86
     */
87
    public function readWord($slang = '')
88
    {
89
        $this->slang = $slang;
90
91
        $foundWord = [
92
            'success' => false,
93
            'key' => null,
94
        ];
95
96
        if (! empty($this->slang)) {
97
98
            foreach ($this->words as $urbanWordKey => $urbanWord) {
99
100
                if (strtolower($urbanWord['slang']) === strtolower($this->slang)) {
101
102
                    $foundWord['success'] = true;
103
                    $foundWord['key'] = $urbanWordKey;
104
105
                    break;
106
                }
107
            }
108
        } else {
109
110
            throw new WordManagerException('Urban word omitted.');
111
        }
112
113
        if ($foundWord['success']) {
114
115
            return [
116
                'word' =>  $this->words[$foundWord['key']],
117
                'position' => $foundWord['key']
118
            ];
119
        }
120
121
        throw new WordManagerException('Urban word not found in our data store.');
122
    }
123
124
    /**
125
     * Update slang Words in the Urban Dictionary
126
     *
127
     * @param string $slang
128
     * @param string $slangUpdate
129
     * @param string $descUpdate
130
     * @param string $sentenceUpdate
131
     *
132
     * @return mixed
133
     *
134
     * @throws \John\Exceptions\UrbanWordException
135
     */
136
    public function updateWord($slang = '', $slangUpdate = '', $descUpdate = '', $sentenceUpdate = '')
137
    {
138
        if (! empty($slangUpdate) && ! empty($descUpdate) && ! empty($sentenceUpdate)) {
139
140
            $this->slang = $slang;
141
            $wordKey = $this->readWord($this->slang);
142
143
            if ($wordKey) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $wordKey of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
144
145
                $position = $wordKey["position"];
146
147
                $this->words[$position]['slang'] = $slangUpdate;
148
                $this->words[$position]['description'] = $descUpdate;
149
                $this->words[$position]['sample-sentence'] = $sentenceUpdate;
150
151
                return $this->words[$position];
152
            }
153
        } else {
154
155
            throw new WordManagerException('Cannot Update: Urban word details omitted.');
156
        }
157
    }
158
159
    /**
160
     * Delete word from Urban dictionary
161
     *
162
     * @param string $slang
163
     *
164
     * @return bool
165
     *
166
     * @throws \John\Exceptions\UrbanWordException
167
     */
168
    public function deleteWord($slang = '')
169
    {
170
        $this->slang = $slang;
171
172
        $foundWord = [
173
            'success' => false,
174
            'key' => null,
175
            'urbanWord' => [],
176
        ];
177
178
        if (! empty($this->slang)) {
179
180
            foreach ($this->words as $urbanWordKey => $urbanWord) {
181
182
                if (strtolower($urbanWord['slang']) === strtolower($this->slang)) {
183
184
                    $foundWord['success'] = true;
185
                    $foundWord['key'] = $urbanWordKey;
186
                    $foundWord['urbanWord'] = $this->words[$urbanWordKey];
187
188
                    break;
189
                }
190
            }
191
        } else {
192
193
            throw new WordManagerException('Urban word omitted.');
194
        }
195
196
        if ($foundWord['success']) {
197
198
            unset($this->words[$foundWord['key']]);
199
200
            return $foundWord['urbanWord'];
201
        }
202
        throw new WordManagerException('Urban word not found in our data store.');
203
    }
204
}
205