MediaInformationService   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 19
c 1
b 0
f 0
dl 0
loc 55
ccs 19
cts 21
cp 0.9048
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getArtistInformation() 0 17 3
A __construct() 0 3 1
A getAlbumInformation() 0 17 3
1
<?php
2
3
namespace App\Services;
4
5
use App\Events\AlbumInformationFetched;
6
use App\Events\ArtistInformationFetched;
7
use App\Models\Album;
8
use App\Models\Artist;
9
10
class MediaInformationService
11
{
12
    private $lastfmService;
13
14 7
    public function __construct(LastfmService $lastfmService)
15
    {
16 7
        $this->lastfmService = $lastfmService;
17 7
    }
18
19
    /**
20
     * Get extra information about an album from Last.fm.
21
     *
22
     * @return array|null The album info in an array format, or null on failure.
23
     */
24 1
    public function getAlbumInformation(Album $album): ?array
25
    {
26 1
        if ($album->is_unknown) {
27
            return null;
28
        }
29
30 1
        $info = $this->lastfmService->getAlbumInformation($album->name, $album->artist->name);
31
32 1
        if ($info) {
33 1
            event(new AlbumInformationFetched($album, $info));
34
35
            // The album may have been updated.
36 1
            $album->refresh();
37 1
            $info['cover'] = $album->cover;
38
        }
39
40 1
        return $info;
41
    }
42
43
    /**
44
     * Get extra information about an artist from Last.fm.
45
     *
46
     * @return array|null The artist info in an array format, or null on failure.
47
     */
48 1
    public function getArtistInformation(Artist $artist): ?array
49
    {
50 1
        if ($artist->is_unknown) {
51
            return null;
52
        }
53
54 1
        $info = $this->lastfmService->getArtistInformation($artist->name);
55
56 1
        if ($info) {
57 1
            event(new ArtistInformationFetched($artist, $info));
58
59
            // The artist may have been updated.
60 1
            $artist->refresh();
61 1
            $info['image'] = $artist->image;
62
        }
63
64 1
        return $info;
65
    }
66
}
67