Artist::transformCollection()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 7
cts 8
cp 0.875
rs 9.8666
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 4.0312
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
19
class Artist implements IMusicServiceEndpoint
20
{
21
    const DATA_SOURCE = 'spotify';
22
    /**
23
     * @var SpotifyService
24
     */
25
    protected $parent;
26
27 39
    public function __construct(SpotifyService $apiService)
28
    {
29 39
        $this->parent = $apiService;
30 39
    }
31
32
    /**
33
     * @param $parent
34
     *
35
     * @return $this
36
     */
37
    public function setParent($parent)
38
    {
39
        $this->parent = $parent;
40
41
        return $this;
42
    }
43
44
    /**
45
     * @return SpotifyService
46
     */
47 6
    public function getParent()
48
    {
49 6
        return $this->parent;
50
    }
51
52
    /**
53
     * @param $arguments
54
     *
55
     * @return void
56
     *
57
     * @throws MethodNotImplementedException
58
     */
59
    public function get($arguments)
60
    {
61
        throw new MethodNotImplementedException;
62
        // TODO: Implement get() method.
63
    }
64
65
    /**
66
     * @param $arguments
67
     *
68
     * @return void
69
     *
70
     * @throws MethodNotImplementedException
71
     */
72
    public function getComplete($arguments)
73
    {
74
        throw new MethodNotImplementedException;
75
        // TODO: Implement getComplete() method.
76
    }
77
78
    /**
79
     * @param $id
80
     *
81
     * @return mixed
82
     */
83
    public function getById($id)
84
    {
85
        return $this->getById($id);
86
    }
87
88
    /**
89
     * @param $name
90
     *
91
     * @return mixed
92
     */
93 5
    public function getByName($name)
94
    {
95 5
        return $this->transform($this->getParent()->getApiClient()->search($name, 'artist'));
96
    }
97
98
    /**
99
     * @param $guid
100
     *
101
     * @return mixed
102
     */
103
    public function getByGuid($guid)
104
    {
105
        return $this->getParent()->getApiClient()->getArtist($guid);
106
    }
107
108
    /**
109
     * @param $raw
110
     *
111
     * @return ArtistModel
112
     */
113 5
    public function transformSingle($raw)
114
    {
115 5
        $object = new ArtistModel;
116
        $object
117 5
            ->setId($raw->id)
118 5
            ->setName($raw->name)
119 5
            ->setType($raw->type)
120 5
            ->setUri($raw->external_urls->spotify)
121 5
            ->setRawData($raw)
122 5
            ->setDataSource(self::DATA_SOURCE);
123
124 5
        if(is_array($raw->images) && count($raw->images) >= 1) {
125 5
            $object->setImage($raw->images[array_keys($raw->images)[0]]->url);
126 5
        }
127
128 5
        return $object;
129
    }
130
131
    /**
132
     * @param $raw
133
     *
134
     * @return ArrayCollection
135
     * @throws \Exception
136
     */
137 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...
138
    {
139 5
        $collection = new ArrayCollection();
140 5
        if (is_object($raw) && isset($raw->artists)) {
141 5
            foreach ($raw->artists->items as $artist) {
142 5
                $collection->add($this->transformSingle($artist));
143 5
            }
144
145 5
            return $collection;
146
        }
147
        throw new \Exception('Transform failed');
148
    }
149
150
    /**
151
     * @param $raw
152
     *
153
     * @return ArrayCollection
154
     * @throws \Exception
155
     */
156 5
    public function transform($raw)
157
    {
158 5
        return $this->transformCollection($raw);
159
    }
160
}