Passed
Pull Request — master (#48)
by
unknown
05:28
created

ItemChanges   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 89
ccs 28
cts 28
cp 1
rs 10
c 2
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getChangesByKey() 0 6 3
A getChangeKeys() 0 9 2
A getChanges() 0 8 4
A __construct() 0 11 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 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->tmdb->checkOptionDateRange($options, $this->params);
64
65 66
            $this->data = $this->tmdb->getRequest($item_type . '/' . $item_id . '/changes', $this->params);
66 15
        } catch (TmdbException $ex) {
67 15
            throw $ex;
68
        }
69 66
    }
70
71
    /**
72
     * Get ItemChanges
73
     * @return \Generator|Results\ItemChange
74
     */
75 72
    public function getChanges() : \Generator
76
    {
77 72
        if (!empty($this->data->changes)) {
78 57
            foreach ($this->data->changes as $change) {
79 57
                foreach ($change->items as $item) {
80 57
                    $item->key = $change->key;
81 57
                    $itemChange = new Results\ItemChange($this->tmdb, $item);
82 57
                    yield $itemChange;
83
                }
84
            }
85
        }
86 30
    }
87
88
    /**
89
     * Get ItemChanges by key
90
     * @param string $key
91
     * @return \Generator|Results\ItemChange
92
     */
93 39
    public function getChangesByKey(string $key) : \Generator
94
    {
95 39
        $itemChanges = $this->getChanges();
96 39
        foreach ($itemChanges as $change) {
97 39
            if ($change->getKey() === $key) {
98 39
                yield $change;
99
            }
100
        }
101 12
    }
102
103
    /**
104
     * Get all ItemChange keys
105
     * @return array
106
     */
107 3
    public function getChangeKeys() : array
108
    {
109 3
        $changeKeys = [];
110 3
        $itemChanges = $this->getChanges();
111 3
        foreach ($itemChanges as $change) {
112 3
            $changeKeys[] = $change->getKey();
113
        }
114
115 3
        return $changeKeys;
116
    }
117
}
118