Completed
Push — master ( 626e87...f67c15 )
by
unknown
01:26
created

EntersaleremotelyAPIConnector   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 112
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sendOrderDataToFeefo() 0 57 3
A sendCurlRequest() 0 19 1
1
<?php
2
3
/**
4
 * Class used to connecto the the Entersaleremotely API
5
 *@author nicolaas [at] sunnysideup.co.nz
6
 */
7
8
9
class EntersaleremotelyAPIConnector extends Object
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
12
    /**
13
     * REQUIRED!
14
     * @var String
15
     */
16
    private static $api_key = "";
0 ignored issues
show
Unused Code introduced by
The property $api_key is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
18
19
    /**
20
     * REQUIRED!
21
     * @var String
22
     */
23
    private static $merchant_identifier = "";
0 ignored issues
show
Unused Code introduced by
The property $merchant_identifier is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
25
26
    /**
27
     * sends the order data from the website to feefo using the Entersaleremotely API
28
     *
29
     * @param  Order $order - the order to be assessed
30
     * @param  int $delay - number of days to wait before feefo will send the feedback email
31
     *
32
     * @return array $messages - an array of message regarding the success of each curl request - one for each Order Item
33
     */
34
    public function sendOrderDataToFeefo($order, $delay = 0)
35
    {
36
        $messages = [];
37
        //api details
38
        $apiKey = Config::inst()->get('EntersaleremotelyAPIConnector', 'api_key');
39
        $merchant = Config::inst()->get('EntersaleremotelyAPIConnector', 'merchant_identifier');
40
41
        //member specific details
42
        $member = $order->Member();
0 ignored issues
show
Bug introduced by
The method Member() does not exist on Order. Did you maybe mean CreateOrReturnExistingMember()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
43
        $email = $member->Email;
44
        $name = $member->FirstName;
45
        $locale = $member->Locale;
46
        $customerRef = $member->ID;
47
48
        //order specific details
49
        $orderRef = $order->ID;
50
        $currency = $order->CurrencyUsed()->Code;
51
        $dateAsString = strtotime($order->Created);
52
        $date = date('Y-m-d', $dateAsString);
53
54
        $feedbackDate = '';
55
        if($delay){
56
            $feedbackDate = date( 'Y-m-d', strtotime('+' . $delay . ' days', $dateAsString) );
57
        }
58
59
        foreach ($order->Items() as $orderItem) {
60
            $amount = $orderItem->CalculatedTotal;
0 ignored issues
show
Unused Code introduced by
$amount is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
61
            $product = $orderItem->Product();
62
            $productTitle = $product->Title;
63
            $link = Director::absoluteURL($product->Link());
0 ignored issues
show
Unused Code introduced by
$link is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
65
            $params = [
66
                'apikey' => $apiKey,
67
                'merchantidentifier' => $merchant,
68
                'feedbackdate' => $feedbackDate,
69
                'email' =>$email,
70
                'name' => $name,
71
                'locale' => $locale,
72
                'customerref' => $customerRef,
73
                'orderref' => $orderRef,
74
                'date' => $date,
75
                'currency' => $currency,
76
                'amount ' => $orderItem->CalculatedTotal,
77
                'description' => $productTitle,
78
                'productsearchcode'=> $productTitle,
79
                'productlink' => 'https://www.picspeanutbutter.com/buy/buy-online/380g-smooth-no-salt/'
80
            ];
81
82
            $result = $this->sendCurlRequest($params);
83
84
            $result .= ' Order ID: ' . $orderRef . '; Product: ' . $productTitle . ';';
85
86
            array_push($messages, $result);
87
        }
88
89
        return $messages;
90
    }
91
92
    /**
93
     * performs the curl request
94
     *
95
     * @param  array $params - the data to send to FEEFO
96
     *
97
     * @return string $reply
98
     */
99
    public function sendCurlRequest($params)
100
    {
101
        $reply = '';
0 ignored issues
show
Unused Code introduced by
$reply is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
102
        $url = 'https://api.feefo.com/api/entersaleremotely';
103
104
        $data = http_build_query($params, '', '&');
105
106
        $ch=curl_init();
107
108
        curl_setopt($ch, CURLOPT_URL, $url);
109
        curl_setopt($ch, CURLOPT_POST, 1);
110
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
111
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
112
113
        $reply = curl_exec($ch);
114
115
        curl_close($ch);
116
        return $reply;
117
    }
118
119
120
}
121