Album::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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