Completed
Branch testing (077765)
by AJ
02:44
created

ShopifyAPIComponent::startup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * CakePHPify : CakePHP Plugin for Shopify API Authentication
4
 * Copyright (c) Multidimension.al (http://multidimension.al)
5
 * Github : https://github.com/multidimension-al/cakephpify
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE file
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright     (c) Multidimension.al (http://multidimension.al)
12
 * @link          https://github.com/multidimension-al/cakephpify CakePHPify Github
13
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
14
 */
15
16
namespace Multidimensional\Shopify\Controller\Component;
17
18
use Cake\Core\Configure;
19
use Cake\Controller\Component;
20
use Cake\Routing\Router;
21
use Cake\Network\Http\Client;
22
use Cake\Event\Event;
23
24
class ShopifyAPIComponent extends Component {
0 ignored issues
show
Coding Style introduced by
The property $api_key is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $shop_domain is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $shared_secret is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $is_private_app is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $private_app_password is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
25
        
26
    public $api_key;
0 ignored issues
show
Coding Style introduced by
$api_key does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
27
    
28
    private $shop_domain;
0 ignored issues
show
Coding Style introduced by
$shop_domain does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
29
    private $token;
30
    private $shared_secret;
0 ignored issues
show
Coding Style introduced by
$shared_secret does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
31
    private $is_private_app;
0 ignored issues
show
Coding Style introduced by
$is_private_app does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
32
    private $private_app_password;
0 ignored issues
show
Coding Style introduced by
$private_app_password does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
33
    private $nonce;
34
        
35 14
    public function initialize(array $config = []) {
36
        
37 14
        parent::initialize($config);
38 14
        $this->api_key = ((isset($config['api_key'])) ? $config['api_key'] : Configure::read('Shopify.api_key'));
39 14
        $this->shared_secret = ((isset($config['shared_secret'])) ? $config['shared_secret'] : Configure::read('Shopify.shared_secret'));
40 14
        $this->scope = ((isset($config['scope'])) ? $config['scope'] : Configure::read('Shopify.scope'));
0 ignored issues
show
Bug introduced by
The property scope 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...
41 14
            $this->is_private_app = ((isset($config['is_private_app'])) ? $config['is_private_app'] : Configure::read('Shopify.is_private_app'));
42 14
        $this->private_app_password = ((isset($config['private_app_password'])) ? $config['private_app_password'] : Configure::read('Shopify.private_app_password'));        
43
        
44 14
    }
45
        
46 14
    public function startup(Event $event) {
47 14
        $this->setController($event->subject());
48 14
    }
49
        
50 14
    public function setController($controller) {
51 14
        $this->controller = $controller;
0 ignored issues
show
Bug introduced by
The property controller 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...
52 14
        if (!isset($this->controller->paginate)) {
53
            $this->controller->paginate = [];
54
        }
55 14
    }
56
57
    public function setShopDomain($shop_domain) {
0 ignored issues
show
Coding Style introduced by
$shop_domain does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
58
        return $this->shop_domain = $shop_domain;
0 ignored issues
show
Coding Style introduced by
$shop_domain does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
59
    }
60
    
61
    public function getShopDomain() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
62
        return $this->shop_domain;
63
    }
64
    
65
    public function setAccessToken($token) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
66
        return $this->token = $token;
67
    }
68
    
69
    public function callsMade() {
70
        return $this->shopApiCallLimitParam(0);
71
    }
72
73
    public function callLimit() {
74
        return $this->shopApiCallLimitParam(1);
75
    }
76
77
    public function callsLeft($response_headers) {
0 ignored issues
show
Coding Style introduced by
$response_headers does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Unused Code introduced by
The parameter $response_headers is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
        return $this->callLimit() - $this->callsMade();
79
    }
80
81
    /**
82
     * @param string $method
83
     * @param string $path
84
     */
85
    public function call($method, $path, $params = array()) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
86
        
87
        if (!$this->_isReady()) {
88
            return false;
89
        }
90
        
91
        if (!in_array($method, array('POST', 'PUT', 'GET', 'DELETE'))) {
92
            return false;    
93
        }
94
        
95
        $http = new Client([
96
            'host' => $this->shop_domain,
97
            'scheme' => 'https',
98
            'headers' => (($this->is_private_app != 'true') ? (['X-Shopify-Access-Token' => $this->token]) : []),
99
            'auth' => (($this->is_private_app != 'true') ? [] : (['username' => $this->api_key, 'password' => $this->private_app_password]))
100
        ]);
101
                            
102
        $this->response = $http->{strtolower($method)}(
103
            $path,
104
            ((in_array($method, array('POST', 'PUT'))) ? json_encode($params) : $params),
105
            ((in_array($method, array('POST', 'PUT'))) ? ['type' => 'json'] : [])
106
        );
107
        $this->response = $this->response->json;
108
109
        return (is_array($this->response) && (count($this->response) > 0)) ? array_shift($this->response) : $this->response;
110
        
111
    }
112
    
113
    /**
114
     * @param integer $index
115
     */
116
    private function shopApiCallLimitParam($index) {
117
        $params = explode("/", $this->response->getHeaderLine('http_x_shopify_shop_api_call_limit'));
0 ignored issues
show
Bug introduced by
The method getHeaderLine() does not exist on Cake\Network\Response. Did you maybe mean header()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
118
        return (int) $params[$index];
119
    }
120
    
121
    public function getAuthorizeUrl($shop_domain, $redirect_url) {
0 ignored issues
show
Coding Style introduced by
$shop_domain does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
122
                
123
        $url = 'https://' . $shop_domain . '/admin/oauth/authorize?client_id=' . $this->api_key;
0 ignored issues
show
Coding Style introduced by
$shop_domain does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
124
        $url .= '&scope=' . urlencode($this->scope);
125
        $url .= '&redirect_uri=' . urlencode($redirect_url);
0 ignored issues
show
Coding Style introduced by
$redirect_url does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
126
        $url .= '&state=' . $this->getNonce($shop_domain);
0 ignored issues
show
Coding Style introduced by
$shop_domain does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Unused Code introduced by
The call to ShopifyAPIComponent::getNonce() has too many arguments starting with $shop_domain.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
127
        return $url;
128
        
129
    }
130
131
    public function getAccessToken($shop_domain, $code) {
0 ignored issues
show
Coding Style introduced by
$shop_domain does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
132
    
133
        $this->shop_domain = $shop_domain;
0 ignored issues
show
Coding Style introduced by
$shop_domain does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
134
    
135
        $http = new Client([
136
            'host' => $shop_domain,
0 ignored issues
show
Coding Style introduced by
$shop_domain does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
137
            'scheme' => 'https'
138
        ]);
139
  
140
        $response = $http->post('/admin/oauth/access_token', 'client_id=' . $this->api_key . 
141
                                    '&client_secret=' . $this->shared_secret .
142
                                    '&code=' . $code);
143
        $response = $response->json; ;
144
        
145
        if (isset($response['access_token'])) {
146
            $this->token = $response['access_token'];
147
            return $this->token;
148
        } else {
149
            return false;
150
        }
151
    
152
        }
153
154
    public function setNonce($shop_domain) {
0 ignored issues
show
Coding Style introduced by
$shop_domain does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
155
        
156
        return $this->nonce = md5(strtolower($shop_domain));
0 ignored issues
show
Coding Style introduced by
$shop_domain does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
157
        
158
    }
159
160
    
161
    public function getNonce() {
162
        
163
        return $this->nonce;
164
        
165
    }
166
    
167
    public function validDomain($shop_domain) {
0 ignored issues
show
Coding Style introduced by
function validDomain() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
$shop_domain does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Unused Code introduced by
The parameter $shop_domain is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
168
    
169
        return true;
170
        
171
    }
172
    
173
    public function getShopData() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
174
    
175
        return $this->call('GET', '/admin/shop.json');
176
    
177
    }
178
    
179
    public function validateHMAC($query) {
0 ignored issues
show
Coding Style introduced by
function validateHMAC() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
180
            
181
        if (!is_array($query) || empty($query['hmac']) || !is_string($query['hmac']) || (isset($query['state']) && $query['state'] != $this->getNonce($query['shop']))) {
0 ignored issues
show
Unused Code introduced by
The call to ShopifyAPIComponent::getNonce() has too many arguments starting with $query['shop'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
182
            return false;
183
        }
184
 
185
        $dataString = array();
186
        
187
        foreach ($query as $key => $value) {
188
            $key = $this->_urlEncode(str_replace('=', '%3D', $key));
189
            $value = $this->_urlEncode($value);
190
            if ($key != 'hmac') {
191
                $dataString[] = $key . '=' . $value;
192
            }
193
        }
194
195
        sort($dataString);
196
        $string = implode("&", $dataString);
197
        return $query['hmac'] == hash_hmac('sha256', $string, $this->shared_secret);
198
    
199
        }
200
201
    /**
202
     * @param string $url
203
     */
204
    private function _urlEncode($url) {
205
    
206
        $url = str_replace('&', '%26', $url);
207
        $url = str_replace('%', '%25', $url);
208
        return $url;
209
        
210
    }
211
    
212
    private function _isReady() {
0 ignored issues
show
Coding Style introduced by
function _isReady() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
213
        return strlen($this->shop_domain) > 0 && strlen($this->token) > 0;
214
    }
215
    
216
}
217
218
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
219