Synchronizer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 13.24 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 9
loc 68
rs 10
c 0
b 0
f 0
ccs 17
cts 18
cp 0.9444

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A syncToPlaylist() 0 8 1
A assertValidPlaylistName() 9 9 3
A getPlaylistId() 0 8 2

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 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