Completed
Push — master ( 6e1c98...aaa94a )
by AJ
14:00
created

InstallController::validate()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 65
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 0
cts 43
cp 0
rs 8.6195
c 0
b 0
f 0
cc 6
eloc 34
nc 6
nop 1
crap 42

How to fix   Long Method   

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
 * 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;
17
18
use Cake\Event\Event;
19
use Cake\Routing\Router;
20
use Cake\ORM\TableRegistry;
21
use Cake\Network\Session;
22
23
use Multidimensional\Shopify\Controller\AppController;
24
25
class InstallController extends AppController {
26
    
27
    private $error;
28
    
29
    public function initialize() {
30
31
        parent::initialize();
32
        $this->loadComponent('Multidimensional/Shopify.ShopifyDatabase');
33
        $this->loadComponent('Multidimensional/Shopify.ShopifyAPI', ['api_key' => $this->request->api_key]);
34
        $this->loadComponent('Flash');
35
        $this->error = false;
36
        		
37
    }
38
        
39
    public function add() {
40
41
        $is_authorized = $this->ShopifyAPI->validateHMAC($this->request->query);
0 ignored issues
show
Coding Style introduced by
$is_authorized 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...
42
        
43
        if ($is_authorized) {
0 ignored issues
show
Coding Style introduced by
$is_authorized 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...
44
        
45
            $access_token = $this->ShopifyAPI->getAccessToken(
0 ignored issues
show
Coding Style introduced by
$access_token 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...
46
                $this->request->query['shop'],
47
                $this->request->query['code']
48
            );
49
        
50
            if ($access_token) {
0 ignored issues
show
Coding Style introduced by
$access_token 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...
51
                
52
                $shop = $this->ShopifyAPI->getShopData();
53
                
54
                if (isset($shop['id'])) {
55
                
56
                    $shop_entity = $this->ShopifyDatabase->shopDataToDatabase($shop);
0 ignored issues
show
Coding Style introduced by
$shop_entity 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...
57
                    
58
                    if ($shop_entity) {
0 ignored issues
show
Coding Style introduced by
$shop_entity 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
                        $access_token_entity = $this->ShopifyDatabase->accessTokenToDatabase(
0 ignored issues
show
Coding Style introduced by
$access_token_entity 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...
61
                            $access_token,
0 ignored issues
show
Coding Style introduced by
$access_token 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...
62
                            $shop_entity->id,
0 ignored issues
show
Coding Style introduced by
$shop_entity 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...
63
                            $this->ShopifyAPI->api_key
64
                        );
65
                        
66
                        if ($access_token_entity) {
0 ignored issues
show
Coding Style introduced by
$access_token_entity 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...
67
                        
68
                            $this->request->session()->write([
69
                                'shopify_access_token_'.$this->ShopifyAPI->api_key => $access_token,
0 ignored issues
show
Coding Style introduced by
$access_token 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...
70
                                'shopify_shop_domain_'.$this->ShopifyAPI->api_key => $this->ShopifyAPI->getShopDomain()
71
                            ]);
72
                        
73
							$this->Auth->setUser($shop_entity);
0 ignored issues
show
Coding Style introduced by
$shop_entity 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...
74
75
                            return $this->redirect([
76
                                'controller' => 'Shopify',
77
                                'plugin' => false,
78
								'api_key' => $this->ShopifyAPI->api_key]);
79
                                
80
                            
81
                        } else {
82
                            $this->Flash->set("Error saving access token. Please try again.");
83
                        }
84
                    
85
                    } else {
86
                        $this->Flash->set("Error inserting Shopify shop data. Please try again.");
87
                    }
88
                    
89
                } else {
90
                    $this->Flash->set("Error accessing Shopify API. Please try again later.");    
91
                }
92
                
93
            } else {
94
                $this->Flash->set("Invalid access token. Pleasy try again.");
95
            }
96
            
97
        } else {
98
            $this->Flash->set("Invalid authoization code. Please try again.");
99
        }
100
        
101
        $this->error = true;
102
        $this->render('index');
103
        
104
    }
105
  
106
    public function index() {
107
                    
108
        if (!empty($this->request->query['code']) && !$this->error) {
109
      
110
            $this->render('add');
111
              
112
          } elseif (!empty($this->request->data['shop_domain']) && !$this->error) {
113
            
114
            $valid_domain = $this->ShopifyAPI->validDomain(
0 ignored issues
show
Coding Style introduced by
$valid_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...
115
                $this->request->data['shop_domain']
116
            );
117
            
118
            if ($valid_domain) {
0 ignored issues
show
Coding Style introduced by
$valid_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...
119
                
120
                $this->request->session()->write([
121
                    'shopify_shop_domain_'.$this->ShopifyAPI->api_key => $this->request->data['shop_domain']
122
                ]);
123
            
124
                $redirect_url = Router::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...
125
                    'controller' => 'Install',
126
                    'action' => 'add',
127
                    'plugin' => 'Multidimensional/Shopify',
128
                    'api_key' => $this->ShopifyAPI->api_key
129
                ], true);
130
                
131
                $auth_url = $this->ShopifyAPI->getAuthorizeUrl(
0 ignored issues
show
Coding Style introduced by
$auth_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...
132
                    $this->request->data['shop_domain'],
133
                    $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...
134
                );
135
                
136
                $this->redirect($auth_url);
0 ignored issues
show
Coding Style introduced by
$auth_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...
137
                
138
            } else {
139
                
140
                $this->Flash->set("Invalid Shopify Domain");
141
                
142
            }
143
            
144
        } elseif (!empty($this->error)) {
145
            
146
            $this->Flash->set($this->error);
147
148
        }
149
      
150
    }
151
    
152
    public function redirect($url, $status = 302) {
153
        
154
        $this->set('shopify_auth_url', $url);
155
        $this->render('redirect');
156
        
157
    }
158
  
159
}
160
161
?>
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...
162