Passed
Push — feature/cache ( 84566e )
by Oguzhan
04:37
created

Artist   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 157
Duplicated Lines 7.64 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 72.72%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 12
loc 157
c 3
b 0
f 0
wmc 17
lcom 1
cbo 5
ccs 32
cts 44
cp 0.7272
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setParent() 0 6 1
A getParent() 0 4 1
A setCache() 0 6 1
A get() 0 5 1
A getComplete() 0 5 1
A getById() 0 4 1
A getByName() 0 4 1
A getByGuid() 0 4 1
A transformSingle() 0 17 3
A transformCollection() 12 12 4
A transform() 0 4 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
 * This file is part of the Pbxg33k\MusicInfo package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * (c) 2017 Oguzhan uysal. All rights reserved
9
 ******************************************************************************/
10
11
namespace Pbxg33k\MusicInfo\Service\Spotify\Endpoint;
12
13
use Doctrine\Common\Collections\ArrayCollection;
14
use Pbxg33k\MusicInfo\Exception\MethodNotImplementedException;
15
use Pbxg33k\MusicInfo\Model\Artist as ArtistModel;
16
use Pbxg33k\MusicInfo\Model\IMusicServiceEndpoint;
17
use Pbxg33k\MusicInfo\Service\Spotify\Service as SpotifyService;
18
use Psr\Cache\CacheItemPoolInterface;
19
20
class Artist implements IMusicServiceEndpoint
21
{
22
    const DATA_SOURCE = 'spotify';
23
24
    /**
25
     * @var SpotifyService
26
     */
27
    protected $parent;
28
29
    protected $cache;
30
31 39
    public function __construct(SpotifyService $apiService, CacheItemPoolInterface $cache)
32
    {
33 39
        $this->parent = $apiService;
34 39
        $this->setCache($cache);
35 39
    }
36
37
    /**
38
     * @param $parent
39
     *
40
     * @return $this
41
     */
42
    public function setParent($parent)
43
    {
44
        $this->parent = $parent;
45
46
        return $this;
47
    }
48
49
    /**
50
     * @return SpotifyService
51
     */
52 6
    public function getParent()
53
    {
54 6
        return $this->parent;
55
    }
56
57
    /**
58
     * @param CacheItemPoolInterface $cacheItemPool
59
     * @return $this
60
     */
61 39
    public function setCache(CacheItemPoolInterface $cacheItemPool)
62
    {
63 39
        $this->cache = $cacheItemPool;
64
65 39
        return $this;
66
    }
67
68
    /**
69
     * @param $arguments
70
     *
71
     * @return void
72
     *
73
     * @throws MethodNotImplementedException
74
     */
75
    public function get($arguments)
76
    {
77
        throw new MethodNotImplementedException;
78
        // TODO: Implement get() method.
79
    }
80
81
    /**
82
     * @param $arguments
83
     *
84
     * @return void
85
     *
86
     * @throws MethodNotImplementedException
87
     */
88
    public function getComplete($arguments)
89
    {
90
        throw new MethodNotImplementedException;
91
        // TODO: Implement getComplete() method.
92
    }
93
94
    /**
95
     * @param $id
96
     *
97
     * @return mixed
98
     */
99
    public function getById($id)
100
    {
101
        return $this->getById($id);
102
    }
103
104
    /**
105
     * @param $name
106
     *
107
     * @return mixed
108
     */
109 5
    public function getByName($name)
110
    {
111 5
        return $this->transform($this->getParent()->getApiClient()->search($name, 'artist'));
112
    }
113
114
    /**
115
     * @param $guid
116
     *
117
     * @return mixed
118
     */
119
    public function getByGuid($guid)
120
    {
121
        return $this->getParent()->getApiClient()->getArtist($guid);
122
    }
123
124
    /**
125
     * @param $raw
126
     *
127
     * @return ArtistModel
128
     */
129 5
    public function transformSingle($raw)
130
    {
131 5
        $object = new ArtistModel;
132
        $object
133 5
            ->setId($raw->id)
134 5
            ->setName($raw->name)
135 5
            ->setType($raw->type)
136 5
            ->setUri($raw->external_urls->spotify)
137 5
            ->setRawData($raw)
138 5
            ->setDataSource(self::DATA_SOURCE);
139
140 5
        if (is_array($raw->images) && count($raw->images) >= 1) {
141 5
            $object->setImage($raw->images[array_keys($raw->images)[0]]->url);
142 5
        }
143
144 5
        return $object;
145
    }
146
147
    /**
148
     * @param $raw
149
     *
150
     * @return ArrayCollection
151
     * @throws \Exception
152
     */
153 5 View Code Duplication
    public function transformCollection($raw)
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...
154
    {
155 5
        $collection = new ArrayCollection();
156 5
        if (is_object($raw) && isset($raw->artists)) {
157 5
            foreach ($raw->artists->items as $artist) {
158 5
                $collection->add($this->transformSingle($artist));
159 5
            }
160
161 5
            return $collection;
162
        }
163
        throw new \Exception('Transform failed');
164
    }
165
166
    /**
167
     * @param $raw
168
     *
169
     * @return ArrayCollection
170
     * @throws \Exception
171
     */
172 5
    public function transform($raw)
173
    {
174 5
        return $this->transformCollection($raw);
175
    }
176
}
177