Completed
Pull Request — master (#6)
by AJ
12:51
created

ShopifyDatabaseComponent::setController()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
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
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
    public function initialize(array $config = [])
31
    {
32
        $this->shops = TableRegistry::get('Multidimensional/Shopify.Shops');
33
        $this->access_tokens = TableRegistry::get('Multidimensional/Shopify.AccessTokens');
34
    }
35
36
    public function startup(Event $event)
37
    {
38
        $this->setController($event->subject());
39
    }
40
41
    public function setController($controller)
42
    {
43
        $this->controller = $controller;
44
        if (!isset($this->controller->paginate)) {
45
            $this->controller->paginate = [];
46
        }
47
    }
48
49
    public function shopDataToDatabase(array $data)
0 ignored issues
show
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...
50
    {
51
52
        $shopEntity = $this->shops->newEntity();
0 ignored issues
show
Unused Code introduced by
$shopEntity is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
53
54
        unset($data['created_at']);
55
        unset($data['updated_at']);
56
         
57
        $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...
Bug introduced by
The variable $shop_entity does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
58
        
59
        $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...
60
        
61
        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...
62
            return $shop_entity->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...
63
64
        } else {
65
            return false;
66
        }
67
    }
68
69
70
    public function accessTokenToDatabase($accessToken, $shopId, $apiKey)
0 ignored issues
show
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...
71
    {
72
73
        $accessTokenEntity = $this->access_tokens->newEntity();
74
75
        $accessTokenArray = [
76
            'shop_id' => $shopId,
77
            'api_key' => $apiKey,
78
            'token' => $accessToken];
79
80
        $accessTokenEntity->set($accessTokenArray);
81
82
        $accessTokenId = $this->access_tokens
83
            ->find()
84
            ->where($accessTokenArray)
85
            ->first();
86
87
        if ($accessTokenId) {
88
            $accessTokenEntity->set([
89
                'id' => $accessTokenId->id,
90
                'updated_at' => new \DateTime('now')
91
            ]);
92
        }
93
                                    
94
        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...
95
            return $access_token_entity->toArray();
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...
Bug introduced by
The variable $access_token_entity does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
96
97
        } else {
98
            return false;
99
        }
100
    }
101
102
    public function getShopIdFromDomain($domain)
103
    {
104
105
        $shopEntity = $this->shops->findByMyshopifyDomain($domain)->first();
106
        if ($shopEntity->id) {
107
            return (int)$shopEntity->id;
108
        } else {
109
            return false;
110
        }
111
    }
112
113
    public function getShopDataFromAccessToken($accessToken, $apiKey)
0 ignored issues
show
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...
114
    {
115
116
        $query = $this->access_tokens->find();
117
        $query = $query->contain(['Shops']);
118
        $query = $query->where(['api_key' => $apiKey, 'token' => $accessToken]);
119
        $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...
120
            return $exp->isNull('expired_at');
121
        });
122
123
        $shopEntity = $query->first()->toArray();
124
125
        if (is_array($shopEntity['shop'])) {
126
            return $shopEntity['shop'];
127
        } else {
128
            return false;
129
        }
130
    }
131
132
    public function getAccessTokenFromShopDomain($shopDomain, $apiKey)
0 ignored issues
show
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...
133
    {
134
135
        $query = $this->access_tokens->find();
136
        $query = $query->contain(['Shops']);
137
        $query = $query->where(['api_key' => $apiKey, 'Shops.myshopify_domain' => $shopDomain]);
138
        $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...
139
            return $exp->isNull('expired_at');
140
        });
141
142
        $accessTokenEntity = $query->first();
143
144
        if ($accessTokenEntity->token) {
145
            return $accessTokenEntity->token;
146
        } else {
147
            return false;
148
        }
149
    }
150
}
151