Completed
Branch account (edf133)
by vincent
02:17
created

Account::setListItem()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 5
nop 4
crap 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;
16
17
use VfacTmdb\Exceptions\TmdbException;
18
use VfacTmdb\Interfaces\TmdbInterface;
19
use VfacTmdb\Traits\GeneratorTrait;
20
21
/**
22
 * abstract account class
23
 * @package Tmdb
24
 * @author Vincent Faliès <[email protected]>
25
 * @copyright Copyright (c) 2017
26
 */
27
abstract class Account
28
{
29
    use GeneratorTrait;
30
31
    /**
32
     * Tmdb object
33
     * @var TmdbInterface
34
     */
35
    protected $tmdb = null;
36
    /**
37
     * Logger
38
     * @var \Psr\Log\LoggerInterface
39
     */
40
    protected $logger = null;
41
    /**
42
     * Session_id string
43
     * @var string
44
     */
45
    protected $auth = null;
46
    /**
47
     * Account id
48
     * @var int
49
     */
50
    protected $account_id;
51
    /**
52
     * Configuration array
53
     * @var \stdClass
54
     */
55
    protected $conf = null;
56
    /**
57
     * Options
58
     * @var array
59
     */
60
    protected $options = [];
61
    /**
62
     * Constructor
63
     * @param TmdbInterface $tmdb
64
     * @param string $session_id
65
     * @param int $account_id
66
     * @param array $options
67
     */
68 28
    public function __construct(TmdbInterface $tmdb, string $session_id, int $account_id, array $options = array())
69
    {
70 28
        $this->tmdb            = $tmdb;
71 28
        $options['session_id'] = $session_id;
72 28
        $this->logger          = $tmdb->getLogger();
73 28
        $this->options         = $this->tmdb->checkOptions($options);
74 28
        $this->account_id      = $account_id;
75
        // Configuration
76 28
        $this->conf            = $tmdb->getConfiguration();
77 28
        $this->setGeneratorTrait($tmdb);
78 28
    }
79
80
    /**
81
     * Add or remove item in list
82
     * @param string $list_type  Type of list (possible value : favorite / watchlist)
83
     * @param string $media_type type of media (movie / tv)
84
     * @param int    $media_id   media_id
85
     * @param bool   $add        add or remove item in list
86
     */
87 10
    protected function setListItem(string $list_type, string $media_type, int $media_id, bool $add)
88
    {
89
        try {
90 10
            $params               = [];
91 10
            $params['media_type'] = $media_type;
92 10
            $params['media_id']   = $media_id;
93 10
            $params[$list_type]   = $add;
94
95 10
            $this->tmdb->postRequest('account/'.$this->account_id.'/'.$list_type, $this->options, $params);
96
97 8
            return $this;
98 2
        } catch (TmdbException $e) {
99 2
            throw $e;
100
        }
101
    }
102
103
    /**
104
     * Get account favorite items
105
     * @param  string $list_type    type of list (possible value : favorite, rated, watchlist)
106
     * @param  string $item         item name, possible value : movies , tv , tv/episodes
107
     * @param  string $result_class class for the results
108
     * @return \Generator
109
     */
110 7
    protected function getAccountListItems(string $list_type, string $item, string $result_class) : \Generator
111
    {
112 7
        $response = $this->tmdb->getRequest('account/'.$this->account_id.'/'.$list_type.'/'.$item, $this->options);
113
114 7
        $this->page          = (int) $response->page;
0 ignored issues
show
Bug introduced by
The property page does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
115 7
        $this->total_pages   = (int) $response->total_pages;
0 ignored issues
show
Bug introduced by
The property total_pages does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
116 7
        $this->total_results = (int) $response->total_results;
0 ignored issues
show
Bug introduced by
The property total_results does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
117
118 7
        return $this->searchItemGenerator($response->results, $result_class);
119
    }
120
}
121