Completed
Push — master ( 292d01...c6d68c )
by AJ
11:06
created

ShopifyDatabaseComponent::getShopIdFromDomain()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 14.6179

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 3
cts 11
cp 0.2727
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 4
nop 1
crap 14.6179
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
        if (!isset($this->controller->paginate)) {
57
            $this->controller->paginate = [];
58
        }
59 18
    }
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 6
    public function getShopIdFromDomain($domain)
126
    {
127 6
        if (empty($domain) || $domain === true) {
128
            return false;    
0 ignored issues
show
introduced by
Double space found
Loading history...
129
        }
130
131 6
        $query = $this->shops->find;
0 ignored issues
show
Bug introduced by
The property find does not seem to exist in Cake\ORM\Table.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

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