Completed
Push — master ( a74e45...c4e592 )
by AJ
09:37
created

getAccessTokenFromShopDomain()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 33.2824

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 3
cts 16
cp 0.1875
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 16
nc 4
nop 2
crap 33.2824
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
            return false;    
0 ignored issues
show
introduced by
Double space found
Loading history...
126
        }
127
128 6
        $query = $this->shops->find();
129 6
        $query = $query->findByShopDomain(['domain' => $domain]);
130
        $shopEntity = $query->first();
131
            
132
        if ($shopEntity->isEmpty()) {
133
            return false;    
0 ignored issues
show
introduced by
Double space found
Loading history...
134
        }
135
            
136
        if ($shopEntity->id) {
137
            return (int)$shopEntity->id;
138
        } else {
139
            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