Completed
Push — master ( a26c9f...9c1131 )
by AJ
02:28
created

getShopDataFromAccessToken()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 14
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 17
nc 4
nop 2
crap 30
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
    public function initialize(array $config = [])
35
    {
36
        $this->shops = TableRegistry::get('Multidimensional/Cakephpify.Shops');
37
        $this->access_tokens = TableRegistry::get('Multidimensional/Cakephpify.AccessTokens');
38
    }
39
40
    /**
41
     * @param Event $event
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
42
     * @return void
43
     */
44
    public function startup(Event $event)
45
    {
46
        $this->setController($event->subject());
47
    }
48
49
    /**
50
     * @param controller $controller
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
51
     * @return void
52
     */
53
    public function setController($controller)
54
    {
55
        $this->controller = $controller;
56
        if (!isset($this->controller->paginate)) {
57
            $this->controller->paginate = [];
58
        }
59
    }
60
61
    /**
62
     * @param array $data
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
63
     * @return array|bool
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
64
     */
65
    public function shopDataToDatabase(array $data)
66
    {
67
        $shopEntity = $this->shops->newEntity();
68
69
        unset($data['created_at']);
70
        unset($data['updated_at']);
71
72
        $shopEntity->set($data);
73
74
        $shopEntity->set(['updated_at' => new \DateTime('now')]);
75
76
        if (!$shopEntity->errors() && $this->shops->save($shopEntity)) {
77
            return $shopEntity->toArray();
78
        } else {
79
            return false;
80
        }
81
    }
82
83
    /**
84
     * @param string $accessToken
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
85
     * @param int    $shopId
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
86
     * @param string $apiKey
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
87
     * @return array|bool
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
88
     */
89
    public function accessTokenToDatabase($accessToken, $shopId, $apiKey)
90
    {
91
        $accessTokenEntity = $this->access_tokens->newEntity();
92
93
        $accessTokenArray = [
94
            'shop_id' => $shopId,
95
            'api_key' => $apiKey,
96
            'token' => $accessToken];
97
98
        $accessTokenEntity->set($accessTokenArray);
99
100
        $accessTokenId = $this->access_tokens
101
            ->find()
102
            ->where($accessTokenArray)
103
            ->first();
104
105
        if ($accessTokenId) {
106
            $accessTokenEntity->set(
107
                [
108
                'id' => $accessTokenId->id,
109
                'updated_at' => new \DateTime('now')
110
                ]
111
            );
112
        }
113
114
        if (!$accessTokenEntity->errors() && $this->access_tokens->save($accessTokenEntity)) {
115
            return $accessTokenEntity->toArray();
116
        } else {
117
            return false;
118
        }
119
    }
120
121
    /**
122
     * @param string $domain
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
123
     * @return int|false
124
     */
125
    public function getShopIdFromDomain($domain)
126
    {
127
		if (empty($domain)) {
128
			return false;	
0 ignored issues
show
introduced by
Double space found
Loading history...
129
		}
130
131
        $shopEntity = $this
132
			->shops
133
			->findByMyshopifyDomain($domain)
134
			->first();
135
			
136
		if ($shopEntity->isEmpty()) {
137
			return false;	
0 ignored issues
show
introduced by
Double space found
Loading history...
138
		}
139
			
140
        if ($shopEntity->id) {
141
            return (int)$shopEntity->id;
142
        } else {
143
            return false;
144
        }
145
    }
146
147
    /**
148
     * @param string $accessToken
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
149
     * @param string $apiKey
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
150
     * @return array|false
151
     */
152
    public function getShopDataFromAccessToken($accessToken, $apiKey)
153
    {
154
		if (empty($accessToken) || empty($apiKey)) {
155
			return false;	
0 ignored issues
show
introduced by
Double space found
Loading history...
156
		}
157
		
158
        $query = $this->access_tokens->find();
159
        $query = $query->contain(['Shops']);
160
        $query = $query->where(['api_key' => $apiKey, 'token' => $accessToken]);
161
        $query = $query->where(
162
            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...
163
                return $exp->isNull('expired_at');
164
            }
165
        );
166
167
        $shopEntity = $query->first();
168
		
169
		if ($shopEntity->isEmpty()) {
170
			return false;	
0 ignored issues
show
introduced by
Double space found
Loading history...
171
		}
172
		
173
		$shopArray = $shopEntity->toArray();
174
175
        if (is_array($shopArray['shop'])) {
176
            return $shopArray['shop'];
177
        } else {
178
            return false;
179
        }
180
    }
181
182
    /**
183
     * @param string $shopDomain
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
184
     * @param string $apiKey
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
185
     * @return string|bool
186
     */
187
    public function getAccessTokenFromShopDomain($shopDomain, $apiKey)
188
    {
189
		if (empty($shopDomain) || empty($apiKey)) {
190
			return false;	
0 ignored issues
show
introduced by
Double space found
Loading history...
191
		}
192
		
193
        $query = $this->access_tokens->find();
194
        $query = $query->contain(['Shops']);
195
        $query = $query->where(['api_key' => $apiKey, 'Shops.myshopify_domain' => $shopDomain]);
196
        $query = $query->where(
197
            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...
198
                return $exp->isNull('expired_at');
199
            }
200
        );
201
202
        $accessTokenEntity = $query->first();
203
204
		if ($accessTokenEntity->isEmpty()) {
205
			return false;	
0 ignored issues
show
introduced by
Double space found
Loading history...
206
		}
207
208
        if ($accessTokenEntity->token) {
209
            return $accessTokenEntity->token;
210
        } else {
211
            return false;
212
        }
213
    }
214
}
215