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

MediaController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 12
c 4
b 1
f 0
lcom 2
cbo 9
dl 0
loc 145
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A all() 0 9 1
A store() 0 14 2
B linkMedia() 0 38 4
A unlinkMedia() 0 18 2
A sortMedia() 0 13 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\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
        
87
        $entity = $entityClass::find($entityId);
88
        $zone = $request->get('zone');
89
        $entity->files()->attach($mediaId, [
90
            'imageable_type' => $entityClass,
91
            'zone' => $zone
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
        if ($file->mimetype == 'video/mp4' || $file->mimetype == 'video/ogv' || $file->mimetype == 'video/webm') {
100
            $thumbnailPath = $file->path->getRelativeUrl();
101
            $mediaType = 'video';
102
        } else {
103
            $thumbnailPath = $this->imagy->getThumbnail($file->path, 'mediumThumb');
104
            $mediaType = 'image';
105
        }
106
        
107
        event(new FileWasLinked($file, $entity));
108
        
109
        return Response::json([
110
            'error' => false,
111
            'message' => 'The link has been added.',
112
            'result' => [
113
                'path' => $thumbnailPath,
114
                'imageableId' => $imageable->id,
115
                'mediaType' => $mediaType
116
            ]
117
        ]);
118
    }
119
120
    /**
121
     * Remove the record in the media__imageables table for the given id
122
     * 
123
     * @param Request $request            
124
     */
125
    public function unlinkMedia(Request $request)
126
    {
127
        $imageableId = $request->get('imageableId');
128
        $deleted = DB::table('media__imageables')->whereId($imageableId)->delete();
129
        if (! $deleted) {
130
            return Response::json([
131
                'error' => true,
132
                'message' => 'The file was not found.'
133
            ]);
134
        }
135
        
136
        event(new FileWasUnlinked($imageableId));
137
        
138
        return Response::json([
139
            'error' => false,
140
            'message' => 'The link has been removed.'
141
        ]);
142
    }
143
144
    /**
145
     * Sort the record in the media__imageables table for the given array
146
     * @param Request $request
147
     */
148
    public function sortMedia(Request $request)
149
    {
150
        $imageableIdArray = $request->get('sortable');
151
152
        $order = 1;
153
154
        foreach ($imageableIdArray as $id) {
155
            DB::table('media__imageables')->whereId($id)->update(['order' => $order]);
156
            $order++;
157
        }
158
159
        return Response::json(['error' => false, 'message' => 'The items have been reorder.']);
160
    }
161
}
162