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

Album::setCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
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
use Psr\Cache\CacheItemPoolInterface;
20
21
class Album implements IMusicServiceEndpoint
22
{
23
    const DATA_SOURCE = 'spotify';
24
25
    /**
26
     * @var SpotifyService
27
     */
28
    protected $parent;
29
30 39
    public function __construct(SpotifyService $service, CacheItemPoolInterface $cache)
31
    {
32 39
        $this->parent = $service;
33 39
        $this->setCache($cache);
34 39
    }
35
36
    /**
37
     * @param $apiService
38
     *
39
     * @return mixed
40
     */
41
    public function setParent($apiService)
42
    {
43
        $this->parent = $apiService;
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param CacheItemPoolInterface $cacheItemPool
50
     * @return $this
51
     */
52 39
    public function setCache(CacheItemPoolInterface $cacheItemPool)
53
    {
54 39
        $this->cache = $cacheItemPool;
0 ignored issues
show
Bug introduced by
The property cache does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
56 39
        return $this;
57
    }
58
59
    /**
60
     * Transform single item to model
61
     *
62
     * @param $raw
63
     *
64
     * @return BaseModel
65
     */
66 2
    public function transformSingle($raw)
67
    {
68 2
        $object = new AlbumModel();
69
        $object
70 2
            ->setId($raw->id)
71 2
            ->setName($raw->name)
72 2
            ->setType($raw->album_type)
73 2
            ->setUri($raw->external_urls->spotify)
74 2
            ->setRawData($raw)
75 2
            ->setDataSource(self::DATA_SOURCE);
76
77 2
        return $object;
78
    }
79
80
    /**
81
     * @param $raw
82
     *
83
     * @return ArrayCollection
84
     * @throws \Exception
85
     */
86 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...
87
    {
88 2
        $collection = new ArrayCollection();
89 2
        if (is_object($raw) && isset($raw->albums)) {
90 2
            foreach ($raw->albums->items as $album) {
91 2
                $collection->add($this->transformSingle($album));
92 2
            }
93
94 2
            return $collection;
95
        }
96
97
        throw new \Exception('Transform failed');
98
    }
99
100
    /**
101
     * Transform to models
102
     *
103
     * @param $raw
104
     *
105
     * @return ArrayCollection
106
     */
107 2
    public function transform($raw)
108
    {
109 2
        return $this->transformCollection($raw);
110
    }
111
112
    /**
113
     * @return mixed
114
     */
115 2
    public function getParent()
116
    {
117 2
        return $this->parent;
118
    }
119
120
    /**
121
     * @param $arguments
122
     *
123
     * @return void
124
     *
125
     * @throws MethodNotImplementedException
126
     */
127
    public function get($arguments)
128
    {
129
        throw new MethodNotImplementedException();
130
        // TODO: Implement get() method.
131
    }
132
133
    /**
134
     * @param $arguments
135
     *
136
     * @return void
137
     *
138
     * @throws MethodNotImplementedException
139
     */
140
    public function getComplete($arguments)
141
    {
142
        throw new MethodNotImplementedException();
143
        // TODO: Implement getComplete() method.
144
    }
145
146
    /**
147
     * @param $id
148
     *
149
     * @return array|object
150
     */
151
    public function getById($id)
152
    {
153
        return $this->getByGuid($id);
154
    }
155
156
    /**
157
     * @param $name
158
     *
159
     * @return mixed
160
     */
161 2
    public function getByName($name)
162
    {
163 2
        return $this->transform($this->getParent()->getApiClient()->search($name, 'album'));
164
    }
165
166
    /**
167
     * @param $guid
168
     *
169
     * @return array|object
170
     */
171
    public function getByGuid($guid)
172
    {
173
        return $this->transform($this->getParent()->getApiClient()->getAlbum($guid));
174
    }
175
}
176