Test Setup Failed
Branch master (4b8153)
by Phan
05:07
created

MediaMetadataService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Services;
4
5
use App\Models\Album;
6
use App\Models\Artist;
7
use Exception;
8
use Illuminate\Log\Logger;
9
10
class MediaMetadataService
11
{
12
    private $logger;
13
14
    public function __construct(Logger $logger)
15
    {
16
        $this->logger = $logger;
17
    }
18
19
    /**
20
     * Download a copy of the album cover.
21
     */
22
    public function downloadAlbumCover(Album $album, string $imageUrl): void
23
    {
24
        $extension = explode('.', $imageUrl);
25
        $this->writeAlbumCover($album, file_get_contents($imageUrl), last($extension));
26
    }
27
28
    /**
29
     * Copy a cover file from an existing image on the system.
30
     *
31 7
     * @param string $source      The original image's full path.
32
     * @param string $destination The destination path. Automatically generated if empty.
33 7
     */
34 7
    public function copyAlbumCover(Album $album, string $source, string $destination = ''): void
35 7
    {
36
        $extension = pathinfo($source, PATHINFO_EXTENSION);
37 7
        $destination = $destination ?: $this->generateAlbumCoverPath($extension);
38 7
        copy($source, $destination);
39
40
        $album->update(['cover' => basename($destination)]);
41
    }
42
43
    /**
44
     * Write an album cover image file with binary data and update the Album with the new cover attribute.
45
     *
46
     * @param string $destination The destination path. Automatically generated if empty.
47
     */
48 7 View Code Duplication
    public function writeAlbumCover(Album $album, string $binaryData, string $extension, string $destination = ''): void
0 ignored issues
show
Duplication introduced by
This method 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...
49
    {
50
        try {
51 7
            $extension = trim(strtolower($extension), '. ');
52 7
            $destination = $destination ?: $this->generateAlbumCoverPath($extension);
53 7
            file_put_contents($destination, $binaryData);
54
55 7
            $album->update(['cover' => basename($destination)]);
56
        } catch (Exception $e) {
57
            $this->logger->error($e);
58
        }
59 7
    }
60
61
    /**
62
     * Download a copy of the artist image.
63
     */
64
    public function downloadArtistImage(Artist $artist, string $imageUrl): void
65
    {
66
        $extension = explode('.', $imageUrl);
67
        $this->writeArtistImage($artist, file_get_contents($imageUrl), last($extension));
68
    }
69
70
    /**
71
     * Write an artist image file with binary data and update the Artist with the new image attribute.
72
     *
73
     * @param string $destination The destination path. Automatically generated if empty.
74
     */
75 View Code Duplication
    public function writeArtistImage(
0 ignored issues
show
Duplication introduced by
This method 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...
76
        Artist $artist,
77
        string $binaryData,
78
        string $extension,
79
        string $destination = ''
80
    ): void {
81 1
        try {
82
            $extension = trim(strtolower($extension), '. ');
83
            $destination = $destination ?: $this->generateArtistImagePath($extension);
84 1
            file_put_contents($destination, $binaryData);
85 1
86 1
            $artist->update(['image' => basename($destination)]);
87
        } catch (Exception $e) {
88 1
            $this->logger->error($e);
89
        }
90
    }
91
92 1
    /**
93
     * Generate a random path for an album cover image.
94
     *
95
     * @param string $extension The extension of the cover (without dot)
96
     */
97
    private function generateAlbumCoverPath($extension): string
98
    {
99
        return sprintf('%s/public/img/covers/%s.%s', app()->publicPath(), uniqid('', true), $extension);
100
    }
101 6
102
    /**
103 6
     * Generate a random path for an artist image.
104
     *
105
     * @param string $extension The extension of the cover (without dot)
106
     */
107
    private function generateArtistImagePath($extension): string
108
    {
109
        return sprintf('%s/public/img/artists/%s.%s', app()->publicPath(), uniqid('', true), $extension);
110
    }
111
}
112