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