OrderReport_Popup::SiteConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This is a stand-alone controller, designed to be
4
 * used with the eCommerce reporting system.
5
 *
6
 * It allows a user to view a template for a packing
7
 * slip of an order, or an invoice with status logs.
8
 *
9
 * @see CurrentOrdersReport
10
 * @see UnprintedOrderReport
11
 *
12
 *
13
 *
14
 * @authors: Silverstripe, Jeremy, Nicolaas
15
 *
16
 * @package: ecommerce
17
 * @sub-package: reports
18
 *
19
 **/
20
21
class OrderReport_Popup extends Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
22
{
23
24
    //basic security for controller
25
    public static $allowed_actions = array(
26
        'index' => 'SHOPADMIN',
27
        'packingslip' => 'SHOPADMIN',
28
        'invoice' => 'SHOPADMIN'
29
    );
30
31
    public function init()
0 ignored issues
show
Coding Style introduced by
init uses the super-global variable $_REQUEST 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...
32
    {
33
        parent::init();
34
        //include print javascript, if print argument is provided
35
        if (isset($_REQUEST['print']) && $_REQUEST['print']) {
36
            Requirements::customScript("if(document.location.href.indexOf('print=1') > 0) {window.print();}");
0 ignored issues
show
Documentation introduced by
'if(document.location.hr...> 0) {window.print();}' is of type string, but the function expects a object<The>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
        }
38
        $this->Title = i18n::_t("ORDER.INVOICE", "Invoice");
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<OrderReport_Popup>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
39
        if ($id = $this->urlParams['ID']) {
40
            $this->Title .= " #$id";
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<OrderReport_Popup>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
41
        }
42
        /*Requirements::themedCSS("reset");*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
43
        /*Requirements::themedCSS("OrderReport");*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
        /*Requirements::themedCSS("OrderReport_Print", "print");*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
45
    }
46
47
    /**
48
     * This is the default action of this
49
     * controller without calling any
50
     * explicit action, such as "show".
51
     *
52
     * This default "action" will show
53
     * order information in a printable view.
54
     */
55
    public function index()
56
    {
57
        Requirements::themedCSS("OrderReport");
58
        return $this->renderWith('Order_Printable');
59
    }
60
61
62
    public function Link($action = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
63
    {
64
        return "OrderReport_Popup/$action";
65
    }
66
67
    /**
68
     * This method is used primarily for cheque orders.
69
     *
70
     * @return unknown
0 ignored issues
show
Documentation introduced by
Should the return type not be ArrayData|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
71
     */
72
    public function SingleOrder()
73
    {
74
        $id = $this->urlParams['ID'];
75
76
        if (is_numeric($id)) {
77
            $order = Order::get_by_id_if_can_view($id);
78
            $payment = $order->Payment();
79
            $cheque = false;
80
81
            if ($record = $payment->First()) {
82
                if ($record->ClassName == 'ChequePayment') {
83
                    $cheque = true;
84
                }
85
            }
86
87
            return new ArrayData(array(
88
                'DisplayFinalisedOrder' => $order,
89
                'IsCheque' => $cheque
90
            ));
91
        }
92
93
        return false;
94
    }
95
96
    /**
97
     * @TODO Get orders by ID or using current filter if ID is not numeric (for getting all orders)
98
     * @TODO Define what the role of this method is. Is it for templates, is it for a report?
99
     *
100
     * @return unknown
101
     */
102
    public function DisplayFinalisedOrder()
0 ignored issues
show
Coding Style introduced by
DisplayFinalisedOrder uses the super-global variable $_REQUEST 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...
103
    {
104
        $id = $this->urlParams['ID'];
105
106
        if (is_numeric($id)) {
107
            $order = Order::get_by_id_if_can_view($id);
108
            if (isset($_REQUEST['print'])) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
109
                //$order->updatePrinted(true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
110
            }
111
112
            return $order;
113
        }
114
115
        return false;
116
    }
117
118
    public function SiteConfig()
119
    {
120
        return SiteConfig::current_site_config();
121
    }
122
}
123