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

Track::getByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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