Completed
Push — master ( aa83e2...f8f77e )
by Jason
22:25 queued 07:26
created

FoxyStripe_Controller   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 90
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getURLSegment() 0 3 1
B index() 0 24 4
B handleDataFeed() 0 23 4
B sso() 0 24 2
1
<?php
2
3
class FoxyStripe_Controller extends Page_Controller {
4
	
5
	const URLSegment = 'foxystripe';
6
7
	public function getURLSegment() {
8
		return self::URLSegment;
9
	}
10
	
11
	static $allowed_actions = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $allowed_actions.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
12
		'index',
13
        'sso'
14
	);
15
	
16
	public function index() {
0 ignored issues
show
Coding Style introduced by
index uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
17
18
	    // handle POST from FoxyCart API transaction
19
		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...
20
21
			$FoxyData_encrypted = (isset($_POST["FoxyData"])) ?
22
                urldecode($_POST["FoxyData"]) :
23
                urldecode($_POST["FoxySubscriptionData"]);
24
			$FoxyData_decrypted = rc4crypt::decrypt(FoxyCart::getStoreKey(),$FoxyData_encrypted);
25
26
            // parse the response and save the order
27
			self::handleDataFeed($FoxyData_encrypted, $FoxyData_decrypted);
28
			
29
			// extend to allow for additional integrations with Datafeed
30
			$this->extend('addIntegrations', $FoxyData_encrypted);
31
			
32
			return 'foxy';
33
			
34
		} else {
35
			
36
			return "No FoxyData or FoxySubscriptionData received.";
37
			
38
		}
39
	}
40
41
    public function handleDataFeed($encrypted, $decrypted){
42
43
        $orders = new SimpleXMLElement($decrypted);
44
45
        // loop over each transaction to find FoxyCart Order ID
46
        foreach ($orders->transactions->transaction as $transaction) {
47
48
            // if FoxyCart order id, then parse order
49
            if (isset($transaction->id)) {
50
51
                ($order = Order::get()->filter('Order_ID', (int) $transaction->id)->First()) ?
52
                    $order = Order::get()->filter('Order_ID', (int) $transaction->id)->First() :
53
                    $order = Order::create();
54
55
                // save base order info
56
                $order->Order_ID = (int) $transaction->id;
57
                $order->Response = urlencode($encrypted);
58
                $order->write();
59
60
            }
61
62
        }
63
    }
64
65
66
	// Single Sign on integration with FoxyCart
67
    public function sso() {
68
69
	    // GET variables from FoxyCart Request
70
        $fcsid = $this->request->getVar('fcsid');
71
        $timestampNew = strtotime('+30 days');
72
73
        // get current member if logged in. If not, create a 'fake' user with Customer_ID = 0
74
        // fake user will redirect to FC checkout, ask customer to log in
75
        // to do: consider a login/registration form here if not logged in
76
        if($Member = Member::currentUser()) {
0 ignored issues
show
Unused Code introduced by
$Member 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...
77
            $Member = Member::currentUser();
78
        } else {
79
            $Member = new Member();
80
            $Member->Customer_ID = 0;
81
        }
82
83
        $auth_token = sha1($Member->Customer_ID . '|' . $timestampNew . '|' . FoxyCart::getStoreKey());
84
85
        $redirect_complete = 'https://' . FoxyCart::getFoxyCartStoreName() . '.foxycart.com/checkout?fc_auth_token=' . $auth_token .
86
            '&fcsid=' . $fcsid . '&fc_customer_id=' . $Member->Customer_ID . '&timestamp=' . $timestampNew;
87
	
88
	    $this->redirect($redirect_complete);
89
90
    }
91
	
92
}