Completed
Pull Request — master (#287)
by Nic
07:49
created

FoxyCart::get_key_prefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * Class FoxyCart
5
 * @package FoxyStripe
6
 */
7
class FoxyCart extends Object
8
{
9
10
    /**
11
     * @var string
12
     */
13
    private static $keyPrefix = 'dYnm1c';
14
15
    /**
16
     * @param int $length
17
     * @param int $count
18
     * @return string
19
     */
20 3
    public static function setStoreKey($length = 54, $count = 0)
21
    {
22 3
        $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' . strtotime('now');
23 3
        $strLength = strlen($charset);
24 3
        $str = '';
25 3
        while ($count < $length) {
26 3
            $str .= $charset[mt_rand(0, $strLength - 1)];
27 3
            $count++;
28 3
        }
29 3
        return static::get_key_prefix() . substr(base64_encode($str), 0, $length);
30
    }
31
32
    /**
33
     * @return string|null
34
     */
35 33
    public static function get_store_key()
36
    {
37 33
        $config = SiteConfig::current_site_config();
38 33
        if ($config->StoreKey) {
39
            return $config->StoreKey;
40
        }
41 33
        return null;
42
    }
43
44
    /**
45
     * @return null|string
46
     */
47
    public static function store_name_warning()
48
    {
49
        $warning = null;
50
        if (static::get_foxy_cart_store_name() === null) {
51
            $warning = 'Must define FoxyCart Store Name in your site settings in the cms';
52
        }
53
        return $warning;
54
    }
55
56
    /**
57
     * @return string|null
58
     */
59 33
    public static function get_foxy_cart_store_name()
60
    {
61 33
        $config = SiteConfig::current_site_config();
62 33
        if ($config->StoreName) {
63
            return $config->StoreName;
64
        }
65 33
        return null;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public static function FormActionURL()
72
    {
73
        return sprintf('https://%s.foxycart.com/cart', static::get_foxy_cart_store_name());
74
    }
75
76
    /**
77
     * FoxyCart API v1.1 functions
78
     *
79
     * @param array $foxyData
80
     * @return string
81
     */
82 33
    private static function getAPIRequest($foxyData = array())
83
    {
84
85 33
        $foxy_domain = static::get_foxy_cart_store_name() . '.foxycart.com';
86 33
        $foxyData["api_token"] = FoxyCart::get_store_key();
87
88 33
        $ch = curl_init();
89 33
        curl_setopt($ch, CURLOPT_URL, "https://" . $foxy_domain . "/api");
90 33
        curl_setopt($ch, CURLOPT_POSTFIELDS, $foxyData);
91 33
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
92 33
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
93 33
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
94 33
        $response = trim(curl_exec($ch));
95
96
        // The following if block will print any CURL errors you might have
97 33
        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...
98 33
            SS_Log::log("Could not connect to FoxyCart API: " . $response, SS_Log::ERR);
99 33
        }
100 33
        curl_close($ch);
101
102 33
        return $response;
103
    }
104
105
    /**
106
     * @param Member|null $Member
107
     * @return string
108
     */
109
    public static function getCustomer(Member $Member = null)
110
    {
111
112
        // throw error if no $Member Object
113
        if (!isset($Member)) trigger_error('No Member set', E_USER_ERROR);
114
115
        // grab customer record from API
116
117
        $foxyData = array();
118
        $foxyData["api_action"] = "customer_get";
119
        if ($Member->Customer_ID) $foxyData["customer_id"] = $Member->Customer_ID;
120
        $foxyData["customer_email"] = $Member->Email;
121
122
        return self::getAPIRequest($foxyData);
123
124
    }
125
126
    /**
127
     * @param Member|null $Member
128
     * @return string
129
     */
130 33
    public static function putCustomer(Member $Member = null)
131
    {
132
        // throw error if no $Member Object
133 33
        if (!isset($Member)) ;//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...
134
135
        // send updated customer record from API
136 33
        $foxyData = array();
137 33
        $foxyData["api_action"] = "customer_save";
138
        // customer_id will be 0 if created in SilverStripe.
139 33
        if ($Member->Customer_ID) $foxyData["customer_id"] = $Member->Customer_ID;
140 33
        $foxyData["customer_email"] = $Member->Email;
141 33
        $foxyData["customer_password_hash"] = $Member->Password;
142 33
        $foxyData["customer_password_salt"] = $Member->Salt;
143 33
        $foxyData["customer_first_name"] = $Member->FirstName;
144 33
        $foxyData["customer_last_name"] = $Member->Surname;
145
146 33
        return self::getAPIRequest($foxyData);
147
    }
148
149
    /**
150
     * @return string
151
     */
152 3
    protected static function get_key_prefix()
153
    {
154 3
        return self::$keyPrefix;
155
    }
156
157
}
158