Completed
Pull Request — master (#36)
by
unknown
02:34
created

MediaController::linkMedia()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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