Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 60 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| FlyShareService | |
0.00% |
0 / 60 |
|
0.00% |
0 / 6 |
240 | |
0.00% |
0 / 1 |
| cloneShortcuts | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| acceptSharedClones | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
42 | |||
| getSharedToOthersShortcuts | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSharedWithShortcuts | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getSharesHistory | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| fullTextToPreview | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Services; |
| 4 | |
| 5 | use App\Http\Models\ClonedSharedShortcut; |
| 6 | use App\Http\Models\FlyShare; |
| 7 | use App\Http\Models\ShareHistory; |
| 8 | use App\Http\Models\SharesShortcut; |
| 9 | use App\Http\Models\Shortcut; |
| 10 | use App\Http\Models\Auth\User; |
| 11 | use App\Http\Scopes\UserScope; |
| 12 | use Illuminate\Database\Eloquent\Collection; |
| 13 | use MongoDB\BSON\UTCDateTime; |
| 14 | |
| 15 | class FlyShareService |
| 16 | { |
| 17 | /** |
| 18 | * Clone the shortcuts. |
| 19 | * |
| 20 | * @param array<string> $shorctutIds |
| 21 | * @return mixed |
| 22 | */ |
| 23 | public function cloneShortcuts(array $shorctutIds, string $flyshareId) |
| 24 | { |
| 25 | $shortcuts = Shortcut::withoutGlobalScope(UserScope::class)->find($shorctutIds); |
| 26 | |
| 27 | if (! $shortcuts || count($shortcuts) < 1) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | $clones = $shortcuts->map(function ($shortcut) use ($flyshareId) { |
| 32 | return collect($shortcut->replicate())->merge([ |
| 33 | 'flyshare_id' => $flyshareId, |
| 34 | 'reference_shortcut_id' => $shortcut->_id, |
| 35 | 'created_at' => new UTCDateTime(now()), |
| 36 | 'updated_at' => new UTCDateTime(now()), |
| 37 | ]); |
| 38 | })->toArray(); |
| 39 | |
| 40 | return ClonedSharedShortcut::insert($clones); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Add shared cloned shortcuts to users received shares |
| 45 | * |
| 46 | */ |
| 47 | public function acceptSharedClones(User $user, FlyShare $flyshare): bool |
| 48 | { |
| 49 | //Check if user has accepted flyshare previously |
| 50 | $updatedRecord = ShareHistory::where([ |
| 51 | ['flyshare_id', $flyshare->id], |
| 52 | ['email', $user->email], |
| 53 | ['status', FlyShare::FLYSHARE_INVITE_STATUS_ACCEPTED], |
| 54 | ])->exists(); |
| 55 | |
| 56 | if ($updatedRecord) { |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | $clonedShortcuts = $flyshare->clonedSharedShortcuts; |
| 61 | $sharerId = $flyshare->user_id; |
| 62 | |
| 63 | if (! $clonedShortcuts || count($clonedShortcuts) < 1) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | $clones = $clonedShortcuts->map(function ($clonedShortcut) use ($user, $sharerId) { |
| 68 | return collect($clonedShortcut->shortcut ? $clonedShortcut->shortcut->replicate() : $clonedShortcut->replicate())->except(['shared_with_others'])->merge([ |
| 69 | 'sharer_id' => $sharerId, |
| 70 | 'user_id' => $user->getKey(), |
| 71 | 'reference_cloned_shortcut_id' => $clonedShortcut->_id, |
| 72 | 'created_at' => new UTCDateTime(now()), |
| 73 | 'updated_at' => new UTCDateTime(now()), |
| 74 | ]); |
| 75 | })->toArray(); |
| 76 | |
| 77 | $saved = SharesShortcut::insert($clones); |
| 78 | |
| 79 | if (! $saved) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | $flyshare->shareHistory()->updateOrCreate(['email' => $user->email], [ |
| 84 | 'email' => $user->email, |
| 85 | 'status' => FlyShare::FLYSHARE_INVITE_STATUS_ACCEPTED, |
| 86 | 'registered' => true, |
| 87 | 'user_id' => $user->id, |
| 88 | ]); |
| 89 | |
| 90 | return $saved; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the shortcuts the user shared with others. |
| 95 | * |
| 96 | * @return Illuminate\Database\Eloquent\Collection |
| 97 | */ |
| 98 | public function getSharedToOthersShortcuts(User $user): Collection |
| 99 | { |
| 100 | return $user->shortcutsSharedWithOthers()->latest()->get(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get the shortcuts shared with the user. |
| 105 | * |
| 106 | * @return Illuminate\Database\Eloquent\Collection |
| 107 | */ |
| 108 | public function getSharedWithShortcuts(User $user): Collection |
| 109 | { |
| 110 | $shortcuts = $user->shortcutsSharedWithUser()->latest()->get(); |
| 111 | $shortcuts->load('sharer'); |
| 112 | |
| 113 | return $shortcuts; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get the shares history for the cloned shortcut. |
| 118 | * |
| 119 | * @return Illuminate\Database\Eloquent\Collection |
| 120 | */ |
| 121 | public function getSharesHistory(ClonedSharedShortcut $clonedSharedShortcut): Collection |
| 122 | { |
| 123 | $sharedWith = $clonedSharedShortcut->flyshare->shareHistory; |
| 124 | |
| 125 | $registeredUsers = $sharedWith->where('registered', true); |
| 126 | |
| 127 | if ($registeredUsers->count() < 1) { |
| 128 | return $sharedWith; |
| 129 | } |
| 130 | |
| 131 | $usersSharedWith = $sharedWith->map(function ($user) use ($clonedSharedShortcut) { |
| 132 | if ($user->registered) { |
| 133 | $userRecord = $user->user; |
| 134 | $user->name = "$userRecord->first_name $userRecord->last_name"; |
| 135 | $user->profile_picture = $userRecord->avatar; |
| 136 | } |
| 137 | |
| 138 | $user->flyshare_token = $clonedSharedShortcut->flyshare->_id; |
| 139 | |
| 140 | return $user; |
| 141 | }); |
| 142 | |
| 143 | return $usersSharedWith; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Extract long text to preview |
| 148 | */ |
| 149 | public function fullTextToPreview(string $text, int $maxChar = 150): string |
| 150 | { |
| 151 | return $text; |
| 152 | // $text = strip_tags($text); |
| 153 | |
| 154 | // if (!$text || strlen($text) <= $maxChar) { |
| 155 | // return $text; |
| 156 | // } |
| 157 | |
| 158 | // $words = preg_split('/\s/', $text); |
| 159 | // $output = ''; |
| 160 | // $i = 0; |
| 161 | |
| 162 | // while (1) { |
| 163 | // $length = strlen($output)+strlen($words[$i]); |
| 164 | |
| 165 | // if ($length > $maxChar) { |
| 166 | // break; |
| 167 | // } else { |
| 168 | // $output .= " " . $words[$i]; |
| 169 | // ++$i; |
| 170 | // } |
| 171 | // } |
| 172 | |
| 173 | // $output .= '...'; |
| 174 | |
| 175 | // return $output; |
| 176 | // //html_entity_decode(str_replace([" ", "nbsp;", "&nbsp;"], '<br>', $output)); |
| 177 | } |
| 178 | } |