OrderStep_Created   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 125
rs 10
c 0
b 0
f 0
wmc 21
lcom 0
cbo 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initStep() 0 4 1
A doStep() 0 14 4
A nextStep() 0 8 2
C addOrderStepFields() 0 39 13
A myDescription() 0 4 1
1
<?php
2
3
4
/**
5
 * This is the first Order Step.
6
 *
7
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
8
 * @package: ecommerce
9
 * @sub-package: model
10
 * @inspiration: Silverstripe Ltd, Jeremy
11
 **/
12
class OrderStep_Created extends OrderStep implements OrderStepInterface
13
{
14
    private static $defaults = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
15
        'CustomerCanEdit' => 1,
16
        'CustomerCanPay' => 1,
17
        'CustomerCanCancel' => 1,
18
        'Name' => 'Create',
19
        'Code' => 'CREATED',
20
        'ShowAsUncompletedOrder' => 1,
21
    );
22
23
    /**
24
     *initStep:
25
     * makes sure the step is ready to run.... (e.g. check if the order is ready to be emailed as receipt).
26
     * should be able to run this function many times to check if the step is ready.
27
     *
28
     * @see Order::doNextStatus
29
     *
30
     * @param Order object
31
     *
32
     * @return bool - true if the current step is ready to be run...
33
     **/
34
    public function initStep(Order $order)
35
    {
36
        return true;
37
    }
38
39
    /**
40
     * Add the member to the order, in case the member is not an admin.
41
     *
42
     * @param DataObject - $order Order
43
     *
44
     * @return bool
45
     **/
46
    public function doStep(Order $order)
47
    {
48
        if (!$order->MemberID) {
0 ignored issues
show
Documentation introduced by
The property MemberID does not exist on object<Order>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
49
            $member = Member::currentUser();
50
            if ($member) {
51
                if (!$member->IsShopAdmin()) {
52
                    $order->MemberID = $member->ID();
0 ignored issues
show
Documentation introduced by
The property MemberID does not exist on object<Order>. 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...
53
                    $order->write();
54
                }
55
            }
56
        }
57
58
        return true;
59
    }
60
61
    /**
62
     * We can run the next step, once any items have been added.
63
     *
64
     * @see Order::doNextStatus
65
     *
66
     * @param Order $order
67
     *
68
     * @return OrderStep | Null (next step OrderStep object)
0 ignored issues
show
Documentation introduced by
Should the return type not be OrderStep|null?

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...
69
     **/
70
    public function nextStep(Order $order)
71
    {
72
        if ($order->TotalItems($recalculate = true)) {
73
            return parent::nextStep($order);
74
        }
75
76
        return;
77
    }
78
79
    /**
80
     * Allows the opportunity for the Order Step to add any fields to Order::getCMSFields.
81
     *
82
     *@param FieldList $fields
83
     *@param Order $order
84
     *
85
     *@return FieldList
86
     **/
87
    public function addOrderStepFields(FieldList $fields, Order $order)
88
    {
89
        $fields = parent::addOrderStepFields($fields, $order);
90
        if (!$order->IsSubmitted()) {
91
            //LINE BELOW IS NOT REQUIRED
92
            $header = _t('OrderStep.SUBMITORDER', 'Submit Order');
93
            $label = _t('OrderStep.SUBMITNOW', 'Submit Now');
94
            $msg = _t('OrderStep.MUSTDOSUBMITRECORD', '<p>Tick the box below to submit this order.</p>');
95
            $problems = array();
96
            if (!$order->getTotalItems()) {
97
                $problems[] = 'There are no --- Order Items (products) --- associated with this order.';
98
            }
99
            if (!$order->MemberID) {
0 ignored issues
show
Documentation introduced by
The property MemberID does not exist on object<Order>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
100
                $problems[] = 'There is no --- Customer --- associated with this order.';
101
            }
102
            if (!$order->BillingAddressID) {
0 ignored issues
show
Documentation introduced by
The property BillingAddressID does not exist on object<Order>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
103
                $problems[] = 'There is no --- Billing Address --- associated with this order.';
104
            } elseif ($billingAddress = $order->BillingAddress()) {
0 ignored issues
show
Bug introduced by
The method BillingAddress() does not exist on Order. Did you maybe mean getBillingAddressField()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
105
                $requiredBillingFields = $billingAddress->getRequiredFields();
106
                if ($requiredBillingFields && is_array($requiredBillingFields) && count($requiredBillingFields)) {
107
                    foreach ($requiredBillingFields as $requiredBillingField) {
108
                        if (!$billingAddress->$requiredBillingField) {
109
                            $problems[] = "There is no --- $requiredBillingField --- recorded in the billing address.";
110
                        }
111
                    }
112
                }
113
            }
114
            if (count($problems)) {
115
                $msg = '<p>You can not submit this order because:</p> <ul><li>'.implode('</li><li>', $problems).'</li></ul>';
116
            }
117
            $fields->addFieldToTab('Root.Next', new HeaderField('CreateSubmitRecordHeader', $header, 3), 'ActionNextStepManually');
118
            $fields->addFieldToTab('Root.Next', new LiteralField('CreateSubmitRecordMessage', $msg), 'ActionNextStepManually');
119
            if (!$problems) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $problems of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
120
                $fields->addFieldToTab('Root.Next', new CheckboxField('SubmitOrderViaCMS', $label), 'ActionNextStepManually');
121
            }
122
        }
123
124
        return $fields;
125
    }
126
127
    /**
128
     * Explains the current order step.
129
     *
130
     * @return string
131
     */
132
    protected function myDescription()
133
    {
134
        return _t('OrderStep.CREATED_DESCRIPTION', 'During this step the customer creates her or his order. The shop admininistrator does not do anything during this step.');
135
    }
136
}
137