Test Setup Failed
Pull Request — master (#590)
by
unknown
11:30
created

SongController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 8
dl 55
loc 55
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B put() 27 27 2
A remove() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Http\Controllers\API\ObjectStorage\GCS;
4
5
use App\Events\LibraryChanged;
6
use App\Http\Requests\API\ObjectStorage\GCS\PutSongRequest;
7
use App\Http\Requests\API\ObjectStorage\GCS\RemoveSongRequest;
8
use App\Models\Album;
9
use App\Models\Artist;
10
use App\Models\Song;
11
use Exception;
12
use Illuminate\Http\JsonResponse;
13
use Media;
14
15 View Code Duplication
class SongController extends Controller
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
{
17
    /**
18
     * Store a new song or update an existing one with data from AWS.
19
     *
20
     * @param PutSongRequest $request
21
     *
22
     * @return JsonResponse
23
     */
24
    public function put(PutSongRequest $request)
25
    {
26
        $path = "gs://{$request->bucket}/{$request->key}";
27
28
        $tags = $request->tags;
29
        $artist = Artist::get(array_get($tags, 'artist'));
30
31
        $compilation = (bool) trim(array_get($tags, 'albumartist'));
32
        $album = Album::get($artist, array_get($tags, 'album'), $compilation);
33
34
        if ($cover = array_get($tags, 'cover')) {
35
            $album->writeCoverFile(base64_decode($cover['data']), $cover['extension']);
36
        }
37
38
        $song = Song::updateOrCreate(['id' => Media::getHash($path)], [
39
            'path' => $path,
40
            'album_id' => $album->id,
41
            'artist_id' => $artist->id,
42
            'title' => trim(array_get($tags, 'title', '')),
43
            'length' => array_get($tags, 'duration', 0),
44
            'track' => (int) array_get($tags, 'track'),
45
            'lyrics' => array_get($tags, 'lyrics', ''),
46
            'mtime' => time(),
47
        ]);
48
49
        return response()->json($song);
50
    }
51
52
    /**
53
     * Remove a song whose info matches with data sent from AWS.
54
     *
55
     * @param RemoveSongRequest $request
56
     *
57
     * @throws Exception
58
     *
59
     * * @return JsonResponse
60
     */
61
    public function remove(RemoveSongRequest $request)
62
    {
63
        $song = Song::byPath("gs://{$request->bucket}/{$request->key}");
64
        abort_unless((bool) $song, 404);
65
        $song->delete();
66
        event(new LibraryChanged());
67
        return response()->json();
68
    }
69
}
70