Passed
Pull Request — master (#347)
by Matthew
13:51
created

DataTestController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 31
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Dynamic\FoxyStripe\Controller;
4
5
6
use Dynamic\FoxyStripe\Model\FoxyCart;
7
use Dynamic\FoxyStripe\Model\Order;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Control\Director;
10
use SilverStripe\Core\Config\Config;
11
12
/**
13
 * Class DataTestController
14
 * @package Dynamic\FoxyStripe\Controller
15
 */
16
class DataTestController extends Controller
17
{
18
19
    /**
20
     * @var string
21
     */
22
    private static $transaction_date = "now";
0 ignored issues
show
introduced by
The private property $transaction_date is not used, and could be removed.
Loading history...
23
24
    /**
25
     * @var string|int
26
     */
27
    private static $order_id = "auto";
0 ignored issues
show
introduced by
The private property $order_id is not used, and could be removed.
Loading history...
28
29
30
31
    /**
32
     * @throws \SilverStripe\ORM\ValidationException
33
     */
34
    public function index() {
35
36
        $rules = Director::config()->get('rules');
37
        $rule = array_search(FoxyStripeController::class, $rules);
38
        $myURL = Director::absoluteBaseURL() . explode('//', $rule)[0];
0 ignored issues
show
Bug introduced by
It seems like $rule can also be of type false; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        $myURL = Director::absoluteBaseURL() . explode('//', /** @scrutinizer ignore-type */ $rule)[0];
Loading history...
39
        $myKey = FoxyCart::getStoreKey();
40
41
        $this->updateConfig();
42
43
        $XMLOutput = $this->renderWith(
44
            'TestData',
45
            Config::inst()->get(static::class)
46
        )->RAW();
47
48
        $XMLOutput_encrypted = \rc4crypt::encrypt($myKey, $XMLOutput);
0 ignored issues
show
Bug introduced by
It seems like $myKey can also be of type false; however, parameter $pwd of rc4crypt::encrypt() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        $XMLOutput_encrypted = \rc4crypt::encrypt(/** @scrutinizer ignore-type */ $myKey, $XMLOutput);
Loading history...
49
        $XMLOutput_encrypted = urlencode($XMLOutput_encrypted);
50
51
        $ch = curl_init();
52
        curl_setopt($ch, CURLOPT_URL, $myURL);
53
        curl_setopt($ch, CURLOPT_POSTFIELDS, array("FoxyData" => $XMLOutput_encrypted));
54
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
55
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
56
57
        $response = curl_exec($ch);
58
        $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
59
        curl_close($ch);
60
61
        if ($responseCode <= 400) {
62
            header("content-type:text/plain");
63
        }
64
        print $response;
65
    }
66
67
    /**
68
     *
69
     */
70
    private function updateConfig()
71
    {
72
        $transaction_date = static::config()->get('transaction_date');
73
        static::config()->update('transaction_date', strtotime($transaction_date));
74
75
        $order_id = static::config()->get('order_id');
76
        if ($order_id === 'auto' || $order_id < 1) {
77
            $lastOrderID = Order::get()->sort('Order_ID')->last()->Order_ID;
78
            static::config()->update('order_id', $lastOrderID + 1);
79
        }
80
81
82
    }
83
}
84