Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 122 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
WPSService | |
0.00% |
0 / 122 |
|
0.00% |
0 / 5 |
342 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
createUserDictionary | |
0.00% |
0 / 58 |
|
0.00% |
0 / 1 |
42 | |||
getUserDictionary | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
addWordToUserDictionary | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
20 | |||
removeWordToUserDictionary | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | namespace App\Http\Services; |
4 | |
5 | use Illuminate\Support\Str; |
6 | use App\Http\Models\Auth\User; |
7 | use App\Http\Models\FlyMsgAI\UserDictionary; |
8 | use Illuminate\Support\Facades\Http; |
9 | use Illuminate\Support\Facades\Log; |
10 | |
11 | class WPSService |
12 | { |
13 | private $url; |
14 | private $serviceId; |
15 | private $auth; |
16 | |
17 | public function __construct() |
18 | { |
19 | $this->url = "https://svc.webspellchecker.net/api?"; |
20 | $this->serviceId = 'A3D30k0lIyTLTm5'; |
21 | $this->auth = 'iHzB4vayxZTNOez4V9rc79FTXnH47mrYJWRTZB2x'; |
22 | } |
23 | |
24 | public function createUserDictionary(User $user, string $word) |
25 | { |
26 | $wordList = [$word]; |
27 | |
28 | $dictionaryName = Str::slug($user->email); |
29 | $dictionaryName = Str::replace('-', '_', $dictionaryName); |
30 | $createDictionary = true; |
31 | $response = Http::withHeaders([ |
32 | 'Authorization' => $this->auth, |
33 | ])->post($this->url, [ |
34 | 'format' => 'json', |
35 | 'customerid' => $this->serviceId, |
36 | 'cmd' => 'user_dictionary', |
37 | 'action' => 'check', |
38 | 'name' => $dictionaryName, |
39 | ]); |
40 | |
41 | if (!$response->failed()) { |
42 | $createDictionary = false; |
43 | |
44 | $result = $response->json(); |
45 | |
46 | $response = Http::withHeaders([ |
47 | 'Authorization' => $this->auth, |
48 | ])->post($this->url, [ |
49 | 'format' => 'json', |
50 | 'customerid' => $this->serviceId, |
51 | 'cmd' => 'user_dictionary', |
52 | 'action' => 'getdict', |
53 | 'name' => $dictionaryName, |
54 | ]); |
55 | |
56 | if ($response->failed()) { |
57 | return false; |
58 | } |
59 | $result = $response->json(); |
60 | $wordList = $result['wordlist'] ?? []; |
61 | |
62 | if (!in_array($word, $wordList)) { |
63 | $wordList[] = $word; |
64 | } |
65 | } |
66 | |
67 | if ($createDictionary) { |
68 | $response = Http::withHeaders([ |
69 | 'Authorization' => $this->auth, |
70 | ])->post($this->url . 'action=createUserDictionary', [ |
71 | 'format' => 'json', |
72 | 'customerid' => $this->serviceId, |
73 | 'cmd' => 'user_dictionary', |
74 | 'action' => 'create', |
75 | 'name' => $dictionaryName, |
76 | 'wordlist' => 'FlyMSG,Vengreso,' . $word, |
77 | ]); |
78 | |
79 | if ($response->failed()) { |
80 | Log::error('Failed to create user dictionary', [ |
81 | 'userId' => $user->id, |
82 | 'response' => $response->body(), |
83 | 'dictionaryName' => $dictionaryName, |
84 | 'wordList' => $wordList, |
85 | 'customerid' => $this->serviceId, |
86 | 'word' => $word |
87 | ]); |
88 | return false; |
89 | } |
90 | } |
91 | |
92 | UserDictionary::updateOrCreate( |
93 | ['user_id' => $user->id, 'name' => $dictionaryName], |
94 | ['words' => $wordList] |
95 | ); |
96 | |
97 | $result = $response->json(); |
98 | |
99 | return $result; |
100 | } |
101 | |
102 | public function getUserDictionary(User $user) |
103 | { |
104 | $userDictionary = UserDictionary::where('user_id', $user->id) |
105 | ->first(); |
106 | |
107 | if (!$userDictionary) { |
108 | $this->createUserDictionary($user, 'Vengreso'); |
109 | |
110 | $userDictionary = UserDictionary::where('user_id', $user->id) |
111 | ->first(); |
112 | } |
113 | |
114 | return $userDictionary; |
115 | } |
116 | |
117 | public function addWordToUserDictionary(User $user, string $word) |
118 | { |
119 | $userDictionary = $this->getUserDictionary($user); |
120 | |
121 | if (!$userDictionary) { |
122 | return false; |
123 | } |
124 | |
125 | $words = $userDictionary->words; |
126 | if (!in_array($word, $words)) { |
127 | $words[] = $word; |
128 | $userDictionary->words = $words; |
129 | $userDictionary->save(); |
130 | } |
131 | |
132 | $response = Http::withHeaders([ |
133 | 'Authorization' => $this->auth, |
134 | ])->post($this->url . 'action=addWordToUserDictionary', [ |
135 | 'format' => 'json', |
136 | 'customerid' => $this->serviceId, |
137 | 'cmd' => 'user_dictionary', |
138 | 'action' => 'addwords', |
139 | 'name' => $userDictionary->name, |
140 | 'word' => $word, |
141 | ]); |
142 | |
143 | if ($response->failed()) { |
144 | Log::error('Failed to add word to user dictionary', [ |
145 | 'userDictionaryId' => $userDictionary->id, |
146 | 'word' => $word, |
147 | 'response' => $response->body(), |
148 | ]); |
149 | return false; |
150 | } |
151 | |
152 | return true; |
153 | } |
154 | |
155 | public function removeWordToUserDictionary(User $user, string $word) |
156 | { |
157 | $userDictionary = $this->getUserDictionary($user); |
158 | |
159 | if (!$userDictionary) { |
160 | return false; |
161 | } |
162 | |
163 | $words = $userDictionary->words; |
164 | if (in_array($word, $words)) { |
165 | $words = array_diff($words, [$word]); |
166 | $words = array_values($words); |
167 | $userDictionary->words = $words; |
168 | $userDictionary->save(); |
169 | } |
170 | |
171 | $response = Http::withHeaders([ |
172 | 'Authorization' => $this->auth, |
173 | ])->post($this->url . 'action=addWordToUserDictionary', [ |
174 | 'format' => 'json', |
175 | 'customerid' => $this->serviceId, |
176 | 'cmd' => 'user_dictionary', |
177 | 'action' => 'deletewords', |
178 | 'name' => $userDictionary->name, |
179 | 'word' => $word, |
180 | ]); |
181 | |
182 | if ($response->failed()) { |
183 | if ($response->json()['message'] !== "Word doesn't exist.") { |
184 | Log::error('Failed to add word to user dictionary', [ |
185 | 'userDictionaryId' => $userDictionary->id, |
186 | 'word' => $word, |
187 | 'response' => $response->body(), |
188 | ]); |
189 | } |
190 | return false; |
191 | } |
192 | |
193 | return true; |
194 | } |
195 | } |