Passed
Pull Request — master (#48)
by
unknown
12:04
created

ItemChanges   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 73
ccs 0
cts 30
cp 0
rs 10
c 1
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getChangesByKey() 0 6 3
A getChanges() 0 8 4
A __construct() 0 12 2
1
<?php declare(strict_types=1);
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 VfacTmdb\Abstracts\Items;
16
17
use VfacTmdb\Results;
18
use VfacTmdb\Exceptions\TmdbException;
19
use VfacTmdb\Interfaces\TmdbInterface;
20
21
/**
22
 * Abstract Item Changes class
23
 * @package Tmdb
24
 * @author Steve Richter <[email protected]>
25
 * @copyright Copyright (c) 2021
26
 */
27
abstract class ItemChanges
28
{
29
    /**
30
     * Tmdb object
31
     * @var TmdbInterface
32
     */
33
    protected $tmdb = null;
34
    /**
35
     * Logger
36
     * @var \Psr\Log\LoggerInterface
37
     */
38
    protected $logger = null;
39
    /**
40
     * Params
41
     * @var array
42
     */
43
    protected $params = [];
44
    /**
45
     * Data
46
     * @var \stdClass
47
     */
48
    protected $data = null;
49
50
    /**
51
     * Constructor
52
     * @param TmdbInterface $tmdb
53
     * @param string $item_type (possible values: movie, person, tv, tv/season, tv/episode)
54
     * @param int $item_id
55
     * @param array $options
56
     */
57
    public function __construct(TmdbInterface $tmdb, string $item_type, int $item_id, array $options = array())
58
    {
59
        try {
60
            $this->tmdb   = $tmdb;
61
            $this->logger = $tmdb->getLogger();
62
63
            $this->tmdb->checkOptionPage($options, $this->params);
64
            $this->tmdb->checkOptionDateRange($options, $this->params);
65
66
            $this->data = $this->tmdb->getRequest($item_type . '/' . $item_id . '/changes', $this->params);
67
        } catch (TmdbException $ex) {
68
            throw $ex;
69
        }
70
    }
71
72
    /**
73
     * Get Changes
74
     * @return \Generator|Results\ItemChange
75
     */
76
    public function getChanges() : \Generator
77
    {
78
        if (!empty($this->data->changes)) {
79
            foreach ($this->data->changes as $change) {
80
                foreach ($change->items as $item) {
81
                    $item->key = $change->key;
82
                    $itemChange = new Results\ItemChange($this->tmdb, $item);
83
                    yield $itemChange;
84
                }
85
            }
86
        }
87
    }
88
89
    /**
90
     * Get Changes by key
91
     * @param string $key
92
     * @return \Generator|Results\ItemChange
93
     */
94
    public function getChangesByKey(string $key) : \Generator
95
    {
96
        $itemChanges = $this->getChanges();
97
        foreach ($itemChanges as $change) {
98
            if ($change->getKey() === $key) {
99
                yield $change;
100
            }
101
        }
102
    }
103
}
104