WuBookApi::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of Laravel WuBook.
5
 *
6
 * (c) Filippo Galante <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace IlGala\LaravelWubook\Api;
13
14
use IlGala\LaravelWubook\Api\WuBookAuth;
15
use IlGala\LaravelWubook\Exceptions\WuBookException;
16
17
/**
18
 * This is the WuBook api abstract class.
19
 *
20
 * @author ilgala
21
 */
22
abstract class WuBookApi
23
{
24
25
    /**
26
     * @var array
27
     */
28
    protected $config;
29
30
    /**
31
     * @var Illuminate\Cache\Repository
32
     */
33
    protected $cache;
34
35
    /**
36
     * @var fXmlRpc\Client
37
     */
38
    protected $client;
39
40
    /**
41
     * @var IlGala\LaravelWubook\Api\WuBookAuth
42
     */
43
    protected $auth;
44
45
    /**
46
     * Creates a new WuBookAuth instance.
47
     *
48
     * @param array $config
49
     * @param Illuminate\Cache\Repository $cache
50
     * @param fXmlRpc\Client $client
51
     */
52
    public function __construct($config, Repository $cache, Client $client)
53
    {
54
        $this->config = $config;
55
        $this->cache = $cache;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cache of type object<IlGala\LaravelWubook\Api\Repository> is incompatible with the declared type object<IlGala\LaravelWub...inate\Cache\Repository> of property $cache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
        $this->client = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client of type object<IlGala\LaravelWubook\Api\Client> is incompatible with the declared type object<IlGala\LaravelWubook\Api\fXmlRpc\Client> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
        $this->auth = new WuBookAuth($config, $cache, $client);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \IlGala\LaravelWuboo...onfig, $cache, $client) of type object<IlGala\LaravelWubook\Api\WuBookAuth> is incompatible with the declared type object<IlGala\LaravelWub...lWubook\Api\WuBookAuth> of property $auth.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
    }
59
60
    /**
61
     * Prepends token and lcode or custom parameters
62
     *
63
     * @param mixed $params
64
     */
65
    protected function setup_client($params)
66
    {
67
        if (!is_array($params)) {
68
            // Setup params
69
            $params = [
0 ignored issues
show
Unused Code introduced by
$params is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70
                $this->get_token($params),
71
                $this->config['lcode']
72
            ];
73
        }
74
75
        $this->client->prependParams($token);
0 ignored issues
show
Bug introduced by
The variable $token does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
76
    }
77
78
    /**
79
     * Validate and, if necessary, retrieves a token or the cached token
80
     *
81
     * @param string $token
82
     * @return string
83
     * @throws WuBookException
84
     */
85
    protected function get_token($token)
86
    {
87
        // Check token
88
        if (empty($token) && $this->config['cache_token']) {
89
            $token = $this->cache->get('wubook.token');
90
        }
91
92
        $response = $this->auth->is_token_valid($token, $this->config['cache_token']);
93
94
        if (is_int($response)) {
95
            // If response is integer => valid token
96
            return $token;
97
        } elseif (is_string($response)) {
98
            // If response is string => new token
99
            return $response;
100
        } else {
101
            // Error
102
            throw new WuBookException('Token is empty or invalid');
103
        }
104
    }
105
106
    /**
107
     * Calls a wired API function.
108
     *
109
     * @param string $token
110
     * @param string $method
111
     * @param array $data
112
     * @return array
113
     * @throws WuBookException
114
     */
115
    protected function call_method($token, $method, $data = [])
116
    {
117
        if ($this->config['cache_token']) {
118
            // Check total operations
119
            $operations = $this->cache->get('wubook.token.ops');
120
            $operations++;
121
122
            if ($operations >= 60) {
123
                // Exceeded max ops, renew token
124
                $this->auth->release_token($this->get_token($token));
125
                $this->auth->acquire_token();
126
            }
127
        }
128
129
        // Setup client
130
        $this->setup_client($token);
131
132
        try {
133
            // Retrieve response
134
            $response = $this->client->call($method, $data);
135
136
            return [
137
                'has_error' => $response[0] != 0,
138
                'data' => $response[1]
139
            ];
140
        } catch (AbstractTransportException $error) {
0 ignored issues
show
Bug introduced by
The class IlGala\LaravelWubook\Api...tractTransportException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
141
            throw new WuBookException($error->getMessage(), $error->getCode(), $error);
142
        } finally {
143
            if ($this->config['cache_token']) {
144
                // Increment total operations
145
                $this->cache->increment('wubook.token.ops');
146
            }
147
        }
148
    }
149
}
150