Completed
Pull Request — master (#347)
by Matthew
03:26
created

DataTestController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
eloc 57
dl 0
loc 97
ccs 0
cts 54
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateEmail() 0 16 3
A index() 0 38 1
A updateConfig() 0 19 4
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
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Dev\DebugView;
13
use SilverStripe\Security\Member;
14
15
/**
16
 * Class DataTestController
17
 * @package Dynamic\FoxyStripe\Controller
18
 */
19
class DataTestController extends Controller
20
{
21
22
    private static $data = [
0 ignored issues
show
introduced by
The private property $data is not used, and could be removed.
Loading history...
23
        "TransactionDate" => "now",
24
        "OrderID" => "auto",
25
        "Email"=> "auto",
26
    ];
27
28
    /**
29
     * @throws \SilverStripe\ORM\ValidationException
30
     */
31
    public function index()
32
    {
33
        $rules = Director::config()->get('rules');
34
        $rule = array_search(FoxyStripeController::class, $rules);
35
        $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

35
        $myURL = Director::absoluteBaseURL() . explode('//', /** @scrutinizer ignore-type */ $rule)[0];
Loading history...
36
        $myKey = FoxyCart::getStoreKey();
37
38
        $this->updateConfig();
39
40
        $config = static::config()->get('data');
41
        $XMLOutput = $this->renderWith(
42
            'TestData',
43
            $config
44
        )->RAW();
45
46
        $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

46
        $XMLOutput_encrypted = \rc4crypt::encrypt(/** @scrutinizer ignore-type */ $myKey, $XMLOutput);
Loading history...
47
        $XMLOutput_encrypted = urlencode($XMLOutput_encrypted);
48
49
        $ch = curl_init();
50
        curl_setopt($ch, CURLOPT_URL, $myURL);
51
        curl_setopt($ch, CURLOPT_POSTFIELDS, array("FoxyData" => $XMLOutput_encrypted));
52
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
53
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
54
55
        $response = curl_exec($ch);
56
        $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
0 ignored issues
show
Unused Code introduced by
The assignment to $responseCode is dead and can be removed.
Loading history...
57
        curl_close($ch);
58
59
        $configString = print_r($config, true);
60
        /** @var DebugView $view */
61
        $view = Injector::inst()->create(DebugView::class);
62
        echo $view->renderHeader();
63
        echo '<div class="info">';
64
        echo "<h2>Data: </h2><pre>{$configString}</pre>";
65
        echo "<h2>Response: </h2><pre>$response</pre>";
66
        echo '<p></p>';
67
        echo '</div>';
68
        echo $view->renderFooter();
69
    }
70
71
    /**
72
     *
73
     */
74
    private function updateConfig()
75
    {
76
        $transaction_date = static::config()->get('data')['TransactionDate'];
77
        static::config()->merge('data', [
78
            'TransactionDate' => strtotime($transaction_date),
79
        ]);
80
81
        $order_id = static::config()->get('data')['OrderID'];
82
        if ($order_id === 'auto' || $order_id < 1) {
83
            $lastOrderID = Order::get()->sort('Order_ID')->last()->Order_ID;
84
            static::config()->merge('data', [
85
                'OrderID' => $lastOrderID + 1,
86
            ]);
87
        }
88
89
        $email = static::config()->get('data')['Email'];
90
        if ($email === 'auto') {
91
            static::config()->merge('data', [
92
                'Email' => $this->generateEmail(),
93
            ]);
94
        }
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    private function generateEmail()
101
    {
102
        $emails = Member::get()->filter([
103
            'Email:EndsWith' => '@example.com',
104
        ])->column('Email');
105
106
        if ($emails && count($emails)) {
107
            $email = $emails[count($emails) - 1];
108
            return preg_replace_callback(
109
                "|(\d+)|",
110
                function($mathces) {
111
                    return ++$mathces[1];
112
                },
113
                $email);
114
        }
115
        return '[email protected]';
116
    }
117
}
118