Completed
Push — 2 ( 3c1312...4bdd9a )
by Jason
09:11
created

FoxyCart::getFoxyCartStoreName()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.4042

Importance

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