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

Album   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 17.07%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 104
c 1
b 0
f 0
wmc 11
lcom 1
cbo 7
ccs 7
cts 41
cp 0.1707
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setCache() 0 6 1
A getParent() 0 4 1
A getByGuid() 0 4 1
A setParent() 0 4 1
A transformSingle() 0 21 1
A transformCollection() 0 13 3
A transform() 0 4 1
A getByName() 0 4 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
/**
12
 * Created by PhpStorm.
13
 * User: PBX_g33k
14
 * Date: 22-May-16
15
 * Time: 22:21
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\Album as AlbumEndpoint;
24
use Pbxg33k\MusicInfo\Model\Album as AlbumModel;
25
use Pbxg33k\VocaDB\Client;
26
use Pbxg33k\VocaDB\Models\Collections\AlbumCollection;
27
use Pbxg33k\VocaDB\Models\Album as VocaDBModel;
28
use Psr\Cache\CacheItemPoolInterface;
29
30
class Album extends AlbumEndpoint implements IMusicServiceEndpoint
31
{
32
    const DATA_SOURCE = 'vocadb';
33
34
    /**
35
     * @var CacheItemPoolInterface
36
     */
37
    protected $cache;
38
39 39
    public function __construct(Client $client, CacheItemPoolInterface $cache)
40
    {
41 39
        $this->setCache($cache);
42 39
        parent::__construct($client);
43 39
    }
44
45
    /**
46
     * @param CacheItemPoolInterface $cacheItemPool
47
     * @return $this
48
     */
49 39
    public function setCache(CacheItemPoolInterface $cacheItemPool)
50
    {
51 39
        $this->cache = $cacheItemPool;
52
53 39
        return $this;
54
    }
55
56
    /**
57
     * @return mixed
58
     */
59
    public function getParent()
60
    {
61
        // TODO: Implement getApiService() method.
62
    }
63
64
    /**
65
     * Aliases getById
66
     *
67
     * @param $guid
68
     *
69
     * @return mixed
70
     */
71
    public function getByGuid($guid)
72
    {
73
        return $this->getById($guid);
74
    }
75
76
    /**
77
     * @return mixed
78
     */
79
    public function setParent($apiService)
80
    {
81
        // TODO: Implement setApiService() method.
82
    }
83
84
    /**
85
     * @param VocaDBModel $raw
86
     * @return AlbumModel
87
     */
88
    public function transformSingle($raw)
89
    {
90
        $object = new AlbumModel();
91
        $object
92
            ->setId($raw->getId())
93
            ->setName($raw->getName())
94
            ->setArtists(
95
                new ArrayCollection(
96
                    [
97
                    (new ArtistModel())->setName($raw->getName())
98
                    ]
99
                )
100
            )
101
            ->setType($raw->getDiscType())
102
            ->setImage($raw->getMainPicture()->urlThumb)
103
            ->setReleaseDate(new \DateTime($raw->getReleaseDate()->formatted))
104
            ->setDataSource('vocadb')
105
            ->setRawData($raw);
106
107
        return $object;
108
    }
109
110
    public function transformCollection($raw)
111
    {
112
        $collection = new ArrayCollection();
113
        if ($raw instanceof AlbumCollection) {
114
            foreach ($raw->collection as $album) {
115
                $collection->add($this->transformSingle($album));
116
            }
117
118
            return $collection;
119
        }
120
121
        throw new \Exception('Transform failed. Object is not an instance of AlbumCollection');
122
    }
123
124
    public function transform($raw)
125
    {
126
        return $this->transformCollection($raw);
127
    }
128
129
    public function getByName($name, $complete = true)
130
    {
131
        return $this->transform(parent::getByName($name, $complete));
132
    }
133
}
134