Completed
Push — master ( f0bcb3...4037dd )
by Jason
09:30 queued 05:20
created

FoxyCart   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Test Coverage

Coverage 52.11%

Importance

Changes 0
Metric Value
wmc 23
eloc 66
dl 0
loc 184
ccs 37
cts 71
cp 0.5211
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 getFoxyCartStoreName() 0 14 4
A putCustomer() 0 21 3
A getCustomer() 0 18 3
A FormActionURL() 0 7 2
A getKeyPrefix() 0 3 1
A getAPIRequest() 0 26 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
        $config = FoxyStripeSetting::current_foxystripe_setting();
94
        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...
95
            return sprintf('https://%s/cart', self::getFoxyCartStoreName());
96
        } else {
97
            return sprintf('https://%s.foxycart.com/cart', self::getFoxyCartStoreName());
98
        }
99
    }
100
101
    /**
102
     * FoxyCart API v1.1 functions.
103
     */
104
105
    /**
106
     * @param array $foxyData
107
     * @return string
108
     * @throws \SilverStripe\ORM\ValidationException
109
     */
110 49
    private static function getAPIRequest($foxyData = array())
111
    {
112 49
        if (self::getStoreKey() && self::getFoxyCartStoreName()) {
113
            $foxy_domain = self::getFoxyCartStoreName().'.foxycart.com';
114
            $foxyData['api_token'] = self::getStoreKey();
115
116
            $ch = curl_init();
117
            curl_setopt($ch, CURLOPT_URL, 'https://'.$foxy_domain.'/api');
118
            curl_setopt($ch, CURLOPT_POSTFIELDS, $foxyData);
119
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
120
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
121
            curl_setopt($ch, CURLOPT_TIMEOUT, 15);
122
            // If you get SSL errors, you can uncomment the following, or ask your host to add the appropriate CA bundle
123
            // 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...
124
            $response = trim(curl_exec($ch));
125
126
            // The following if block will print any CURL errors you might have
127
            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...
128
                //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...
129
                Injector::inst()->get(LoggerInterface::class)->error('Could not connect to FoxyCart API');
130
            }
131
            curl_close($ch);
132
133
            return $response;
134
        }
135 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...
136
    }
137
138
    /**
139
     * @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...
140
     * @return string
141
     * @throws \SilverStripe\ORM\ValidationException
142
     */
143
    public static function getCustomer(Member $Member = null)
144
    {
145
146
        // throw error if no $Member Object
147
        if (!isset($Member)) {
148
            trigger_error('No Member set', E_USER_ERROR);
149
        }
150
151
        // grab customer record from API
152
153
        $foxyData = array();
154
        $foxyData['api_action'] = 'customer_get';
155
        if ($Member->Customer_ID) {
156
            $foxyData['customer_id'] = $Member->Customer_ID;
157
        }
158
        $foxyData['customer_email'] = $Member->Email;
159
160
        return self::getAPIRequest($foxyData);
161
    }
162
163
    /**
164
     * @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...
165
     * @return string
166
     * @throws \SilverStripe\ORM\ValidationException
167
     */
168 49
    public static function putCustomer(Member $Member = null)
169
    {
170
        // throw error if no $Member Object
171 49
        if ($Member === null) {
172
//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...
173
            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...
174
        }
175
        // send updated customer record from API
176 49
        $foxyData = array();
177 49
        $foxyData['api_action'] = 'customer_save';
178
        // customer_id will be 0 if created in SilverStripe.
179 49
        if ($Member->Customer_ID) {
180
            $foxyData['customer_id'] = $Member->Customer_ID;
181
        }
182 49
        $foxyData['customer_email'] = $Member->Email;
183 49
        $foxyData['customer_password_hash'] = $Member->Password;
184 49
        $foxyData['customer_password_salt'] = $Member->Salt;
185 49
        $foxyData['customer_first_name'] = $Member->FirstName;
186 49
        $foxyData['customer_last_name'] = $Member->Surname;
187
188 49
        return self::getAPIRequest($foxyData);
189
    }
190
191
    /**
192
     * @return string
193
     */
194 3
    public static function getKeyPrefix()
195
    {
196 3
        return self::$keyPrefix;
197
    }
198
}
199