Passed
Push — master ( d9de79...dbde73 )
by Rodrigo
01:37
created

CittaApi::request()   C

Complexity

Conditions 12
Paths 28

Size

Total Lines 61
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 29
c 1
b 0
f 0
nc 28
nop 2
dl 0
loc 61
rs 6.9666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use Yii;
0 ignored issues
show
Bug introduced by
The type Yii 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...
10
11
/** 
12
 *
13
 * @author Rodrigo Dornelles <[email protected]> <[email protected]>
14
 * @version 0.1  (28/04/2020)
15
 * 
16
 */
17
class CittaApi extends \yii\base\Model
0 ignored issues
show
Bug introduced by
The type yii\base\Model 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...
18
{   
19
    /**
20
     * @param string $url_reference
21
     * @param object $instance 
22
     */
23
    private static $url_reference;
24
    private static $instance;
25
26
    /** 
27
     * __construct
28
     * 
29
     * @see Sigleton Pattern
30
     *   
31
     */ 
32
    private function  __construct ( $config = [] )
33
    {
34
        parent::__construct( $config );
35
    }
36
37
    /**
38
     * url
39
     * 
40
     * @see Sigleton Pattern
41
     *
42
     * @param array|void $url_reference
43
     * @return object self
44
     * 
45
     */
46
    public static function url($uri = null)
47
    {   
48
        // change API URI
49
        if($uri !== null){
50
            self::$url_reference = $uri;
51
        }
52
53
        if(self::$instance === null){
54
            self::$instance = new self;
55
        }
56
57
        return self::$instance;
58
    }
59
60
    /**
61
     * request
62
     * 
63
     * dataProvide
64
     *
65
     * @param array|string $uri 
66
     * @return array|void $dataProviderParams
67
     * 
68
     * @throws CittaException
69
     * 
70
     */
71
    public static function request($uri, $dataProviderParams = [])
72
    {
73
        $curl = new \Curl\Curl();
74
        $redirects_count = 0;
75
76
        // base URI reference
77
        if (!self::$url_reference){
78
            throw new CittaException('URI Reference Error');
79
        }
80
        
81
        // consult URI
82
        if (is_array($uri)){
83
            $curl->get(self::$url_reference.\yii\helpers\Url::to($uri));
0 ignored issues
show
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...
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

83
            $curl->/** @scrutinizer ignore-call */ 
84
                   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...
84
85
        } else if (is_string($uri)){
0 ignored issues
show
introduced by
The condition is_string($uri) is always true.
Loading history...
86
            $curl->get(self::$url_reference."/{$uri}");
87
88
        } else {
89
            throw new CittaException('URI Type Error');
90
        }
91
92
93
        // redirect URI
94
        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...
95
            // probably loop
96
            if (++$redirects_count > 10){
97
                throw new CittaException('Too many redirects');
98
            }
99
100
            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...
101
                // ignore header
102
                if (strpos($header, 'Location:') === false){
103
                    continue;
104
                }
105
106
                // try new uri
107
                $curl->get(strtr($header,[
108
                        'Location: ' => '',
109
                        'location: ' => '',
110
                        'Location:' => '',
111
                        'location:' => ''
112
                    ])
113
                );
114
            }
115
        }
116
117
        // resquest error
118
        if ($curl->error && $curl->http_status_code){
0 ignored issues
show
Bug introduced by
The property error does not seem to exist on Curl\Curl.
Loading history...
119
            throw new CittaException("HTTP Status {$curl->http_status_code} Error");
120
        }
121
122
        // curl error
123
        if ($curl->error){
124
            throw new CittaException($curl->error_message);
0 ignored issues
show
Bug introduced by
The property error_message does not seem to exist on Curl\Curl.
Loading history...
125
        }
126
127
        // return response data
128
        return new \yii\data\ArrayDataProvider(
0 ignored issues
show
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...
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...
129
            \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...
130
                ['allModels' =>  \yii\helpers\Json::decode($curl->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...
Bug introduced by
The property response does not seem to exist on Curl\Curl.
Loading history...
131
                $dataProviderParams
132
            )
133
        );
134
    }
135
}