Passed
Push — master ( 97d330...e7fc5b )
by Jason
01:48
created

FoxyController::parseFeedData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Foxy\Orders\Controller;
4
5
use Dynamic\Foxy\Model\FoxyHelper;
6
use Dynamic\Foxy\Model\Order;
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\Model\Order was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\ORM\ValidationException;
10
use SilverStripe\Security\Member;
11
use SilverStripe\Security\Security;
12
13
class FoxyController extends Controller
14
{
15
    /**
16
     *
17
     */
18
    const URLSEGMENT = 'foxy';
19
20
    /**
21
     * @var array
22
     */
23
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
24
        'index',
25
    ];
26
27
    /**
28
     * @return string
29
     */
30
    public function getURLSegment()
31
    {
32
        return self::URLSEGMENT;
33
    }
34
35
    /**
36
     * @return string
37
     * @throws ValidationException
38
     */
39
    public function index()
40
    {
41
        $request = $this->getRequest();
42
        if ($request->postVar('FoxyData') || $request->postVar('FoxySubscriptionData')) {
43
            $this->processFoxyRequest($request);
44
            return 'foxy';
45
        }
46
        return 'No FoxyData or FoxySubscriptionData received.';
47
    }
48
49
    /**
50
     * Process a request after a transaction is completed via Foxy
51
     *
52
     * @param HTTPRequest $request
53
     * @throws ValidationException
54
     */
55
    protected function processFoxyRequest(HTTPRequest $request)
56
    {
57
        $encryptedData = $request->postVar('FoxyData')
58
            ? urldecode($request->postVar('FoxyData'))
59
            : urldecode($request->postVar('FoxySubscriptionData'));
60
        $this->parseFeedData($encryptedData);
61
62
        $this->extend('addIntegrations', $encryptedData);
63
    }
64
65
    /**
66
     * Parse the XML data feed from Foxy to a SimpleXMLElement object
67
     *
68
     * @param $encryptedData
69
     * @param $decryptedData
70
     * @throws ValidationException
71
     */
72
    private function parseFeedData($encryptedData)
73
    {
74
        $decryptedData = $this->decryptFeedData($encryptedData);
75
        $orders = new \SimpleXMLElement($decryptedData);
76
        // loop over each transaction to find FoxyCart Order ID
77
        foreach ($orders->transactions->transaction as $transaction) {
78
            $this->processTransaction($transaction, $encryptedData);
79
        }
80
    }
81
82
    /**
83
     * @param $transaction
84
     * @return bool
85
     * @throws ValidationException
86
     */
87
    private function processTransaction($transaction, $encryptedData)
88
    {
89
        if (!isset($transaction->id)) {
90
            return false;
91
        }
92
        if (!$order = Order::get()->filter('OrderID', (int)$transaction->id)->first()) {
93
            $order = Order::create();
94
            $order->OrderID = (int)$transaction->id;
95
        }
96
        $order->Response = urlencode($encryptedData);
97
        $order->write();
98
    }
99
100
    /**
101
     * Decrypt the XML data feed from Foxy
102
     *
103
     * @param $data
104
     * @return string
105
     * @throws \SilverStripe\ORM\ValidationException
106
     */
107
    private function decryptFeedData($data)
108
    {
109
        $helper = FoxyHelper::create();
110
        return \rc4crypt::decrypt($helper->config()->get('secret'), $data);
111
    }
112
}
113