Completed
Push — 2.0 ( 60b181...751b46 )
by Nicolas
03:03
created

MediaController::getThumbnailPathFor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace Modules\Media\Http\Controllers\Api;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Routing\Controller;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Facades\Response;
9
use Modules\Media\Entities\File;
10
use Modules\Media\Events\FileWasLinked;
11
use Modules\Media\Events\FileWasUnlinked;
12
use Modules\Media\Events\FileWasUploaded;
13
use Modules\Media\Helpers\FileHelper;
14
use Modules\Media\Http\Requests\UploadMediaRequest;
15
use Modules\Media\Image\Imagy;
16
use Modules\Media\Repositories\FileRepository;
17
use Modules\Media\Services\FileService;
18
19
class MediaController extends Controller
20
{
21
    /**
22
     * @var FileService
23
     */
24
    private $fileService;
25
26
    /**
27
     * @var FileRepository
28
     */
29
    private $file;
30
31
    /**
32
     * @var Imagy
33
     */
34
    private $imagy;
35
36
    public function __construct(FileService $fileService, FileRepository $file, Imagy $imagy)
37
    {
38
        $this->fileService = $fileService;
39
        $this->file = $file;
40
        $this->imagy = $imagy;
41
    }
42
43
    public function all()
44
    {
45
        $files = $this->file->all();
46
47
        return [
48
            'count' => $files->count(),
49
            'data' => $files,
50
        ];
51
    }
52
53
    /**
54
     * Store a newly created resource in storage.
55
     *
56
     * @param UploadMediaRequest $request
57
     * @return Response
58
     */
59
    public function store(UploadMediaRequest $request)
60
    {
61
        $savedFile = $this->fileService->store($request->file('file'));
0 ignored issues
show
Bug introduced by
It seems like $request->file('file') targeting Illuminate\Http\Request::file() can also be of type array or null; however, Modules\Media\Services\FileService::store() does only seem to accept object<Symfony\Component...tion\File\UploadedFile>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
62
63
        if (is_string($savedFile)) {
64
            return Response::json([
65
                'error' => $savedFile,
66
            ], 409);
67
        }
68
69
        event(new FileWasUploaded($savedFile));
70
71
        return Response::json($savedFile->toArray());
72
    }
73
74
    /**
75
     * Link the given entity with a media file
76
     *
77
     * @param Request $request
78
     */
79
    public function linkMedia(Request $request)
80
    {
81
        $mediaId = $request->get('mediaId');
82
        $entityClass = $request->get('entityClass');
83
        $entityId = $request->get('entityId');
84
        $order = $request->get('order');
85
86
        $entity = $entityClass::find($entityId);
87
        $zone = $request->get('zone');
88
        $entity->files()->attach($mediaId, [
89
            'imageable_type' => $entityClass,
90
            'zone' => $zone,
91
            'order' => $order,
92
        ]);
93
        $imageable = DB::table('media__imageables')->whereFileId($mediaId)
94
            ->whereZone($zone)
95
            ->whereImageableType($entityClass)
96
            ->first();
97
        $file = $this->file->find($imageable->file_id);
98
99
        $mediaType = FileHelper::getTypeByMimetype($file->mimetype);
100
101
        $thumbnailPath = $this->getThumbnailPathFor($mediaType, $file);
102
103
        event(new FileWasLinked($file, $entity));
104
105
        return Response::json([
106
            'error' => false,
107
            'message' => 'The link has been added.',
108
            'result' => [
109
                'path' => $thumbnailPath,
110
                'imageableId' => $imageable->id,
111
                'mediaType' => $mediaType,
112
                'mimetype' => $file->mimetype,
113
            ],
114
        ]);
115
    }
116
117
    /**
118
     * Remove the record in the media__imageables table for the given id
119
     *
120
     * @param Request $request
121
     */
122
    public function unlinkMedia(Request $request)
123
    {
124
        $imageableId = $request->get('imageableId');
125
        $deleted = DB::table('media__imageables')->whereId($imageableId)->delete();
126
        if (! $deleted) {
127
            return Response::json([
128
                'error' => true,
129
                'message' => 'The file was not found.',
130
            ]);
131
        }
132
133
        event(new FileWasUnlinked($imageableId));
134
135
        return Response::json([
136
            'error' => false,
137
            'message' => 'The link has been removed.',
138
        ]);
139
    }
140
141
    /**
142
     * Sort the record in the media__imageables table for the given array
143
     * @param Request $request
144
     */
145
    public function sortMedia(Request $request)
146
    {
147
        $imageableIdArray = $request->get('sortable');
148
149
        $order = 1;
150
151
        foreach ($imageableIdArray as $id) {
152
            DB::table('media__imageables')->whereId($id)->update(['order' => $order]);
153
            $order++;
154
        }
155
156
        return Response::json(['error' => false, 'message' => 'The items have been reorder.']);
157
    }
158
159
    /**
160
     * Get the path for the given file and type
161
     * @param string $mediaType
162
     * @param File $file
163
     * @return string
164
     */
165
    private function getThumbnailPathFor($mediaType, File $file)
166
    {
167
        if ($mediaType === 'image') {
168
            return $this->imagy->getThumbnail($file->path, 'mediumThumb');
169
        }
170
171
        return $file->path->getRelativeUrl();
172
    }
173
}
174