Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 74 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
StatisticController | |
0.00% |
0 / 74 |
|
0.00% |
0 / 6 |
380 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
shortcutsMediaSizes | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
30 | |||
updateMediaSizes | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
mediaSizesAllUsers | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
30 | |||
createUpdateMediaSizes | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
30 | |||
storeMediaSizes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace App\Http\Controllers\v1; |
4 | |
5 | use App\Http\Controllers\Controller; |
6 | use App\Http\Models\MediaSizes; |
7 | use App\Http\Models\Shortcut; |
8 | use App\Http\Scopes\UserScope; |
9 | use Exception; |
10 | use Illuminate\Http\JsonResponse; |
11 | use Illuminate\Support\Facades\DB; |
12 | use Symfony\Component\HttpFoundation\Response; |
13 | |
14 | class StatisticController extends Controller |
15 | { |
16 | public function __construct() |
17 | { |
18 | ini_set('max_execution_time', '600'); |
19 | } |
20 | |
21 | public function shortcutsMediaSizes(): JsonResponse |
22 | { |
23 | $result = []; |
24 | try { |
25 | DB::table('shortcuts')->whereNotNull('uploads')->orderBy('id')->chunk(250, function ($shortcuts) use (&$result) { |
26 | foreach ($shortcuts as $shortcut) { |
27 | if (count($shortcut->uploads) > 0) { |
28 | $imagesize = 0.0; |
29 | foreach ($shortcut->uploads as $media) { |
30 | $imagesize += getSizeFromRemoteFile($media); |
31 | } |
32 | $isUpdated = $this->updateMediaSizes($shortcut->_id, $imagesize); |
33 | $result[] = [ |
34 | 'id' => $shortcut->_id, |
35 | 'isUpdated' => $isUpdated, |
36 | ]; |
37 | } |
38 | } |
39 | }); |
40 | } catch (Exception $e) { |
41 | return response()->json(['data' => [], 'message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR); |
42 | } |
43 | |
44 | return response()->json(['data' => $result, 'message' => 'Succeed'], Response::HTTP_OK); |
45 | } |
46 | |
47 | private function updateMediaSizes($_id, $_imagesize): bool |
48 | { |
49 | try { |
50 | $imagesizeForHuman = getSizeUploadedFileToHumanReadable($_imagesize); |
51 | $searchArray = ['_id' => $_id]; |
52 | $shortcut = Shortcut::withoutGlobalScope(UserScope::class)->where($searchArray)->first(); |
53 | $shortcut->media_sizes = $imagesizeForHuman; |
54 | $result = $shortcut->save(); |
55 | } catch (Exception $e) { |
56 | $result = false; |
57 | } |
58 | |
59 | return $result; |
60 | } |
61 | |
62 | public function mediaSizesAllUsers(): JsonResponse |
63 | { |
64 | $result = null; |
65 | $processed_users = 0; |
66 | $users_with_used_space = 0; |
67 | $users_without_used_space = 0; |
68 | try { |
69 | DB::table('users')->orderBy('id')->chunk(250, function ($users) use (&$result, &$processed_users) { |
70 | foreach ($users as $user) { |
71 | $result[] = $this->createUpdateMediaSizes($user->_id); |
72 | $processed_users++; |
73 | } |
74 | }); |
75 | $storage_used_total = 0; |
76 | foreach ($result as $data) { |
77 | $storage_used_total += $data['storage_used']; |
78 | if ($data['storage_used'] > 0) { |
79 | $users_with_used_space++; |
80 | } else { |
81 | $users_without_used_space++; |
82 | } |
83 | } |
84 | $result['storage_used_total'] = getSizeUploadedFileToHumanReadable($storage_used_total); |
85 | $result['processed_users'] = $processed_users; |
86 | $result['users_with_used_space'] = $users_with_used_space; |
87 | $result['users_without_used_space'] = $users_without_used_space; |
88 | } catch (Exception $e) { |
89 | return response()->json(['data' => [], 'message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR); |
90 | } |
91 | |
92 | return response()->json(['data' => $result, 'message' => 'Succeed'], Response::HTTP_OK); |
93 | } |
94 | |
95 | private function createUpdateMediaSizes($user_id): array |
96 | { |
97 | $result = []; |
98 | $result['storage_used'] = null; |
99 | $user_id = (string) $user_id; |
100 | $imagesizeTotal = 0.0; |
101 | $shortcutIDs = null; |
102 | $uploads = null; |
103 | $shortcuts = Shortcut::withoutGlobalScope(UserScope::class)->where('user_id', '=', $user_id)->get(); |
104 | foreach ($shortcuts as $shortcut) { |
105 | if (isset($shortcut['uploads']) && count($shortcut['uploads']) > 0) { |
106 | $imagesize = 0.0; |
107 | foreach ($shortcut['uploads'] as $media) { |
108 | $imagesize += getSizeFromRemoteFile($media); |
109 | $uploads[] = $media; |
110 | } |
111 | $imagesizeTotal += $imagesize; |
112 | } |
113 | $shortcutID = (string) $shortcut['_id']; |
114 | $shortcutIDs[] = $shortcutID; |
115 | } |
116 | $imagesizeForHuman = getSizeUploadedFileToHumanReadable($imagesizeTotal); |
117 | $data = [ |
118 | 'user_id' => $user_id, |
119 | 'shortcut' => $shortcutIDs, |
120 | 'storage_used' => $imagesizeForHuman, |
121 | 'uploads' => $uploads, |
122 | ]; |
123 | $result['data'] = $this->storeMediaSizes($data); |
124 | $result['storage_used'] += $imagesizeTotal; |
125 | |
126 | return $result; |
127 | } |
128 | |
129 | private function storeMediaSizes($data) |
130 | { |
131 | return MediaSizes::updateOrCreate($data); |
132 | } |
133 | } |