Completed
Branch testing (077765)
by AJ
02:44
created

ShopifyDatabaseComponent   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 2.99%

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 6
dl 0
loc 125
ccs 2
cts 67
cp 0.0299
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 4 1
A startup() 0 3 1
A setController() 0 6 2
A shopDataToDatabase() 0 18 3
B accessTokenToDatabase() 0 32 4
A getShopIdFromDomain() 0 10 2
A getShopDataFromAccessToken() 0 18 2
A getAccessTokenFromShopDomain() 0 18 2
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 10
    public function initialize(array $config = []) {
28 10
        $this->shops = TableRegistry::get('Multidimensional/Shopify.Shops');
29
        $this->access_tokens = TableRegistry::get('Multidimensional/Shopify.AccessTokens');    
30
    }
31
    
32
    public function startup(Event $event) {
33
        $this->setController($event->subject());
34
    }
35
    
36
    public function setController($controller) {
37
        $this->controller = $controller;
0 ignored issues
show
Bug introduced by
The property controller does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
        if (!isset($this->controller->paginate)) {
39
            $this->controller->paginate = [];
40
        }
41
    }
42
    
43
    public function shopDataToDatabase(array $data) {
44
        
45
        $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...
46
47
        unset($data['created_at']);
48
        unset($data['updated_at']);
49
                    
50
        $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...
51
        
52
        $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...
53
        
54
        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...
55
            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...
56
        } else {
57
            return false;    
58
        }
59
        
60
    }
61
    
62
        
63
    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...
64
        
65
        $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...
66
        
67
        $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...
68
            '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...
69
            '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...
70
            '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...
71
    
72
        $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...
73
    
74
        $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...
75
            ->find()
76
            ->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...
77
            ->first();
78
    
79
        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...
80
        
81
            $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...
82
                '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...
83
                'updated_at' => new \DateTime('now')
84
            ]);
85
            
86
        }
87
                                    
88
        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...
89
            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...
90
        } else {
91
            return false;    
92
        }
93
        
94
    }
95
    
96
    public function getShopIdFromDomain($domain) {
97
        
98
        $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...
99
        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...
100
            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...
101
        } else {
102
            return false;
103
        }
104
        
105
    }
106
    
107
    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...
108
        
109
        $query = $this->access_tokens->find();
110
        $query = $query->contain(['Shops']);
111
        $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...
112
        $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...
113
            return $exp->isNull('expired_at');
114
        });
115
                
116
        $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...
117
                                
118
        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...
119
            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...
120
        } else {
121
            return false;
122
        }
123
        
124
    }
125
    
126
    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...
127
        
128
        $query = $this->access_tokens->find();
129
        $query = $query->contain(['Shops']);
130
        $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...
131
        $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...
132
            return $exp->isNull('expired_at');
133
        });
134
                
135
        $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...
136
                
137
        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...
138
            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...
139
        } else {
140
            return false;
141
        }
142
        
143
    }
144
    
145
    
146
}
147