Passed
Pull Request — master (#316)
by Jason
03:31
created

FoxyCart::putCustomer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0327

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 21
ccs 11
cts 13
cp 0.8462
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0327
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 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->StoreName) {
75
            return $config->StoreName;
76
        }
77
78 1
        return false;
79
    }
80
81
    /**
82
     * @return string
83
     * @throws \SilverStripe\ORM\ValidationException
84
     */
85
    public static function FormActionURL()
86
    {
87
        return sprintf('https://%s.foxycart.com/cart', self::getFoxyCartStoreName());
0 ignored issues
show
Bug introduced by
It seems like self::getFoxyCartStoreName() can also be of type false; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
        return sprintf('https://%s.foxycart.com/cart', /** @scrutinizer ignore-type */ self::getFoxyCartStoreName());
Loading history...
88
    }
89
90
    /**
91
     * FoxyCart API v1.1 functions.
92
     */
93
94
    /**
95
     * @param array $foxyData
96
     * @return string
97
     * @throws \SilverStripe\ORM\ValidationException
98
     */
99 49
    private static function getAPIRequest($foxyData = array())
100
    {
101 49
        if (self::getStoreKey() && self::getFoxyCartStoreName()) {
102
            $foxy_domain = self::getFoxyCartStoreName().'.foxycart.com';
0 ignored issues
show
Bug introduced by
Are you sure self::getFoxyCartStoreName() of type SilverStripe\ORM\FieldType\DBVarchar|false can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
            $foxy_domain = /** @scrutinizer ignore-type */ self::getFoxyCartStoreName().'.foxycart.com';
Loading history...
103
            $foxyData['api_token'] = self::getStoreKey();
104
105
            $ch = curl_init();
106
            curl_setopt($ch, CURLOPT_URL, 'https://'.$foxy_domain.'/api');
107
            curl_setopt($ch, CURLOPT_POSTFIELDS, $foxyData);
108
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
109
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
110
            curl_setopt($ch, CURLOPT_TIMEOUT, 15);
111
            // If you get SSL errors, you can uncomment the following, or ask your host to add the appropriate CA bundle
112
            // 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...
113
            $response = trim(curl_exec($ch));
114
115
            // The following if block will print any CURL errors you might have
116
            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...
117
                //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...
118
                Injector::inst()->get(LoggerInterface::class)->error('Could not connect to FoxyCart API');
119
            }
120
            curl_close($ch);
121
122
            return $response;
123
        }
124 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...
125
    }
126
127
    /**
128
     * @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...
129
     * @return string
130
     * @throws \SilverStripe\ORM\ValidationException
131
     */
132
    public static function getCustomer(Member $Member = null)
133
    {
134
135
        // throw error if no $Member Object
136
        if (!isset($Member)) {
137
            trigger_error('No Member set', E_USER_ERROR);
138
        }
139
140
        // grab customer record from API
141
142
        $foxyData = array();
143
        $foxyData['api_action'] = 'customer_get';
144
        if ($Member->Customer_ID) {
145
            $foxyData['customer_id'] = $Member->Customer_ID;
146
        }
147
        $foxyData['customer_email'] = $Member->Email;
148
149
        return self::getAPIRequest($foxyData);
150
    }
151
152
    /**
153
     * @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...
154
     * @return string
155
     * @throws \SilverStripe\ORM\ValidationException
156
     */
157 49
    public static function putCustomer(Member $Member = null)
158
    {
159
        // throw error if no $Member Object
160 49
        if ($Member === null) {
161
//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...
162
            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...
163
        }
164
        // send updated customer record from API
165 49
        $foxyData = array();
166 49
        $foxyData['api_action'] = 'customer_save';
167
        // customer_id will be 0 if created in SilverStripe.
168 49
        if ($Member->Customer_ID) {
169
            $foxyData['customer_id'] = $Member->Customer_ID;
170
        }
171 49
        $foxyData['customer_email'] = $Member->Email;
172 49
        $foxyData['customer_password_hash'] = $Member->Password;
173 49
        $foxyData['customer_password_salt'] = $Member->Salt;
174 49
        $foxyData['customer_first_name'] = $Member->FirstName;
175 49
        $foxyData['customer_last_name'] = $Member->Surname;
176
177 49
        return self::getAPIRequest($foxyData);
178
    }
179
180
    /**
181
     * @return string
182
     */
183 3
    public static function getKeyPrefix()
184
    {
185 3
        return self::$keyPrefix;
186
    }
187
}
188