Completed
Branch testing (d49f3a)
by AJ
02:08
created

ShopifyDatabaseComponent::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\Controller\Component;
19
use Cake\ORM\TableRegistry;
20
use Cake\Event\Event;
21
22
class ShopifyDatabaseComponent extends Component {
0 ignored issues
show
Coding Style introduced by
The property $access_tokens 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...
23
    
24
    private $shops;
25
    private $access_tokens;
0 ignored issues
show
Coding Style introduced by
$access_tokens 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...
26
    
27
    public $controller = null;
28
    
29 30
    public function initialize(array $config = []) {
30 30
        $this->shops = TableRegistry::get('Multidimensional/Shopify.Shops');
31 30
        $this->access_tokens = TableRegistry::get('Multidimensional/Shopify.AccessTokens');    
32 30
    }
33
    
34 30
    public function startup(Event $event) {
35 30
        $this->setController($event->subject());
36 30
    }
37
    
38 30
    public function setController($controller) {
39 30
        $this->controller = $controller;
40 30
        if (!isset($this->controller->paginate)) {
41
            $this->controller->paginate = [];
42
        }
43 30
    }
44
    
45
    public function shopDataToDatabase(array $data) {
46
        
47
        $shop_entity = $this->shops->newEntity();
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...
48
49
        unset($data['created_at']);
50
        unset($data['updated_at']);
51
                    
52
        $shop_entity->set($data);
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...
53
        
54
        $shop_entity->set(['updated_at' => new \DateTime('now')]);
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...
55
        
56
        if (!$shop_entity->errors() && $this->shops->save($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...
57
            return $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...
58
        } else {
59
            return false;    
60
        }
61
        
62
    }
63
    
64
        
65
    public function accessTokenToDatabase($access_token, $shop_id, $api_key) {
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...
66
        
67
        $access_token_entity = $this->access_tokens->newEntity();
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...
68
        
69
        $access_token_array = [
0 ignored issues
show
Coding Style introduced by
$access_token_array 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
            'shop_id' => $shop_id,
0 ignored issues
show
Coding Style introduced by
$shop_id 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...
71
            'api_key' => $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...
72
            'token' => $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...
73
    
74
        $access_token_entity->set($access_token_array);
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...
75
    
76
        $access_token_id = $this->access_tokens
0 ignored issues
show
Coding Style introduced by
$access_token_id 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...
77
            ->find()
78
            ->where($access_token_array)
0 ignored issues
show
Coding Style introduced by
$access_token_array 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...
79
            ->first();
80
    
81
        if ($access_token_id) {
0 ignored issues
show
Coding Style introduced by
$access_token_id 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...
82
        
83
            $access_token_entity->set([
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...
84
                'id' => $access_token_id->id,
0 ignored issues
show
Coding Style introduced by
$access_token_id 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...
85
                'updated_at' => new \DateTime('now')
86
            ]);
87
            
88
        }
89
                                    
90
        if (!$access_token_entity->errors() && $this->access_tokens->save($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...
91
            return $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...
92
        } else {
93
            return false;    
94
        }
95
        
96
    }
97
    
98
    public function getShopIdFromDomain($domain) {
99
        
100
        $shop_entity = $this->shops->findByMyshopifyDomain($domain)->first();
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...
101
        if ($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...
102
            return (int) $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...
103
        } else {
104
            return false;
105
        }
106
        
107
    }
108
    
109
    public function getShopDataFromAccessToken($access_token, $api_key) {
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...
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...
110
        
111
        $query = $this->access_tokens->find();
112
        $query = $query->contain(['Shops']);
113
        $query = $query->where(['api_key' => $api_key, 'token' => $access_token]);
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...
114
        $query = $query->where(function($exp, $q) {
0 ignored issues
show
Unused Code introduced by
The parameter $q 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...
115
            return $exp->isNull('expired_at');
116
        });
117
                
118
        $shop_entity = $query->first()->toArray();
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...
119
                                
120
        if (is_array($shop_entity['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...
121
            return $shop_entity['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...
122
        } else {
123
            return false;
124
        }
125
        
126
    }
127
    
128
    public function getAccessTokenFromShopDomain($shop_domain, $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...
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...
129
        
130
        $query = $this->access_tokens->find();
131
        $query = $query->contain(['Shops']);
132
        $query = $query->where(['api_key' => $api_key, 'Shops.myshopify_domain' => $shop_domain]);
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...
133
        $query = $query->where(function($exp, $q) {
0 ignored issues
show
Unused Code introduced by
The parameter $q 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...
134
            return $exp->isNull('expired_at');
135
        });
136
                
137
        $access_token_entity = $query->first();
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...
138
                
139
        if ($access_token_entity->token) {
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...
140
            return $access_token_entity->token;        
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...
141
        } else {
142
            return false;
143
        }
144
        
145
    }
146
    
147
    
148
}
149