Passed
Pull Request — master (#17)
by ARCANEDEV
05:21
created

MediaApiController::all()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 1
1
<?php namespace Arcanesoft\Media\Http\Controllers;
2
3
use Arcanesoft\Media\Entities\MediaItem;
4
use Arcanesoft\Media\Http\Requests\NewFolderRequest;
5
use Arcanesoft\Media\Http\Requests\RenameMediaRequest;
6
use Arcanesoft\Media\Http\Requests\UploadMediaRequest;
7
use Illuminate\Http\Request;
8
9
/**
10
 * Class     MediaApiController
11
 *
12
 * @package  Arcanesoft\Media\Http\Controllers
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class MediaApiController extends Controller
16
{
17
    /* -----------------------------------------------------------------
18
     |  Main Methods
19
     | -----------------------------------------------------------------
20
     */
21
22
    /**
23
     * @param  \Illuminate\Http\Request  $request
24
     *
25
     * @return \Illuminate\Http\JsonResponse
26
     */
27
    public function all(Request $request)
28
    {
29
        // TODO: Add authorization check
30
31
        $location = $request->get('location', '/');
32
33
        if ( ! $this->manager->exists($location))
34
            return static::jsonResponseError(['message' => 'Location not found'], 404);
35
36
        return static::jsonResponse(
37
            $this->manager->all($location)->values()->toArray()
38
        );
39
    }
40
41
    /**
42
     * Get all the directories based on the given location.
43
     *
44
     * @param  \Illuminate\Http\Request  $request
45
     *
46
     * @return \Illuminate\Http\JsonResponse
47
     */
48
    public function directories(Request $request)
49
    {
50
        // TODO: Add authorization check
51
        $location = $request->get('location', '/');
52
53
        if ($this->manager->exists($location)) {
54
            return static::jsonResponse(
55
                $this->manager->directories($location)->values()->toArray()
56
            );
57
        }
58
59
        return static::jsonResponseError(['message' => 'Location not found'], 404);
60
    }
61
62
    /**
63
     * Upload a media.
64
     *
65
     * @param  \Arcanesoft\Media\Http\Requests\UploadMediaRequest  $request
66
     *
67
     * @return \Illuminate\Http\JsonResponse
68
     */
69
    public function upload(UploadMediaRequest $request)
70
    {
71
        // TODO: Add authorization check
72
73
        $url = $this->manager->putFile(
74
            $request->get('location'),
0 ignored issues
show
Bug introduced by
It seems like $request->get('location') can also be of type null; however, parameter $path of Arcanesoft\Media\MediaManager::putFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
            /** @scrutinizer ignore-type */ $request->get('location'),
Loading history...
75
            $request->file('files')[0]
76
        );
77
78
        return static::jsonResponse(compact('url'));
79
    }
80
81
    /**
82
     * Create a new folder.
83
     *
84
     * @param  \Arcanesoft\Media\Http\Requests\NewFolderRequest  $request
85
     *
86
     * @return \Illuminate\Http\JsonResponse
87
     */
88
    public function newFolder(NewFolderRequest $request)
89
    {
90
        // TODO: Add authorization check
91
92
        $this->manager->makeDirectory(
93
            $path = $request->get('path')
0 ignored issues
show
Bug introduced by
It seems like $path = $request->get('path') can also be of type null; however, parameter $path of Arcanesoft\Media\MediaManager::makeDirectory() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
            /** @scrutinizer ignore-type */ $path = $request->get('path')
Loading history...
94
        );
95
96
        return static::jsonResponse(compact('path'));
97
    }
98
99
    /**
100
     * Move a media.
101
     *
102
     * @param  \Illuminate\Http\Request  $request
103
     *
104
     * @return \Illuminate\Http\JsonResponse
105
     */
106
    public function move(Request $request)
107
    {
108
        $from        = $request->get('path');
109
        $destination = $request->get('destination');
110
111
        $parts = explode('/', $from);
112
        $file  = array_pop($parts);
113
        $to    = implode('/', array_merge($parts, [$destination], [$file]));
114
115
        $this->manager->move($from, $to);
0 ignored issues
show
Bug introduced by
It seems like $from can also be of type null; however, parameter $from of Arcanesoft\Media\MediaManager::move() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
        $this->manager->move(/** @scrutinizer ignore-type */ $from, $to);
Loading history...
116
117
        return static::jsonResponse();
118
    }
119
120
    /**
121
     * Rename a media.
122
     *
123
     * @param  \Arcanesoft\Media\Http\Requests\RenameMediaRequest  $request
124
     *
125
     * @return \Illuminate\Http\JsonResponse
126
     */
127
    public function rename(RenameMediaRequest $request)
128
    {
129
        // TODO: Add authorization check
130
131
        $this->manager->rename(
132
            $request->get('old_path'),
0 ignored issues
show
Bug introduced by
It seems like $request->get('old_path') can also be of type null; however, parameter $oldName of Arcanesoft\Media\MediaManager::rename() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

132
            /** @scrutinizer ignore-type */ $request->get('old_path'),
Loading history...
133
            $request->get('new_path')
0 ignored issues
show
Bug introduced by
It seems like $request->get('new_path') can also be of type null; however, parameter $newName of Arcanesoft\Media\MediaManager::rename() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
            /** @scrutinizer ignore-type */ $request->get('new_path')
Loading history...
134
        );
135
136
        return static::jsonResponse();
137
    }
138
139
    /**
140
     * Delete a media.
141
     *
142
     * @param  \Illuminate\Http\Request  $request
143
     *
144
     * @return \Illuminate\Http\JsonResponse
145
     */
146
    public function delete(Request $request)
147
    {
148
        // TODO: Add authorization check
149
150
        $type = $request->get('type');
151
        $path = $request->get('path');
152
153
        if ($type === MediaItem::TYPE_FILE){
154
            $this->manager->deleteFile($path);
155
        }
156
        elseif ($type === MediaItem::TYPE_DIRECTORY) {
157
            $this->manager->deleteDirectory($path);
0 ignored issues
show
Bug introduced by
It seems like $path can also be of type null; however, parameter $directory of Arcanesoft\Media\MediaManager::deleteDirectory() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

157
            $this->manager->deleteDirectory(/** @scrutinizer ignore-type */ $path);
Loading history...
158
        }
159
        else {
160
            // TODO: Throw an exception ?
161
        }
162
163
        return static::jsonResponse();
164
    }
165
166
    public function download(Request $request)
167
    {
168
        return $this->manager->download(
169
            $request->get('path')
0 ignored issues
show
Bug introduced by
It seems like $request->get('path') can also be of type null; however, parameter $path of Arcanesoft\Media\MediaManager::download() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

169
            /** @scrutinizer ignore-type */ $request->get('path')
Loading history...
170
        );
171
    }
172
}
173