Completed
Pull Request — develop (#15)
by Oguzhan
06:29 queued 03:34
created

Artist::transformSingle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 13
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
crap 1
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 View Code Duplication
class Artist implements IMusicServiceEndpoint
0 ignored issues
show
Duplication introduced by
This class 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...
20
{
21
    const DATA_SOURCE = 'spotify';
22
    /**
23
     * @var SpotifyService
24
     */
25
    protected $parent;
26
27 32
    public function __construct(SpotifyService $apiService)
28
    {
29 32
        $this->parent = $apiService;
30 32
    }
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 5
    public function getParent()
48
    {
49 5
        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 4
    public function getByName($name)
94
    {
95 4
        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 4
    public function transformSingle($raw)
114
    {
115 4
        $object = new ArtistModel;
116
        $object
117 4
            ->setId($raw->id)
118 4
            ->setName($raw->name)
119 4
            ->setType($raw->type)
120 4
            ->setUri($raw->external_urls->spotify)
121 4
            ->setRawData($raw)
122 4
            ->setDataSource(self::DATA_SOURCE);
123
124 4
        return $object;
125
    }
126
127
    /**
128
     * @param $raw
129
     *
130
     * @return ArrayCollection
131
     * @throws \Exception
132
     */
133 4
    public function transformCollection($raw)
134
    {
135 4
        $collection = new ArrayCollection();
136 4
        if (is_object($raw) && isset($raw->artists)) {
137 4
            foreach ($raw->artists->items as $artist) {
138 4
                $collection->add($this->transformSingle($artist));
139 4
            }
140
141 4
            return $collection;
142
        }
143
        throw new \Exception('Transform failed');
144
    }
145
146
    /**
147
     * @param $raw
148
     *
149
     * @return ArrayCollection
150
     * @throws \Exception
151
     */
152 4
    public function transform($raw)
153
    {
154 4
        return $this->transformCollection($raw);
155
    }
156
}