Synchronizer::syncToPlaylist()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 5
cp 1
cc 1
eloc 5
nc 1
nop 3
crap 1
1
<?php
2
3
namespace HansOtt\Lastify;
4
5
use HansOtt\Lastify\Exception\PlaylistDoesNotExist;
6
use InvalidArgumentException;
7
8
final class Synchronizer
9
{
10
    /**
11
     * @var CanManagePlaylists The target.
12
     */
13
    private $target;
14
15
    /**
16
     * Synchronizer constructor.
17
     *
18
     * @param CanManagePlaylists $target The target.
19
     */
20 6
    public function __construct(CanManagePlaylists $target)
21
    {
22 6
        $this->target = $target;
23 6
    }
24
25
    /**
26
     * Sync a track collection to a playlist.
27
     *
28
     * @param string $name The name of the target playlist.
29
     * @param TrackCollection $source The track collection to sync to the playlist.
30
     * @param SyncProgressCallback|null $callback The progress callback.
31
     *
32
     * @return SyncResult
33
     */
34 6
    public function syncToPlaylist($name, TrackCollection $source, SyncProgressCallback $callback = null)
35
    {
36 6
        $this->assertValidPlaylistName($name);
37 3
        $syncProgress = $source->createSyncProgress($callback);
38 3
        $playlistId = $this->getPlaylistId($name);
39
40 3
        return $this->target->replacePlaylistTracks($playlistId, $source, $syncProgress);
41
    }
42
43
    /**
44
     * Assert that the playlist name is valid.
45
     *
46
     * @param string $name
47
     */
48 6 View Code Duplication
    private function assertValidPlaylistName($name)
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 6
        if (!is_string($name)) {
51
            throw new InvalidArgumentException('The playlist name should be a string, instead got:' . gettype($name));
52
        }
53 6
        if (empty($name)) {
54 3
            throw new InvalidArgumentException('The playlist name cannot be empty');
55
        }
56 3
    }
57
58
    /**
59
     * Get the playlist id of a playlist.
60
     *
61
     * If the playlist not exists, the playlist will be created.
62
     *
63
     * @param string $name
64
     *
65
     * @return string The playlist id.
66
     */
67 3
    private function getPlaylistId($name)
68
    {
69
        try {
70 3
            return $this->target->getPlaylistId($name);
71 3
        } catch (PlaylistDoesNotExist $e) {
72 3
            return $this->target->createPlaylist($name);
73
        }
74
    }
75
}
76