Completed
Pull Request — master (#316)
by Matthew
12:03 queued 08:57
created

FoxyCart::FormActionURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Dynamic\FoxyStripe\Model;
4
5
use Psr\Log\LoggerInterface;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Security\Member;
8
9
/**
10
 *
11
 */
12
class FoxyCart
13
{
14
    /**
15
     * @var string
16
     */
17
    private static $keyPrefix = 'dYnm1c';
18
19
    /**
20
     * @param int $length
21
     * @param int $count
22
     *
23
     * @return string
24
     */
25 3
    public static function setStoreKey($length = 54, $count = 0)
26
    {
27 3
        $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.strtotime('now');
28 3
        $strLength = strlen($charset);
29 3
        $str = '';
30 3
        while ($count < $length) {
31 3
            $str .= $charset[mt_rand(0, $strLength - 1)];
32 3
            ++$count;
33
        }
34
35 3
        return self::getKeyPrefix().substr(base64_encode($str), 0, $length);
36
    }
37
38
    /**
39
     * @return bool
40
     */
41 49
    public static function getStoreKey()
42
    {
43 49
        $config = FoxyStripeSetting::current_foxystripe_setting();
44 49
        if ($config->StoreKey) {
45
            return $config->StoreKey;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $config->StoreKey returns the type SilverStripe\ORM\FieldType\DBVarchar which is incompatible with the documented return type boolean.
Loading history...
46
        }
47
48 49
        return false;
49
    }
50
51
    /**
52
     * @return null|string
53
     */
54 1
    public static function store_name_warning()
55
    {
56 1
        $warning = null;
57 1
        if (self::getFoxyCartStoreName() === null) {
58
            $warning = 'Must define FoxyCart Store Name in your site settings in the cms';
59
        }
60
61 1
        return $warning;
62
    }
63
64
    /**
65
     * @return bool|\SilverStripe\ORM\FieldType\DBVarchar
66
     */
67 1
    public static function getFoxyCartStoreName()
68
    {
69 1
        $config = FoxyStripeSetting::current_foxystripe_setting();
70 1
        if ($config->StoreName) {
71
            return $config->StoreName;
72
        }
73
74 1
        return false;
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public static function FormActionURL()
81
    {
82
        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

82
        return sprintf('https://%s.foxycart.com/cart', /** @scrutinizer ignore-type */ self::getFoxyCartStoreName());
Loading history...
83
    }
84
85
    /**
86
     * FoxyCart API v1.1 functions.
87
     */
88
89
    /**
90
     * @param array $foxyData
91
     *
92
     * @return bool|string
93
     * @throws \Psr\Container\NotFoundExceptionInterface
94
     */
95 49
    private static function getAPIRequest($foxyData = array())
96
    {
97 49
        if (self::getStoreKey() && self::getFoxyCartStoreName()) {
98
            $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

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