Completed
Pull Request — master (#41)
by
unknown
02:46
created

MediaController::linkMedia()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 40
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 40
rs 8.8571
cc 2
eloc 30
nc 2
nop 1
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\Events\FileWasLinked;
10
use Modules\Media\Events\FileWasUnlinked;
11
use Modules\Media\Events\FileWasUploaded;
12
use Modules\Media\Http\Requests\UploadMediaRequest;
13
use Modules\Media\Image\Imagy;
14
use Modules\Media\Repositories\FileRepository;
15
use Modules\Media\Services\FileService;
16
17
class MediaController extends Controller
18
{
19
20
    /**
21
     *
22
     * @var FileService
23
     */
24
    private $fileService;
25
26
    /**
27
     *
28
     * @var FileRepository
29
     */
30
    private $file;
31
32
    /**
33
     *
34
     * @var Imagy
35
     */
36
    private $imagy;
37
38
    public function __construct(FileService $fileService, FileRepository $file, Imagy $imagy)
39
    {
40
        $this->fileService = $fileService;
41
        $this->file = $file;
42
        $this->imagy = $imagy;
43
    }
44
45
    public function all()
46
    {
47
        $files = $this->file->all();
48
49
        return [
50
            'count' => $files->count(),
51
            'data' => $files
52
        ];
53
    }
54
55
    /**
56
     * Store a newly created resource in storage.
57
     *
58
     * @param UploadMediaRequest $request
59
     * @return Response
60
     */
61
    public function store(UploadMediaRequest $request)
62
    {
63
        $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...
64
65
        if (is_string($savedFile)) {
66
            return Response::json([
67
                'error' => $savedFile
68
            ], 409);
69
        }
70
71
        event(new FileWasUploaded($savedFile));
72
73
        return Response::json($savedFile->toArray());
74
    }
75
76
    /**
77
     * Link the given entity with a media file
78
     *
79
     * @param Request $request
80
     */
81
    public function linkMedia(Request $request)
82
    {
83
        $mediaId = $request->get('mediaId');
84
        $entityClass = $request->get('entityClass');
85
        $entityId = $request->get('entityId');
86
        $order = $request->get('order');
87
88
        $entity = $entityClass::find($entityId);
89
        $zone = $request->get('zone');
90
        $entity->files()->attach($mediaId, [
91
            'imageable_type' => $entityClass,
92
            'zone' => $zone,
93
            'order' => $order
94
        ]);
95
        $imageable = DB::table('media__imageables')->whereFileId($mediaId)
96
            ->whereZone($zone)
97
            ->whereImageableType($entityClass)
98
            ->first();
99
        $file = $this->file->find($imageable->file_id);
100
101
        if (str_contains($file->mimetype, 'video')) {
102
            $thumbnailPath = $file->path->getRelativeUrl();
103
            $mediaType = 'video';
104
        } else {
105
            $thumbnailPath = $this->imagy->getThumbnail($file->path, 'mediumThumb');
106
            $mediaType = 'image';
107
        }
108
109
        event(new FileWasLinked($file, $entity));
110
111
        return Response::json([
112
            'error' => false,
113
            'message' => 'The link has been added.',
114
            'result' => [
115
                'path' => $thumbnailPath,
116
                'imageableId' => $imageable->id,
117
                'mediaType' => $mediaType
118
            ]
119
        ]);
120
    }
121
122
    /**
123
     * Remove the record in the media__imageables table for the given id
124
     *
125
     * @param Request $request
126
     */
127
    public function unlinkMedia(Request $request)
128
    {
129
        $imageableId = $request->get('imageableId');
130
        $deleted = DB::table('media__imageables')->whereId($imageableId)->delete();
131
        if (! $deleted) {
132
            return Response::json([
133
                'error' => true,
134
                'message' => 'The file was not found.'
135
            ]);
136
        }
137
138
        event(new FileWasUnlinked($imageableId));
139
140
        return Response::json([
141
            'error' => false,
142
            'message' => 'The link has been removed.'
143
        ]);
144
    }
145
146
    /**
147
     * Sort the record in the media__imageables table for the given array
148
     * @param Request $request
149
     */
150
    public function sortMedia(Request $request)
151
    {
152
        $imageableIdArray = $request->get('sortable');
153
154
        $order = 1;
155
156
        foreach ($imageableIdArray as $id) {
157
            DB::table('media__imageables')->whereId($id)->update(['order' => $order]);
158
            $order++;
159
        }
160
161
        return Response::json(['error' => false, 'message' => 'The items have been reorder.']);
162
    }
163
}
164