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

Artist::transformSingle()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2.0006

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 26
c 1
b 0
f 0
ccs 17
cts 18
cp 0.9444
rs 8.8571
cc 2
eloc 19
nc 2
nop 1
crap 2.0006
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
/**
12
 * Created by PhpStorm.
13
 * User: PBX_g33k
14
 * Date: 22-May-16
15
 * Time: 22:14
16
 */
17
18
namespace Pbxg33k\MusicInfo\Service\VocaDB\Endpoint;
19
20
use Doctrine\Common\Collections\ArrayCollection;
21
use Pbxg33k\MusicInfo\Model\Artist as ArtistModel;
22
use Pbxg33k\MusicInfo\Model\IMusicServiceEndpoint;
23
use Pbxg33k\VocaDB\Artist as ArtistEndpoint;
24
use Pbxg33k\VocaDB\Models\Artist as VocaDBArtistModel;
25
use Pbxg33k\VocaDB\Client;
26
use Psr\Cache\CacheItemPoolInterface;
27
28
class Artist extends ArtistEndpoint implements IMusicServiceEndpoint
29
{
30
    /**
31
     *
32
     */
33
    const DATA_SOURCE = 'vocadb';
34
35
    /**
36
     * @var CacheItemPoolInterface
37
     */
38
    protected $cache;
39
40 39
    public function __construct(Client $client, CacheItemPoolInterface $cache)
41
    {
42 39
        $this->setCache($cache);
43 39
        parent::__construct($client);
44 39
    }
45
46
    /**
47
     * @param CacheItemPoolInterface $cacheItemPool
48
     * @return $this
49
     */
50 39
    public function setCache(CacheItemPoolInterface $cacheItemPool)
51
    {
52 39
        $this->cache = $cacheItemPool;
53
54 39
        return $this;
55
    }
56
57
    /**
58
     * @return mixed
59
     */
60
    public function getParent()
61
    {
62
        // TODO: Implement getApiService() method.
63
    }
64
65
    /**
66
     * Aliases getById
67
     *
68
     * @param $guid
69
     *
70
     * @return mixed
71
     */
72
    public function getByGuid($guid)
73
    {
74
        return $this->getById($guid);
75
    }
76
77
    /**
78
     * @return mixed
79
     */
80
    public function setParent($apiService)
81
    {
82
        // TODO: Implement setApiService() method.
83
    }
84
85 5
    public function getByName($name, $complete = true)
86
    {
87 5
        $cacheKey = substr(sprintf(
88 5
            '%s_get_%s',
89 5
            self::DATA_SOURCE,
90 5
            preg_replace('~[A-Za-z0-9\.\_]~', '_',$name)
91 5
        ),0,64);
92
93 5
        $cachedObject = $this->cache->getItem($cacheKey);
94
95 5
        if(!$cachedObject->isHit()) {
96 5
            $result = $this->transform(parent::getByName($name, $complete));
97 5
            $cachedObject->set($result);
98 5
        } else {
99
            $result = $cachedObject->get();
100
        }
101
102 5
        return $result;
103
    }
104
105
    /**
106
     * @param VocaDBArtistModel $raw
107
     *
108
     * @return ArtistModel
109
     */
110 5
    public function transformSingle($raw)
111
    {
112 5
        $cacheKey = substr(sprintf(
113 5
            '%s_transform_%s',
114 5
            self::DATA_SOURCE,
115 5
            preg_replace('~[A-Za-z0-9\.\_]~', '_',$raw->getDefaultName())
116 5
        ),0,64);
117
118 5
        $cachedObject = $this->cache->getItem($cacheKey);
119
120 5
        if(!$cachedObject->isHit()) {
121 5
            $object = new ArtistModel;
122
            $object
123 5
                ->setId($raw->getId())
124 5
                ->setName($raw->getName())
125 5
                ->setType($raw->getArtistType())
126 5
                ->setDataSource(self::DATA_SOURCE)
127 5
                ->setRawData($raw);
128 5
            $cachedObject->set($cachedObject);
129 5
        } else {
130
            /** @var ArtistModel $object */
131
            $object = $cachedObject->get();
132
        }
133
134 5
        return $object;
135
    }
136
137
    /**
138
     * @param $raw
139
     *
140
     * @return ArrayCollection
141
     */
142 5
    public function transformCollection($raw)
143
    {
144 5
        $collection = new ArrayCollection();
145 5
        foreach ($raw->collection as $artist) {
146 5
            $collection->add($this->transformSingle($artist));
147 5
        }
148
149 5
        return $collection;
150
    }
151
152
    /**
153
     * @param $raw
154
     *
155
     * @return ArrayCollection
156
     */
157 5
    public function transform($raw)
158
    {
159 5
        return $this->transformCollection($raw);
160
    }
161
}
162