ItemChanges::getChanges()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
nc 4
nop 0
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 4
rs 10
c 1
b 0
f 0
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 81
    public function __construct(TmdbInterface $tmdb, string $item_type, int $item_id, array $options = array())
58
    {
59
        try {
60 81
            $this->tmdb   = $tmdb;
61 81
            $this->logger = $tmdb->getLogger();
62
63 81
            $this->logger->debug('Starting Item Changes', array('item_type' => $item_type, 'item_id' => $item_id, 'options' => $options));
64
65 81
            $this->tmdb->checkOptionDateRange($options, $this->params);
66
67 66
            $this->data = $this->tmdb->getRequest($item_type . '/' . $item_id . '/changes', $this->params);
68 15
        } catch (TmdbException $ex) {
69 15
            throw $ex;
70
        }
71 66
    }
72
73
    /**
74
     * Get ItemChanges
75
     * @return \Generator|Results\ItemChange
76
     */
77 72
    public function getChanges() : \Generator
78
    {
79 72
        if (!empty($this->data->changes)) {
80 57
            foreach ($this->data->changes as $change) {
81 57
                foreach ($change->items as $item) {
82 57
                    $item->key = $change->key;
83 57
                    $itemChange = new Results\ItemChange($this->tmdb, $item);
84 57
                    yield $itemChange;
85
                }
86
            }
87
        }
88 30
    }
89
90
    /**
91
     * Get ItemChanges by key
92
     * @param string $key
93
     * @return \Generator|Results\ItemChange
94
     */
95 39
    public function getChangesByKey(string $key) : \Generator
96
    {
97 39
        $itemChanges = $this->getChanges();
98 39
        foreach ($itemChanges as $change) {
99 39
            if ($change->getKey() === $key) {
100 39
                yield $change;
101
            }
102
        }
103 12
    }
104
105
    /**
106
     * Get all ItemChange keys
107
     * @return array
108
     */
109 3
    public function getChangeKeys() : array
110
    {
111 3
        $changeKeys = [];
112 3
        $itemChanges = $this->getChanges();
113 3
        foreach ($itemChanges as $change) {
114 3
            $changeKeys[] = $change->getKey();
115
        }
116
117 3
        return $changeKeys;
118
    }
119
}
120