Passed
Push — master ( e8259d...24138f )
by Rodrigo
01:16
created

CittaApi   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 39
c 2
b 0
f 0
dl 0
loc 132
rs 10
wmc 17

4 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 20 4
A url() 0 14 3
A __construct() 0 2 1
B run() 0 47 9
1
<?php
2
/**
3
 * @copyright Copyright (c) 2019 Dynamika Web
4
 * @link https://github.com/dynamikaweb/yii2-citta-api
5
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
6
 */
7
namespace dynamikaweb\api;
8
9
/** 
10
 *
11
 * @author Rodrigo Dornelles <[email protected]> <[email protected]>
12
 * @version 0.1  (28/04/2020)
13
 * 
14
 */
15
class CittaApi
16
{   
17
    /**
18
     * @param string $url_reference
19
     * @param object $instance 
20
     */
21
    private static $url_reference;
22
    private static $instance;
23
24
    /** 
25
     * __construct
26
     * 
27
     * @see Sigleton Pattern
28
     */ 
29
    private function  __construct ()
30
    {
31
32
    }
33
34
    /**
35
     * url
36
     * 
37
     * @see Sigleton Pattern
38
     *
39
     * @param array|void $url_reference
40
     * @return object self
41
     * 
42
     */
43
    public static function url($uri = null)
44
    {   
45
        // change API URI
46
        if($uri !== null){
47
            self::$url_reference = $uri;
48
        }
49
50
        // create object
51
        if(self::$instance === null){
52
            self::$instance = new self;
53
        }
54
55
        // unice instance
56
        return self::$instance;
57
    }
58
59
    /**
60
     * run
61
     * 
62
     * call @api
63
     * 
64
     * @param string|array $uri
65
     * @return object
66
     */
67
    private static function run($uri)
68
    {
69
        $curl = new \Curl\Curl();
70
        $redirects_count = 0;
71
72
        // base URI reference
73
        if (!self::$url_reference){
74
            throw new CittaException('URI Reference Error');
75
        }
76
        
77
        // consult URI
78
        if (is_array($uri)){
79
            $curl->get(self::$url_reference.\yii\helpers\Url::to($uri));
0 ignored issues
show
Bug introduced by
The method get() does not exist on Curl\Curl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
            $curl->/** @scrutinizer ignore-call */ 
80
                   get(self::$url_reference.\yii\helpers\Url::to($uri));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The type yii\helpers\Url was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
80
81
        } else if (is_string($uri)){
0 ignored issues
show
introduced by
The condition is_string($uri) is always true.
Loading history...
82
            $curl->get(self::$url_reference."/{$uri}");
83
84
        } else {
85
            throw new CittaException('URI Type Error');
86
        }
87
88
        // redirect URI
89
        while (300 <= $curl->http_status_code && $curl->http_status_code < 400){
0 ignored issues
show
Bug introduced by
The property http_status_code does not seem to exist on Curl\Curl.
Loading history...
90
            // probably loop
91
            if (++$redirects_count > 10){
92
                throw new CittaException('Too many redirects');
93
            }
94
95
            foreach ($curl->response_headers as $header) {
0 ignored issues
show
Bug introduced by
The property response_headers does not seem to exist on Curl\Curl.
Loading history...
96
                // ignore header
97
                if (strpos($header, 'Location:') === false){
98
                    continue;
99
                }
100
101
                // try new uri
102
                $curl->get(strtr($header,[
103
                        'Location: ' => '',
104
                        'location: ' => '',
105
                        'Location:' => '',
106
                        'location:' => ''
107
                    ])
108
                );
109
            }
110
        }
111
112
        // return request
113
        return $curl;
114
    }
115
116
    /**
117
     * request
118
     * 
119
     * dataProvider
120
     *
121
     * @param array|string $uri 
122
     * @return array|void $dataProviderParams
123
     * 
124
     * @throws CittaException
125
     * 
126
     */
127
    public static function request($uri, $dataProviderParams = [])
128
    {
129
        // cal; api
130
        $api = self::run($uri);
131
132
        // resquest error
133
        if ($api->error && $api->http_status_code){
134
            throw new CittaException("HTTP Status {$api->http_status_code} Error");
135
        }
136
137
        // curl error
138
        if ($api->error){
139
            throw new CittaException($api->error_message);
140
        }
141
142
        // return request
143
        return new \yii\data\ArrayDataProvider(
1 ignored issue
show
Bug Best Practice introduced by
The expression return new yii\data\Arra..., $dataProviderParams)) returns the type yii\data\ArrayDataProvider which is incompatible with the documented return type array.
Loading history...
Bug introduced by
The type yii\data\ArrayDataProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
144
            \yii\Helpers\ArrayHelper::merge(
0 ignored issues
show
Bug introduced by
The type yii\Helpers\ArrayHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
145
                ['allModels' =>  \yii\helpers\Json::decode($api->response, true)],
0 ignored issues
show
Bug introduced by
The type yii\helpers\Json was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
146
                $dataProviderParams
147
            )
148
        );
149
    }
150
}