Passed
Pull Request — master (#351)
by Jason
09:43
created

FoxyCart   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Test Coverage

Coverage 52.86%

Importance

Changes 0
Metric Value
wmc 23
eloc 65
dl 0
loc 183
ccs 37
cts 70
cp 0.5286
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A store_name_warning() 0 8 2
A setStoreKey() 0 11 2
A getStoreKey() 0 8 2
A putCustomer() 0 21 3
A getCustomer() 0 18 3
A FormActionURL() 0 6 2
A getKeyPrefix() 0 3 1
A getAPIRequest() 0 26 4
A getFoxyCartStoreName() 0 14 4
1
<?php
2
3
namespace Dynamic\FoxyStripe\Model;
4
5
use Psr\Log\LoggerInterface;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\SiteConfig\SiteConfig;
8
use SilverStripe\Security\Member;
9
10
/**
11
 *
12
 */
13
class FoxyCart
14
{
15
    /**
16
     * @var string
17
     */
18
    private static $keyPrefix = 'dYnm1c';
19
20
    /**
21
     * @param int $length
22
     * @param int $count
23
     *
24
     * @return string
25
     */
26 3
    public static function setStoreKey($length = 54, $count = 0)
27
    {
28 3
        $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.strtotime('now');
29 3
        $strLength = strlen($charset);
30 3
        $str = '';
31 3
        while ($count < $length) {
32 3
            $str .= $charset[mt_rand(0, $strLength - 1)];
33 3
            ++$count;
34
        }
35
36 3
        return self::getKeyPrefix().substr(base64_encode($str), 0, $length);
37
    }
38
39
    /**
40
     * @return mixed|null
41
     * @throws \SilverStripe\ORM\ValidationException
42
     */
43 49
    public static function getStoreKey()
44
    {
45 49
        $config = FoxyStripeSetting::current_foxystripe_setting();
46 49
        if ($config->StoreKey) {
47
            return $config->StoreKey;
48
        }
49
50 49
        return false;
51
    }
52
53
    /**
54
     * @return null|string
55
     * @throws \SilverStripe\ORM\ValidationException
56
     */
57 1
    public static function store_name_warning()
58
    {
59 1
        $warning = null;
60 1
        if (self::getFoxyCartStoreName() === null) {
61
            $warning = 'Must define FoxyCart Store Name or Store Remote Domain in your site settings in the cms';
62
        }
63
64 1
        return $warning;
65
    }
66
67
    /**
68
     * @return mixed|null
69
     * @throws \SilverStripe\ORM\ValidationException
70
     */
71 1
    public static function getFoxyCartStoreName()
72
    {
73 1
        $config = FoxyStripeSetting::current_foxystripe_setting();
74 1
        if ($config->CustomSSL) {
0 ignored issues
show
Bug Best Practice introduced by
The property CustomSSL does not exist on Dynamic\FoxyStripe\Model\FoxyStripeSetting. Since you implemented __get, consider adding a @property annotation.
Loading history...
75
            if ($config->RemoteDomain) {
0 ignored issues
show
Bug Best Practice introduced by
The property RemoteDomain does not exist on Dynamic\FoxyStripe\Model\FoxyStripeSetting. Since you implemented __get, consider adding a @property annotation.
Loading history...
76
                return $config->RemoteDomain;
77
            }
78
        } else {
79 1
            if ($config->StoreName){
80
                return $config->StoreName;
81
            }
82
        }
83
84 1
        return false;
85
    }
86
87
    /**
88
     * @return string
89
     * @throws \SilverStripe\ORM\ValidationException
90
     */
91
    public static function FormActionURL()
92
    {
93
        if ($config->CustomSSL) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $config seems to be never defined.
Loading history...
94
            return sprintf('https://%s/cart', self::getFoxyCartStoreName() );
95
        } else {
96
            return sprintf('https://%s.foxycart.com/cart', self::getFoxyCartStoreName());
97
        }
98
    }
99
100
    /**
101
     * FoxyCart API v1.1 functions.
102
     */
103
104
    /**
105
     * @param array $foxyData
106
     * @return string
107
     * @throws \SilverStripe\ORM\ValidationException
108
     */
109 49
    private static function getAPIRequest($foxyData = array())
110
    {
111 49
        if (self::getStoreKey() && self::getFoxyCartStoreName()) {
112
            $foxy_domain = self::getFoxyCartStoreName().'.foxycart.com';
113
            $foxyData['api_token'] = self::getStoreKey();
114
115
            $ch = curl_init();
116
            curl_setopt($ch, CURLOPT_URL, 'https://'.$foxy_domain.'/api');
117
            curl_setopt($ch, CURLOPT_POSTFIELDS, $foxyData);
118
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
119
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
120
            curl_setopt($ch, CURLOPT_TIMEOUT, 15);
121
            // If you get SSL errors, you can uncomment the following, or ask your host to add the appropriate CA bundle
122
            // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
123
            $response = trim(curl_exec($ch));
124
125
            // The following if block will print any CURL errors you might have
126
            if ($response == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $response of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
127
                //trigger_error("Could not connect to FoxyCart API", E_USER_ERROR);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
128
                Injector::inst()->get(LoggerInterface::class)->error('Could not connect to FoxyCart API');
129
            }
130
            curl_close($ch);
131
132
            return $response;
133
        }
134 49
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
135
    }
136
137
    /**
138
     * @param null $Member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $Member is correct as it would always require null to be passed?
Loading history...
139
     * @return string
140
     * @throws \SilverStripe\ORM\ValidationException
141
     */
142
    public static function getCustomer(Member $Member = null)
143
    {
144
145
        // throw error if no $Member Object
146
        if (!isset($Member)) {
147
            trigger_error('No Member set', E_USER_ERROR);
148
        }
149
150
        // grab customer record from API
151
152
        $foxyData = array();
153
        $foxyData['api_action'] = 'customer_get';
154
        if ($Member->Customer_ID) {
155
            $foxyData['customer_id'] = $Member->Customer_ID;
156
        }
157
        $foxyData['customer_email'] = $Member->Email;
158
159
        return self::getAPIRequest($foxyData);
160
    }
161
162
    /**
163
     * @param null $Member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $Member is correct as it would always require null to be passed?
Loading history...
164
     * @return string
165
     * @throws \SilverStripe\ORM\ValidationException
166
     */
167 49
    public static function putCustomer(Member $Member = null)
168
    {
169
        // throw error if no $Member Object
170 49
        if ($Member === null) {
171
//trigger_error('No Member set', E_USER_ERROR);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
172
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
173
        }
174
        // send updated customer record from API
175 49
        $foxyData = array();
176 49
        $foxyData['api_action'] = 'customer_save';
177
        // customer_id will be 0 if created in SilverStripe.
178 49
        if ($Member->Customer_ID) {
179
            $foxyData['customer_id'] = $Member->Customer_ID;
180
        }
181 49
        $foxyData['customer_email'] = $Member->Email;
182 49
        $foxyData['customer_password_hash'] = $Member->Password;
183 49
        $foxyData['customer_password_salt'] = $Member->Salt;
184 49
        $foxyData['customer_first_name'] = $Member->FirstName;
185 49
        $foxyData['customer_last_name'] = $Member->Surname;
186
187 49
        return self::getAPIRequest($foxyData);
188
    }
189
190
    /**
191
     * @return string
192
     */
193 3
    public static function getKeyPrefix()
194
    {
195 3
        return self::$keyPrefix;
196
    }
197
}
198