ShopifyInstallShell::main()   D
last analyzed

Complexity

Conditions 18
Paths 136

Size

Total Lines 121
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 5
Bugs 0 Features 0
Metric Value
dl 0
loc 121
ccs 0
cts 95
cp 0
rs 4.5072
c 5
b 0
f 0
cc 18
eloc 82
nc 136
nop 0
crap 342

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
 * 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
17
namespace Multidimensional\Cakephpify\Shell;
18
19
use Cake\Console\Helper;
20
use Cake\Console\Shell;
21
use Cake\Core\Configure;
22
use Migrations\Migrations;
23
24
class ShopifyInstallShell extends Shell
25
{
26
27
    /**
28
     * @return void
29
     */
30
    public function main()
31
    {
32
        $this->clear();
33
34
        $this->_io->styles('error', ['text' => 'red']);
35
        $this->helper('Multidimensional/Cakephpify.Header')->output();
0 ignored issues
show
Bug introduced by
The call to output() misses a required argument $args.

This check looks for function calls that miss required arguments.

Loading history...
36
37
        $firstRun = ((Configure::check('Multidimensional/Cakephpify')) ? false : true);
38
39
        //Activate Plugin
40
        if ((($firstRun) ? (strtolower($this->in('Install Shopify Plugin?', ['y', 'n'])) == 'y') : (strtolower($this->in('Update Configuration?', ['y', 'n'])) == 'y'))
41
        ) {
42
            $this->out();
43
            $this->out('Please enter your API credentials from your Shopify App page.', 2);
44
            $apiKey = $this->in('API Key:');
45
            $this->out();
46
47
            Configure::write('Multidimensional/Cakephpify.' . $apiKey . '.sharedSecret', $this->in('Shared Secret:'));
48
49
            $this->out();
50
51
            $scopeArray = [
52
                'read_content',
53
                'write_content',
54
                'read_themes',
55
                'write_themes',
56
                'read_products',
57
                'write_products',
58
                'read_customers',
59
                'write_customers',
60
                'read_orders',
61
                'write_orders',
62
                'read_script_tags',
63
                'write_script_tags',
64
                'read_fulfillments',
65
                'write_fulfillments',
66
                'read_shipping',
67
                'write_shipping',
68
                'read_analytics',
69
                'read_users',
70
                'write_users'];
71
72
            $this->out('Enter your application\'s scope here.', 2);
73
            $this->out('Valid scope options:', 2);
74
            $this->helper('Multidimensional/Cakephpify.Table')->output($scopeArray);
75
            $this->out('');
76
            $this->out('Separate your desired scope options with a comma.');
77
78
            do {
79
                $this->out('');
80
                $scope = $this->in('Scope:');
81
82
                $scope = explode(",", $scope);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal , 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...
83
84
                if (!is_array($scope)) {
85
                    $scope = [$scope];
86
                }
87
                $scope = array_map('trim', $scope);
88
                $scope = array_map('strtolower', $scope);
89
                array_walk(
90
                    $scope,
91
                    function (&$value) {
92
                        $value = str_replace(" ", "_", $value);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal 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...
Coding Style Comprehensibility introduced by
The string literal _ 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...
93
                    }
94
                );
95
96
                if ((count($scope)) && (strlen(trim(implode("", $scope))) > 0) && (count(array_diff($scope, $scopeArray)) > 0)) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal 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...
97
                    $this->out('');
98
                    $this->_io->out('<error>Invalid Scope. Try again, or leave blank to continue.</error>');
99
                }
100
101
                $count = count($scope);
102
                $scopeLength = strlen(trim(implode("", $scope)));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal 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...
103
                $scopeDiffCount = count(array_diff($scope, $scopeArray));
104
            } while ($count && $scopeLength > 0 && $scopeDiffCount > 0);
105
106
            Configure::write('Multidimensional/Cakephpify.' . $apiKey . '.scope', implode(',', $scope));
107
108
            $this->out('');
109
            $isPrivateApp = strtolower($this->in('Is this a private app?', ['y', 'n']));
110
111
            if ($isPrivateApp == 'y') {
112
                $isPrivateApp = 'true';
113
            } else {
114
                $isPrivateApp = 'false';
115
            }
116
117
118
            Configure::write('Multidimensional/Cakephpify.' . $apiKey . '.privateApp', $isPrivateApp);
119
120
            if ($isPrivateApp == 'true') {
121
                Configure::write('Multidimensional/Cakephpify.' . $apiKey . '.privateAppPassword', $this->in('Private App Password:'));
122
            } else {
123
                Configure::write('Multidimensional/Cakephpify.' . $apiKey . '.privateAppPassword', null);
124
            }
125
126
            Configure::dump('shopify', 'default', ['Multidimensional/Cakephpify']);
127
        }
128
129
        $this->out('');
130
131
        if (($firstRun) || (strtolower($this->in('Update Database?', ['y', 'n'])) == 'y')) {
132
            $this->out('');
133
134
            $migrations = new Migrations(['plugin' => 'Multidimensional/Cakephpify']);
135
136
            $status = $migrations->status();
137
138
            if ((is_array($status)) && (count($status))) {
139
                $this->out('Updating Databases', 2);
140
141
                if ($migrations->migrate()) {
142
                    $this->out('Success!', 2);
143
                } else {
144
                    $this->_io->out('<error>Update Failed!</error>', 2);
145
                }
146
            } else {
147
                $this->out('Shopify Database Files Not Found', 2);
148
            }
149
        }
150
    }
151
}
152