Completed
Pull Request — master (#309)
by Jason
05:20
created

FoxyStripeController::handleDataFeed()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 0
cts 10
cp 0
rs 9.2
cc 4
eloc 9
nc 4
nop 2
crap 20
1
<?php
2
3
namespace Dynamic\FoxyStripe\Controller;
4
5
use Dynamic\FoxyStripe\Model\FoxyCart;
6
use Dynamic\FoxyStripe\Model\Order;
7
use SilverStripe\Security\Member;
8
9
class FoxyStripeController extends \PageController
10
{
11
    /**
12
     *
13
     */
14
    const URLSEGMENT = 'foxystripe';
15
16
    /**
17
     * @return string
18
     */
19
    public function getURLSegment()
20
    {
21
        return self::URLSEGMENT;
22
    }
23
24
    /**
25
     * @var array
26
     */
27
    private static $allowed_actions = array(
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
28
        'index',
29
        'sso',
30
    );
31
32
    /**
33
     * @return string
34
     *
35
     * @throws \SilverStripe\ORM\ValidationException
36
     */
37
    public function index()
38
    {
39
        // handle POST from FoxyCart API transaction
40
        if ((isset($_POST['FoxyData']) or isset($_POST['FoxySubscriptionData']))) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
41
            $FoxyData_encrypted = (isset($_POST['FoxyData'])) ?
42
                urldecode($_POST['FoxyData']) :
43
                urldecode($_POST['FoxySubscriptionData']);
44
            $FoxyData_decrypted = \rc4crypt::decrypt(FoxyCart::getStoreKey(), $FoxyData_encrypted);
0 ignored issues
show
Bug introduced by
The type rc4crypt 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...
45
46
            // parse the response and save the order
47
            self::handleDataFeed($FoxyData_encrypted, $FoxyData_decrypted);
0 ignored issues
show
Bug Best Practice introduced by
The method Dynamic\FoxyStripe\Contr...oller::handleDataFeed() is not static, but was called statically. ( Ignorable by Annotation )

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

47
            self::/** @scrutinizer ignore-call */ 
48
                  handleDataFeed($FoxyData_encrypted, $FoxyData_decrypted);
Loading history...
48
49
            // extend to allow for additional integrations with Datafeed
50
            $this->extend('addIntegrations', $FoxyData_encrypted);
51
52
            return 'foxy';
53
        } else {
54
            return 'No FoxyData or FoxySubscriptionData received.';
55
        }
56
    }
57
58
    /**
59
     * @param $encrypted
60
     * @param $decrypted
61
     *
62
     * @throws \SilverStripe\ORM\ValidationException
63
     */
64
    public function handleDataFeed($encrypted, $decrypted)
65
    {
66
        $orders = new \SimpleXMLElement($decrypted);
67
68
        // loop over each transaction to find FoxyCart Order ID
69
        foreach ($orders->transactions->transaction as $transaction) {
70
            // if FoxyCart order id, then parse order
71
            if (isset($transaction->id)) {
72
                ($order = Order::get()->filter('Order_ID', (int) $transaction->id)->First()) ?
0 ignored issues
show
Unused Code introduced by
The assignment to $order is dead and can be removed.
Loading history...
73
                    $order = Order::get()->filter('Order_ID', (int) $transaction->id)->First() :
74
                    $order = Order::create();
75
76
                // save base order info
77
                $order->Order_ID = (int) $transaction->id;
78
                $order->Response = urlencode($encrypted);
79
                $order->write();
80
            }
81
        }
82
    }
83
84
    /**
85
     * Single Sign on integration with FoxyCart.
86
     */
87
    public function sso()
88
    {
89
90
        // GET variables from FoxyCart Request
91
        $fcsid = $this->request->getVar('fcsid');
92
        $timestampNew = strtotime('+30 days');
93
94
        // get current member if logged in. If not, create a 'fake' user with Customer_ID = 0
95
        // fake user will redirect to FC checkout, ask customer to log in
96
        // to do: consider a login/registration form here if not logged in
97
        if ($Member = Member::currentUser()) {
0 ignored issues
show
Unused Code introduced by
The assignment to $Member is dead and can be removed.
Loading history...
Deprecated Code introduced by
The function SilverStripe\Security\Member::currentUser() has been deprecated: 5.0.0 use Security::getCurrentUser() ( Ignorable by Annotation )

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

97
        if ($Member = /** @scrutinizer ignore-deprecated */ Member::currentUser()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
98
            $Member = Member::currentUser();
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Security\Member::currentUser() has been deprecated: 5.0.0 use Security::getCurrentUser() ( Ignorable by Annotation )

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

98
            $Member = /** @scrutinizer ignore-deprecated */ Member::currentUser();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
99
        } else {
100
            $Member = new Member();
101
            $Member->Customer_ID = 0;
102
        }
103
104
        $auth_token = sha1($Member->Customer_ID.'|'.$timestampNew.'|'.FoxyCart::getStoreKey());
105
106
        $redirect_complete = 'https://'.FoxyCart::getFoxyCartStoreName().'.foxycart.com/checkout?fc_auth_token='.
107
            $auth_token.'&fcsid='.$fcsid.'&fc_customer_id='.$Member->Customer_ID.'&timestamp='.$timestampNew;
108
109
        $this->redirect($redirect_complete);
110
    }
111
}
112