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