Completed
Pull Request — master (#29)
by vincent
02:44
created

Collection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * This file is part of the Tmdb package.
4
 *
5
 * (c) Vincent Faliès <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author Vincent Faliès <[email protected]>
11
 * @copyright Copyright (c) 2017
12
 */
13
14
15
namespace vfalies\tmdb\Items;
16
17
use vfalies\tmdb\Abstracts\Item;
18
use vfalies\tmdb\Interfaces\Items\CollectionInterface;
19
use vfalies\tmdb\lib\Guzzle\Client as HttpClient;
20
use vfalies\tmdb\Exceptions\NotFoundException;
21
use vfalies\tmdb\Traits\ElementTrait;
22
use vfalies\tmdb\Results\Image;
23
use vfalies\tmdb\Interfaces\TmdbInterface;
24
25
/**
26
 * Class to manipulate a collection
27
 * @package Tmdb
28
 * @author Vincent Faliès <[email protected]>
29
 * @copyright Copyright (c) 2017
30
 */
31
class Collection extends Item implements CollectionInterface
32
{
33
34
    use ElementTrait;
35
36
    /**
37
     * Data
38
     * @var \stdClass
39
     */
40
    protected $data = null;
41
    /**
42
     * Configuration
43
     * @var \stdClass
44
     */
45
    protected $conf = null;
46
    /**
47
     * Id
48
     * @var int
49
     */
50
    protected $id = null;
51
    /**
52
     * Tmdb object
53
     * @var TmdbInterface
54
     */
55
    protected $tmdb = null;
56
57
    /**
58
     * Constructor
59
     * @param \vfalies\tmdb\Interfaces\TmdbInterface $tmdb
60
     * @param int $collection_id
61
     * @param array $options
62
     */
63 19
    public function __construct(TmdbInterface $tmdb, $collection_id, array $options = array())
64
    {
65 19
        parent::__construct($tmdb, $collection_id, $options, 'collection');
66 18
    }
67
68
    /**
69
     * Get collection ID
70
     * @return int
71
     */
72 2
    public function getId()
73
    {
74 2
        return $this->id;
75
    }
76
77
    /**
78
     * Get collection name
79
     * @return string
80
     * @throws NotFoundException
81
     */
82 2
    public function getName()
83
    {
84 2
        if (isset($this->data->name))
85
        {
86 1
            return $this->data->name;
87
        }
88 1
        $this->logger->error('Collection name not found', array('collection_id' => $this->id));
89 1
        throw new NotFoundException;
90
    }
91
92
    /**
93
     * Get collection parts
94
     * @return Generator
95
     */
96 2
    public function getParts()
97
    {
98 2
        if (!empty($this->data->parts))
99
        {
100 1
            foreach ($this->data->parts as $part)
101
            {
102 1
                $movie = new \vfalies\tmdb\Results\Movie($this->tmdb, $part);
103 1
                yield $movie;
104
            }
105
        }
106 2
    }
107
108
    /**
109
     * Get collection backdrops
110
     * @return \Generator|Results\Image
111
     */
112 9
    public function getBackdrops()
113
    {
114 9
        $data = $this->tmdb->sendRequest(new HttpClient(new \GuzzleHttp\Client()), '/collection/' . (int) $this->id . '/images', null, $this->params);
115
116 9
        foreach ($data->backdrops as $b)
117
        {
118 9
            $image = new Image($this->tmdb, $this->id, $b);
119 9
            yield $image;
120
        }
121 1
    }
122
123
    /**
124
     * Get collection posters
125
     * @return \Generator|Results\Image
126
     */
127 1
    public function getPosters()
128
    {
129 1
        $data = $this->tmdb->sendRequest(new HttpClient(new \GuzzleHttp\Client()), '/collection/' . (int) $this->id . '/images', null, $this->params);
130
131 1
        foreach ($data->posters as $b)
132
        {
133 1
            $image = new Image($this->tmdb, $this->id, $b);
134 1
            yield $image;
135
        }
136 1
    }
137
}
138