OrderStepField::performReadonlyTransformation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
nc 1
nop 0
1
<?php
2
3
4
/**
5
 * This field shows the admin (and maybe the customer) where the Order is at (which orderstep).
6
 *
7
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
8
 * @package: ecommerce
9
 * @sub-package: forms
10
 * @inspiration: Silverstripe Ltd, Jeremy
11
 **/
12
class OrderStepField extends DatalessField
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $content;
18
19
    /**
20
     * @param string $name
21
     * @param Order  $order
22
     * @param Member $member
0 ignored issues
show
Documentation introduced by
Should the type for parameter $member not be null|Member?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
23
     */
24
    public function __construct($name, Order $order, Member $member = null)
25
    {
26
        if (!$member) {
27
            $member = $order->Member();
0 ignored issues
show
Bug introduced by
The method Member() does not exist on Order. Did you maybe mean CreateOrReturnExistingMember()?

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...
28
        }
29
        if (!$member) {
30
            $member = new Member();
31
        }
32
        $orderSteps = OrderStep::get();
33
        $where = '"HideStepFromCustomer" = 0';
0 ignored issues
show
Unused Code introduced by
$where 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...
34
        $currentStep = $order->CurrentStepVisibleToCustomer();
0 ignored issues
show
Unused Code introduced by
$currentStep 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...
35
        if ($member->IsShopAdmin()) {
36
            $currentStep = $order->MyStep();
37
        } else {
38
            $currentStep = $order->CurrentStepVisibleToCustomer();
39
            $orderSteps = $orderSteps
40
                ->filter(array('HideStepFromCustomer' => 0));
41
        }
42
        $future = false;
43
        $html = '
44
        <div class="orderStepField">
45
            <ol>';
46
        if ($orderSteps->count()) {
47
            foreach ($orderSteps as $orderStep) {
48
                if ($orderStep->HideFromEveryone()) {
49
                    continue;
50
                }
51
                $description = '';
52
                if ($member->IsShopAdmin()) {
53
                    if ($orderStep->Description) {
54
                        $description = ' title="'.Convert::raw2att($orderStep->Description).'" ';
55
                    }
56
                }
57
                $class = '';
58
                if ($orderStep->ID == $currentStep->ID) {
59
                    $future = true;
60
                    $class .= ' current';
61
                } elseif ($future) {
62
                    $class .= ' todo';
63
                } else {
64
                    $class .= ' done';
65
                }
66
                $html .= '<li class="'.$class.'" '.$description.'><a href="'.$orderStep->CMSEditLink().'">'.$orderStep->Title.'</a></li>';
67
            }
68
        } else {
69
            $html .= 'no steps';
70
        }
71
        $html .= '</ol><div class="clear"></div></div>';
72
        $this->content = $html;
73
        Requirements::themedCSS('OrderStepField', 'ecommerce');
74
        parent::__construct($name);
75
    }
76
77
    /**
78
     * standard SS method.
79
     *
80
     * @param array $properties
81
     *
82
     * @return HTML
83
     */
84
    public function FieldHolder($properties = array())
85
    {
86
        return is_object($this->content) ? $this->content->forTemplate() : $this->content;
87
    }
88
89
    /**
90
     * standard SS method.
91
     *
92
     * @param array $properties
93
     *
94
     * @return HTML
95
     */
96
    public function Field($properties = array())
97
    {
98
        return $this->FieldHolder();
99
    }
100
101
    /**
102
     * Sets the content of this field to a new value.
103
     *
104
     * @param string $content
105
     */
106
    public function setContent($content)
107
    {
108
        $this->content = $content;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getContent()
115
    {
116
        return $this->content;
117
    }
118
119
    /**
120
     * Synonym of {@link setContent()} so that LiteralField is more compatible with other field types.
121
     *
122
     * @param mixed
123
     */
124
    public function setValue($value)
125
    {
126
        return $this->setContent($value);
127
    }
128
129
    /**
130
     * standard SS method.
131
     *
132
     * @return Field
0 ignored issues
show
Documentation introduced by
Should the return type not be OrderStepField?

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...
133
     */
134
    public function performReadonlyTransformation()
135
    {
136
        $clone = clone $this;
137
        $clone->setReadonly(true);
138
139
        return $clone;
140
    }
141
}
142