Completed
Push — master ( 4813ce...0bf679 )
by AJ
06:13
created

InstallController::add()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 59
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 0
cts 50
cp 0
rs 8.7117
c 0
b 0
f 0
cc 6
eloc 36
nc 6
nop 0
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\Cakephpify\Controller\Shopify;
17
18
use Cake\Event\Event;
19
use Cake\Network\Session;
20
use Cake\ORM\TableRegistry;
21
use Cake\Routing\Router;
22
use Multidimensional\Cakephpify\Controller\AppController;
23
24
class InstallController extends AppController
25
{
26
27
    private $error;
28
29
    /**
30
     * @return void
31
     */
32
    public function initialize()
33
    {
34
        parent::initialize();
35
        $this->loadComponent('Multidimensional/Cakephpify.ShopifyDatabase');
36
        $this->loadComponent('Multidimensional/Cakephpify.ShopifyAPI', ['apiKey' => $this->request->apiKey]);
37
        $this->loadComponent('Flash');
38
        $this->error = false;
39
    }
40
41
    /**
42
     * @return void|redirect
43
     */
44
    public function add()
45
    {
46
        $isAuthorized = $this->shopifyApi->validateHMAC($this->request->query);
47
48
        if ($isAuthorized) {
49
            $accessToken = $this->shopifyApi->getAccessToken(
50
                $this->request->query['shop'],
51
                $this->request->query['code']
52
            );
53
54
            if ($accessToken) {
55
                $shop = $this->shopifyApi->getShopData();
56
57
                if (isset($shop['id'])) {
58
                    $shopEntity = $this->shopifyDatabase->shopDataToDatabase($shop);
59
60
                    if ($shopEntity) {
61
                        $accessTokenEntity = $this->shopifyDatabase->accessTokenToDatabase(
62
                            $accessToken,
63
                            $shopEntity->id,
64
                            $this->shopifyApi->apiKey
65
                        );
66
67
                        if ($accessTokenEntity) {
68
                            $this->request->session()->write(
69
                                [
70
                                'shopify_access_token_' . $this->shopifyApi->apiKey => $accessToken,
71
                                'shopify_shop_domain_' . $this->shopifyApi->apiKey => $this->shopifyApi->getShopDomain()
72
                                ]
73
                            );
74
75
76
                            $this->Auth->setUser($shopEntity);
77
78
                            return $this->redirect(
79
                                [
80
                                'controller' => 'Shopify',
81
                                'plugin' => false,
82
                                'apiKey' => $this->shopifyApi->apiKey]
83
                            );
84
                        } else {
85
                            $this->Flash->set("Error saving access token. Please try again.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Error saving access token. Please try again. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
86
                        }
87
                    } else {
88
                        $this->Flash->set("Error inserting Shopify shop data. Please try again.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Error inserting Shopify ...data. Please try again. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
89
                    }
90
                } else {
91
                    $this->Flash->set("Error accessing Shopify API. Please try again later.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Error accessing Shopify ...Please try again later. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
92
                }
93
            } else {
94
                $this->Flash->set("Invalid access token. Pleasy try again.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Invalid access token. Pleasy try again. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
95
            }
96
        } else {
97
            $this->Flash->set("Invalid authoization code. Please try again.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Invalid authoization code. Please try again. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
98
        }
99
100
        $this->error = true;
101
        $this->render('index');
102
    }
103
104
    /**
105
     * @return void|redirect
106
     */
107
    public function index()
108
    {
109
        if (!empty($this->request->query['code']) && !$this->error) {
110
            $this->render('add');
111
        } elseif (!empty($this->request->data['shop_domain']) && !$this->error) {
112
            $validDomain = $this->shopifyApi->validDomain(
113
                $this->request->data['shop_domain']
114
            );
115
116
            if ($validDomain) {
117
                $this->request->session()->write(
118
                    [
119
                    'shopify_shop_domain_' . $this->shopifyApi->apiKey => $this->request->data['shop_domain']
120
                    ]
121
                );
122
123
                $redirectUrl = Router::url(
124
                    [
125
                    'controller' => 'Install',
126
                    'action' => 'add',
127
                    'plugin' => 'Multidimensional/Cakephpify',
128
                    'apiKey' => $this->shopifyApi->apiKey
129
                    ],
130
                    true
131
                );
132
133
                $authUrl = $this->shopifyApi->getAuthorizeUrl(
134
                    $this->request->data['shop_domain'],
135
                    $redirectUrl
136
                );
137
138
                $this->redirect($authUrl);
139
            } else {
140
                $this->Flash->set("Invalid Shopify Domain");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Invalid Shopify Domain does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
141
            }
142
        } elseif (!empty($this->error)) {
143
            $this->Flash->set($this->error);
144
        }
145
    }
146
147
    /**
148
     * @param string $url
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
149
     * @param int    $status
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
150
     * @return void
151
     */
152
    public function redirect($url, $status = 302)
153
    {
154
        $this->set('shopify_auth_url', $url);
155
        $this->render('redirect');
156
    }
157
}
158