Passed
Push — master ( fc0106...465d67 )
by vincent
51s
created

Collection::getBackdrops()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
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
     * Configuration
38
     * @var \stdClass
39
     */
40
    protected $conf = null;
41
    /**
42
     * Id
43
     * @var int
44
     */
45
    protected $id = null;
46
    /**
47
     * Tmdb object
48
     * @var TmdbInterface
49
     */
50
    protected $tmdb = null;
51
52
    /**
53
     * Constructor
54
     * @param \vfalies\tmdb\Interfaces\TmdbInterface $tmdb
55
     * @param int $collection_id
56
     * @param array $options
57
     */
58 19
    public function __construct(TmdbInterface $tmdb, $collection_id, array $options = array())
59
    {
60 19
        parent::__construct($tmdb, $collection_id, $options, 'collection');
61 18
    }
62
63
    /**
64
     * Get collection ID
65
     * @return int
66
     */
67 2
    public function getId()
68
    {
69 2
        return $this->id;
70
    }
71
72
    /**
73
     * Get collection name
74
     * @return string
75
     * @throws NotFoundException
76
     */
77 2
    public function getName()
78
    {
79 2
        if (isset($this->data->name))
80
        {
81 1
            return $this->data->name;
82
        }
83 1
        $this->logger->error('Collection name not found', array('collection_id' => $this->id));
84 1
        throw new NotFoundException;
85
    }
86
87
    /**
88
     * Get collection parts
89
     * @return Generator
90
     */
91 2
    public function getParts()
92
    {
93 2
        if (!empty($this->data->parts))
94
        {
95 1
            foreach ($this->data->parts as $part)
96
            {
97 1
                $movie = new \vfalies\tmdb\Results\Movie($this->tmdb, $part);
98 1
                yield $movie;
99
            }
100
        }
101 2
    }
102
103
    /**
104
     * Get collection backdrops
105
     * @return \Generator|Results\Image
106
     */
107 9
    public function getBackdrops()
108
    {
109 9
        $data = $this->tmdb->sendRequest(new HttpClient(new \GuzzleHttp\Client()), '/collection/' . (int) $this->id . '/images', null, $this->params);
110
111 9
        foreach ($data->backdrops as $b)
112
        {
113 9
            $image = new Image($this->tmdb, $this->id, $b);
114 9
            yield $image;
115
        }
116 1
    }
117
118
    /**
119
     * Get collection posters
120
     * @return \Generator|Results\Image
121
     */
122 1
    public function getPosters()
123
    {
124 1
        $data = $this->tmdb->sendRequest(new HttpClient(new \GuzzleHttp\Client()), '/collection/' . (int) $this->id . '/images', null, $this->params);
125
126 1
        foreach ($data->posters as $b)
127
        {
128 1
            $image = new Image($this->tmdb, $this->id, $b);
129 1
            yield $image;
130
        }
131 1
    }
132
}
133