Completed
Pull Request — master (#304)
by Jason
12:34
created

FoxyCart   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 82.26%

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 3
dl 0
loc 112
ccs 51
cts 62
cp 0.8226
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setStoreKey() 0 10 2
A getStoreKey() 0 7 2
A getFoxyCartStoreName() 0 7 2
A FormActionURL() 0 3 1
B getAPIRequest() 0 24 2
A getCustomer() 0 15 3
A putCustomer() 0 17 3
A getKeyPrefix() 0 3 1
A store_name_warning() 0 7 2
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 48
	public static function getStoreKey(){
24 48
		$config = SiteConfig::current_site_config();
25 48
		if($config->StoreKey){
26
			return $config->StoreKey;
27
		}
28 48
		return null;
29
	}
30
31 1
	public static function store_name_warning(){
32 1
		$warning = null;
33 1
		if(self::getFoxyCartStoreName()===null){
34 1
			$warning = 'Must define FoxyCart Store Name in your site settings in the cms';
35 1
		}
36 1
		return $warning;
37
	}
38
39 48
	public static function getFoxyCartStoreName(){
40 48
		$config = SiteConfig::current_site_config();
41 48
		if($config->StoreName){
42
			return $config->StoreName;
43
		}
44 48
		return null;
45
	}
46
47
	public static function FormActionURL() {
48
		return sprintf('https://%s.foxycart.com/cart', self::getFoxyCartStoreName() );
49
	}
50
51
    /**
52
     * FoxyCart API v1.1 functions
53
     */
54
55
    // FoxyCart API Request
56 48
    private static function getAPIRequest($foxyData = array()) {
57
58 48
        $foxy_domain = FoxyCart::getFoxyCartStoreName().'.foxycart.com';
59 48
        $foxyData["api_token"] = FoxyCart::getStoreKey();
60
61 48
        $ch = curl_init();
62 48
        curl_setopt($ch, CURLOPT_URL, "https://" . $foxy_domain . "/api");
63 48
        curl_setopt($ch, CURLOPT_POSTFIELDS, $foxyData);
64 48
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
65 48
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
66 48
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
67
        // If you get SSL errors, you can uncomment the following, or ask your host to add the appropriate CA bundle
68
        // 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...
69 48
        $response = trim(curl_exec($ch));
70
71
        // The following if block will print any CURL errors you might have
72 48
        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...
73
            //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...
74 48
            SS_Log::log("Could not connect to FoxyCart API: " . $response, SS_Log::ERR);
75 48
        }
76 48
        curl_close($ch);
77
78 48
        return $response;
79
    }
80
81
    public static function getCustomer($Member = null) {
82
83
        // throw error if no $Member Object
84
        if (!isset($Member)) trigger_error('No Member set', E_USER_ERROR);
85
86
        // grab customer record from API
87
88
        $foxyData = array();
89
        $foxyData["api_action"] = "customer_get";
90
        if ($Member->Customer_ID) $foxyData["customer_id"] = $Member->Customer_ID;
91
        $foxyData["customer_email"] = $Member->Email;
92
93
        return self::getAPIRequest($foxyData);
94
95
    }
96
97 48
    public static function putCustomer($Member = null) {
98
        // throw error if no $Member Object
99 48
        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...
100
101
        // send updated customer record from API
102 48
        $foxyData = array();
103 48
        $foxyData["api_action"] = "customer_save";
104
        // customer_id will be 0 if created in SilverStripe.
105 48
        if ($Member->Customer_ID) $foxyData["customer_id"] = $Member->Customer_ID;
106 48
        $foxyData["customer_email"] = $Member->Email;
107 48
        $foxyData["customer_password_hash"] = $Member->Password;
108 48
        $foxyData["customer_password_salt"] = $Member->Salt;
109 48
        $foxyData["customer_first_name"] = $Member->FirstName;
110 48
        $foxyData["customer_last_name"] = $Member->Surname;
111
112 48
        return self::getAPIRequest($foxyData);
113
    }
114
115 3
	public static function getKeyPrefix(){
116 3
		return self::$keyPrefix;
117
	}
118
119
}
120