Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 103 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ImportShortcutController | |
0.00% |
0 / 103 |
|
0.00% |
0 / 3 |
182 | |
0.00% |
0 / 1 |
| import | |
0.00% |
0 / 53 |
|
0.00% |
0 / 1 |
90 | |||
| checkShortCodeCategory | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| replace | |
0.00% |
0 / 43 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\v1; |
| 4 | |
| 5 | use Carbon\Carbon; |
| 6 | use Illuminate\Http\Request; |
| 7 | use App\Http\Models\Shortcut; |
| 8 | use MongoDB\BSON\UTCDateTime; |
| 9 | use Illuminate\Validation\Rule; |
| 10 | use App\Traits\SubscriptionTrait; |
| 11 | use Illuminate\Http\JsonResponse; |
| 12 | use App\Http\Controllers\Controller; |
| 13 | use App\Http\Models\ShortcutVersion; |
| 14 | use App\Http\Models\ShortcutCategory; |
| 15 | use Illuminate\Support\Facades\Validator; |
| 16 | use App\Http\Requests\ImportShortcutRequest; |
| 17 | |
| 18 | class ImportShortcutController extends Controller |
| 19 | { |
| 20 | use SubscriptionTrait; |
| 21 | |
| 22 | public function import(ImportShortcutRequest $request): JsonResponse |
| 23 | { |
| 24 | |
| 25 | $userId = $request->user()->getKey(); |
| 26 | $data = $request->all(); |
| 27 | |
| 28 | $validator = Validator::make($data, [ |
| 29 | '*.title' => 'string', |
| 30 | '*.shortcut' => Rule::unique('shortcuts', 'shortcut')->where('user_id', $userId), |
| 31 | '*.text' => 'required|string', |
| 32 | '*.html' => 'required|string', |
| 33 | '*.category' => 'string', |
| 34 | '*.uploads' => 'sometimes|array' |
| 35 | ]); |
| 36 | |
| 37 | if ($validator->fails()) { |
| 38 | $errors = $validator->errors()->all(); |
| 39 | $error_string = []; |
| 40 | $duplicate_shortcuts = []; |
| 41 | $duplicate_shortcut_data = []; |
| 42 | foreach ($errors as $error) { |
| 43 | $error_string_data = explode('The ', $error); |
| 44 | if (isset($error_string_data[1])) { |
| 45 | $error_string_data = explode('.shortcut', $error_string_data[1]); |
| 46 | if (isset($error_string_data[0])) { |
| 47 | $duplicate_num = $error_string_data[0]; |
| 48 | array_push($duplicate_shortcuts, $duplicate_num); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (!empty($duplicate_shortcuts)) { |
| 54 | $i = 0; |
| 55 | foreach ($data as $key => $row) { |
| 56 | if (in_array($key, $duplicate_shortcuts)) { |
| 57 | $shortcut_title = $row["shortcut"]; |
| 58 | $error_string[$key . '.shortcut'][] = "The " . $shortcut_title . " shortcut has already been taken."; |
| 59 | $duplicate_shortcut_data[$i] = $row; |
| 60 | //array_push($error_string, $string_array); |
| 61 | |
| 62 | $i++; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return response()->json([ |
| 68 | 'error' => 'The given data was invalid.', |
| 69 | 'error_list' => $error_string, |
| 70 | 'data' => $duplicate_shortcut_data |
| 71 | ]); |
| 72 | } |
| 73 | |
| 74 | $items = []; |
| 75 | |
| 76 | foreach ($data as $item) { |
| 77 | $this->checkStylesTagsPermissions($request, $item['html']); |
| 78 | $this->checkIfGiphyExists($request, $item['html']); |
| 79 | $this->checkFlyCutsCount($request); |
| 80 | $this->checkCharacterCount($request, $item['html']); |
| 81 | |
| 82 | $item['user_id'] = $userId; |
| 83 | $item['version'] = 1; |
| 84 | $item['user_defined'] = true; |
| 85 | |
| 86 | $category = $request->get('category', 'Imported FlyCuts'); |
| 87 | |
| 88 | $item['category_id'] = $this->checkShortCodeCategory($category); |
| 89 | |
| 90 | $now = new UTCDateTime(Carbon::now()); |
| 91 | |
| 92 | $item['created_at'] = $now; |
| 93 | $item['updated_at'] = $now; |
| 94 | |
| 95 | unset($item['category']); |
| 96 | |
| 97 | array_push($items, $item); |
| 98 | } |
| 99 | |
| 100 | Shortcut::insert($items); |
| 101 | |
| 102 | return response()->json(true); |
| 103 | } |
| 104 | |
| 105 | protected function checkShortCodeCategory($category) |
| 106 | { |
| 107 | $category = ShortcutCategory::firstOrNew([ |
| 108 | 'name' => $category, |
| 109 | 'user_id' => request()->user()->getKey(), |
| 110 | 'is_default' => false, |
| 111 | ]); |
| 112 | |
| 113 | $category->save(); |
| 114 | |
| 115 | return $category->id; |
| 116 | } |
| 117 | |
| 118 | public function replace(Request $request) |
| 119 | { |
| 120 | $userId = $request->user()->getKey(); |
| 121 | $data = $request->all(); |
| 122 | |
| 123 | $shortcutSearchArray = ["user_id" => $userId, 'shortcut' => $request->shortcut]; |
| 124 | $shortcut = Shortcut::where($shortcutSearchArray)->first(); |
| 125 | if (!empty($shortcut)) { |
| 126 | $shortcutId = $shortcut->id; |
| 127 | |
| 128 | // delete shortcut versions |
| 129 | $isShortcutVersionExists = ShortcutVersion::where('shortcut_id', $shortcutId)->first(); |
| 130 | if (!empty($isShortcutVersionExists)) { |
| 131 | ShortcutVersion::where('shortcut_id', $shortcutId)->delete(); |
| 132 | } |
| 133 | |
| 134 | Shortcut::where($shortcutSearchArray)->delete(); |
| 135 | } |
| 136 | |
| 137 | $data['user_id'] = $userId; |
| 138 | $data['version'] = 1; |
| 139 | $data['user_defined'] = true; |
| 140 | $data['version_counts'] = 1; |
| 141 | |
| 142 | $charactors_count = strlen(strip_tags($data['text'])); |
| 143 | $words_count = str_word_count(strip_tags($data['text'])); |
| 144 | $html_count = strlen(strip_tags($data['html'])); |
| 145 | |
| 146 | $data['charactors_count'] = $charactors_count; |
| 147 | $data['words_count'] = $words_count; |
| 148 | $data['html_count'] = $html_count; |
| 149 | |
| 150 | $result = Shortcut::create($data); |
| 151 | |
| 152 | $shortcutId = $result['_id']; |
| 153 | |
| 154 | dispatch(function () use ($userId, $charactors_count, $words_count, $html_count, $data, $shortcutId) { |
| 155 | |
| 156 | // Create a flymessage version |
| 157 | $shortcutVersionHistory = new ShortcutVersion(); |
| 158 | $statisticsData = array( |
| 159 | 'charactors_count' => $charactors_count, |
| 160 | 'words_count' => $words_count, |
| 161 | 'html_count' => $html_count, |
| 162 | 'media_count' => count($data['uploads']), //use media size when this endpoint gets to be used as in ShortcutControler::create |
| 163 | 'updated_at' => now(), |
| 164 | ); |
| 165 | |
| 166 | $flymsg_details = array_merge($data, $statisticsData); |
| 167 | $versionHistoryData[1] = json_encode($flymsg_details, true); |
| 168 | $versionHistoryData['shortcut_id'] = $shortcutId; |
| 169 | $versionHistoryData['user_id'] = $userId; |
| 170 | $versionHistoryData['last_version_count'] = 1; |
| 171 | $versionHistoryData['current_version'] = 0; |
| 172 | |
| 173 | $shortcutVersionHistory->fill($versionHistoryData); |
| 174 | $shortcutVersionHistory->push(); |
| 175 | |
| 176 | $this->dispatchShortcutEvent(); |
| 177 | |
| 178 | $this->saveBrowserUsed($userId); |
| 179 | }); |
| 180 | |
| 181 | |
| 182 | return response()->json($shortcutId); |
| 183 | } |
| 184 | } |