Completed
Push — master ( 076ad5...3005ec )
by AJ
14:22
created

InstallController::index()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 25
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 23
nc 5
nop 0
crap 56
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\Cakephpify\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\Cakephpify\Controller\AppController;
24
25
class InstallController extends AppController
26
{
27
28
    private $error;
29
30
    public function initialize()
31
    {
32
33
        parent::initialize();
34
        $this->loadComponent('Multidimensional/Cakephpify.ShopifyDatabase');
35
        $this->loadComponent('Multidimensional/Cakephpify.ShopifyAPI', ['api_key' => $this->request->api_key]);
36
        $this->loadComponent('Flash');
37
        $this->error = false;
38
39
    }
40
41
    public function add() {
42
43
        $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...
44
        
45
        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...
46
        
47
            $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...
Unused Code introduced by
$access_token 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...
48
                $this->request->query['shop'],
49
                $this->request->query['code']
50
            );
51
52
            if ($accessToken) {
53
                $shop = $this->ShopifyAPI->getShopData();
54
55
                if (isset($shop['id'])) {
56
                    $shopEntity = $this->ShopifyDatabase->shopDataToDatabase($shop);
57
58
                    if ($shopEntity) {
59
                        $accessTokenEntity = $this->ShopifyDatabase->accessTokenToDatabase(
60
                            $accessToken,
0 ignored issues
show
Bug introduced by
The variable $accessToken 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...
61
                            $shopEntity->id,
62
                            $this->ShopifyAPI->api_key
63
                        );
64
65
                        if ($accessTokenEntity) {
66
                            $this->request->session()->write([
67
                                'shopify_access_token_' . $this->ShopifyAPI->api_key => $accessToken,
68
                                'shopify_shop_domain_' . $this->ShopifyAPI->api_key => $this->ShopifyAPI->getShopDomain()
69
                            ]);
70
71
                        
72
							$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...
Bug introduced by
The variable $shop_entity 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...
73
74
                            return $this->redirect([
75
                                'controller' => 'Shopify',
76
                                'plugin' => false,
77
								'api_key' => $this->ShopifyAPI->api_key]);
78
                                
79
                        } else {
80
                            $this->Flash->set("Error saving access token. Please try again.");
81
                        }
82
                    } else {
83
                        $this->Flash->set("Error inserting Shopify shop data. Please try again.");
84
                    }
85
                } else {
86
                    $this->Flash->set("Error accessing Shopify API. Please try again later.");
87
                }
88
            } else {
89
                $this->Flash->set("Invalid access token. Pleasy try again.");
90
            }
91
        } else {
92
            $this->Flash->set("Invalid authoization code. Please try again.");
93
        }
94
95
        $this->error = true;
96
        $this->render('index');
97
    }
98
99
    public function index()
100
    {
101
102
        if (!empty($this->request->query['code']) && !$this->error) {
103
104
            $this->render('add');
105
              
106
          } elseif (!empty($this->request->data['shop_domain']) && !$this->error) {
107
            
108
            $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...
Unused Code introduced by
$valid_domain 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...
109
                $this->request->data['shop_domain']
110
            );
111
112
            if ($validDomain) {
0 ignored issues
show
Bug introduced by
The variable $validDomain 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...
113
                $this->request->session()->write([
114
                    'shopify_shop_domain_' . $this->ShopifyAPI->api_key => $this->request->data['shop_domain']
115
                ]);
116
117
                $redirectUrl = Router::url([
118
                    'controller' => 'Install',
119
                    'action' => 'add',
120
                    'plugin' => 'Multidimensional/Cakephpify',
121
                    'api_key' => $this->ShopifyAPI->api_key
122
                ], true);
123
124
                $authUrl = $this->ShopifyAPI->getAuthorizeUrl(
125
                    $this->request->data['shop_domain'],
126
                    $redirectUrl
127
                );
128
129
                $this->redirect($authUrl);
130
            } else {
131
                $this->Flash->set("Invalid Shopify Domain");
132
            }
133
        } elseif (!empty($this->error)) {
134
            $this->Flash->set($this->error);
135
        }
136
    }
137
138
    public function redirect($url, $status = 302)
139
    {
140
141
        $this->set('shopify_auth_url', $url);
142
        $this->render('redirect');
143
    }
144
}
145