Completed
Push — feature/spotify ( 015a1d...74fcff )
by Oguzhan
02:19
created

Album::getByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Pbxg33k\MusicInfo\Service\Spotify\Endpoint;
3
4
use Doctrine\Common\Collections\ArrayCollection;
5
use Pbxg33k\MusicInfo\Exception\MethodNotImplementedException;
6
use Pbxg33k\MusicInfo\Model\BaseModel;
7
use Pbxg33k\MusicInfo\Model\IMusicServiceEndpoint;
8
use Pbxg33k\MusicInfo\Service\Spotify\Service as SpotifyService;
9
use Pbxg33k\MusicInfo\Model\Album as AlbumModel;
10
11 View Code Duplication
class Album 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...
12
{
13
    const DATA_SOURCE = 'spotify';
14
15
    /**
16
     * @var SpotifyService
17
     */
18
    protected $parent;
19
20
    public function __construct(SpotifyService $service)
21
    {
22
        $this->parent = $service;
23
    }
24
25
    /**
26
     * @param $apiService
27
     *
28
     * @return mixed
29
     */
30
    public function setParent($apiService)
31
    {
32
        $this->parent = $apiService;
33
34
        return $this;
35
    }
36
37
    /**
38
     * Transform single item to model
39
     *
40
     * @param $raw
41
     *
42
     * @return BaseModel
43
     */
44
    public function transformSingle($raw)
45
    {
46
        $object = new AlbumModel();
47
        $object
48
            ->setId($raw->id)
49
            ->setName($raw->name)
50
            ->setType($raw->album_type)
51
            ->setUri($raw->external_urls->spotify)
52
            ->setRawData($raw)
53
            ->setDataSource(self::DATA_SOURCE);
54
55
        return $object;
56
    }
57
58
    /**
59
     * @param $raw
60
     *
61
     * @return ArrayCollection
62
     * @throws \Exception
63
     */
64
    public function transformCollection($raw)
65
    {
66
        $collection = new ArrayCollection();
67
        if (is_object($raw) && isset($raw->albums)) {
68
            foreach ($raw->albums->items as $album) {
69
                $collection->add($this->transformSingle($album));
70
            }
71
72
            return $collection;
73
        }
74
75
        throw new \Exception('Transform failed');
76
    }
77
78
    /**
79
     * Transform to models
80
     *
81
     * @param $raw
82
     *
83
     * @return ArrayCollection
84
     */
85
    public function transform($raw)
86
    {
87
        return $this->transformCollection($raw);
88
    }
89
90
    /**
91
     * @return mixed
92
     */
93
    public function getParent()
94
    {
95
        return $this->parent;
96
    }
97
98
    /**
99
     * @param $arguments
100
     *
101
     * @return void
102
     *
103
     * @throws MethodNotImplementedException
104
     */
105
    public function get($arguments)
106
    {
107
        throw new MethodNotImplementedException();
108
        // TODO: Implement get() method.
109
    }
110
111
    /**
112
     * @param $arguments
113
     *
114
     * @return void
115
     *
116
     * @throws MethodNotImplementedException
117
     */
118
    public function getComplete($arguments)
119
    {
120
        throw new MethodNotImplementedException();
121
        // TODO: Implement getComplete() method.
122
    }
123
124
    /**
125
     * @param $id
126
     *
127
     * @return array|object
128
     */
129
    public function getById($id)
130
    {
131
        return $this->getByGuid($id);
132
    }
133
134
    /**
135
     * @param $name
136
     *
137
     * @return mixed
138
     */
139
    public function getByName($name)
140
    {
141
        return $this->transform($this->getParent()->getApiClient()->search($name, 'album'));
142
    }
143
144
    /**
145
     * @param $guid
146
     *
147
     * @return array|object
148
     */
149
    public function getByGuid($guid)
150
    {
151
        return $this->getParent()->getApiClient()->getAlbum($guid);
152
    }
153
}