Passed
Pull Request — master (#309)
by Jason
13:47
created

FoxyCart::getCustomer()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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