Completed
Push — master ( e901cb...6ce8e1 )
by AJ
09:30
created

ShopifyDatabaseComponent::getShopIdFromDomain()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 7
cts 8
cp 0.875
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 1
crap 4.0312
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\Component;
17
18
use Cake\Controller\Component;
19
use Cake\Event\Event;
20
use Cake\ORM\TableRegistry;
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
25
    private $shops;
26
    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...
27
28
    public $controller = null;
29
30
    /**
31
     * @param array $config
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
32
     * @return void
33
     */
34 18
    public function initialize(array $config = [])
35
    {
36 18
        $this->shops = TableRegistry::get('Multidimensional/Cakephpify.Shops');
37 18
        $this->access_tokens = TableRegistry::get('Multidimensional/Cakephpify.AccessTokens');
38 18
    }
39
40
    /**
41
     * @param Event $event
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
42
     * @return void
43
     */
44 18
    public function startup(Event $event)
45
    {
46 18
        $this->setController($event->subject());
47 18
    }
48
49
    /**
50
     * @param controller $controller
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
51
     * @return void
52
     */
53 18
    public function setController($controller)
54
    {
55 18
        $this->controller = $controller;
56 18
    }
57
58
    /**
59
     * @param array $data
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
60
     * @return array|false
61
     */
62
    public function shopDataToDatabase(array $data)
63
    {
64
        $shopEntity = $this->shops->newEntity();
65
66
        unset($data['created_at']);
67
        unset($data['updated_at']);
68
69
        $shopEntity->set($data);
70
71
        $shopEntity->set(['updated_at' => new \DateTime('now')]);
72
73
        if (!$shopEntity->errors() && $this->shops->save($shopEntity)) {
74
            return $shopEntity->toArray();
75
        } else {
76
            return false;
77
        }
78
    }
79
80
    /**
81
     * @param string $accessToken
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
82
     * @param int    $shopId
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
83
     * @param string $apiKey
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
84
     * @return array|false
85
     */
86
    public function accessTokenToDatabase($accessToken, $shopId, $apiKey)
87
    {
88
        $accessTokenEntity = $this->access_tokens->newEntity();
89
90
        $accessTokenArray = [
91
            'shop_id' => $shopId,
92
            'api_key' => $apiKey,
93
            'token' => $accessToken];
94
95
        $accessTokenEntity->set($accessTokenArray);
96
97
        $accessTokenId = $this->access_tokens
98
            ->find()
99
            ->where($accessTokenArray)
100
            ->first();
101
102
        if ($accessTokenId) {
103
            $accessTokenEntity->set(
104
                [
105
                'id' => $accessTokenId->id,
106
                'updated_at' => new \DateTime('now')
107
                ]
108
            );
109
        }
110
111
        if (!$accessTokenEntity->errors() && $this->access_tokens->save($accessTokenEntity)) {
112
            return $accessTokenEntity->toArray();
113
        } else {
114
            return false;
115
        }
116
    }
117
118
    /**
119
     * @param string $domain
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
120
     * @return int|false
121
     */
122 6
    public function getShopIdFromDomain($domain)
123
    {
124 6
        if (empty($domain) || $domain === true) {
125 6
            return false;    
0 ignored issues
show
introduced by
Double space found
Loading history...
126
        }
127
128 6
        $query = $this->shops->find('shopDomain', ['domain' => $domain]);
129
        
130 6
		$shopEntity = $query->first();
131
            
132
        /*if ($shopEntity->isEmpty()) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
133
            return false;    
134
        }*/
135
            
136 6
        if ($shopEntity->id) {
137
            return (int)$shopEntity->id;
138
        } else {
139 6
            return false;
140
        }
141
    }
142
143
    /**
144
     * @param string $accessToken
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
145
     * @param string $apiKey
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
146
     * @return array|false
147
     */
148 6
    public function getShopDataFromAccessToken($accessToken, $apiKey)
149
    {
150 6
        if (empty($accessToken) || empty($apiKey) || $accessToken === true || $apiKey === true) {
151 6
            return false;    
0 ignored issues
show
introduced by
Double space found
Loading history...
152
        }
153
        
154
        $query = $this->access_tokens->find();
155
        $query = $query->contain(['Shops']);
156
        $query = $query->where(['api_key' => $apiKey, 'token' => $accessToken]);
157
        $query = $query->where(
158
            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...
159
                return $exp->isNull('expired_at');
160
            }
161
        );
162
163
        $shopEntity = $query->first();
164
        
165
        if ($shopEntity->isEmpty()) {
166
            return false;    
0 ignored issues
show
introduced by
Double space found
Loading history...
167
        }
168
        
169
        $shopArray = $shopEntity->toArray();
170
171
        if (is_array($shopArray['shop'])) {
172
            return $shopArray['shop'];
173
        } else {
174
            return false;
175
        }
176
    }
177
178
    /**
179
     * @param string $shopDomain
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
180
     * @param string $apiKey
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
181
     * @return string|bool
182
     */
183 6
    public function getAccessTokenFromShopDomain($shopDomain, $apiKey)
184
    {
185 6
        if (empty($shopDomain) || empty($apiKey) || $shopDomain === true || $apiKey === true) {
186 6
            return false;    
0 ignored issues
show
introduced by
Double space found
Loading history...
187
        }
188
        
189
        $query = $this->access_tokens->find();
190
        $query = $query->contain(['Shops']);
191
        $query = $query->where(['api_key' => $apiKey, 'Shops.myshopify_domain' => $shopDomain]);
192
        $query = $query->where(
193
            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...
194
                return $exp->isNull('expired_at');
195
            }
196
        );
197
198
        $accessTokenEntity = $query->first();
199
200
        if ($accessTokenEntity->isEmpty()) {
201
            return false;    
0 ignored issues
show
introduced by
Double space found
Loading history...
202
        }
203
204
        if ($accessTokenEntity->token) {
205
            return $accessTokenEntity->token;
206
        } else {
207
            return false;
208
        }
209
    }
210
}
211