Completed
Push — master ( 21af4b...3bae41 )
by Nicolaas
03:29
created

OrderStatusLog_WhitelistCustomer::canDelete()   A

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 1
1
<?php
2
3
4
/**
5
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
6
 * @package: ecommerce
7
 * @sub-package: model
8
 * @inspiration: Silverstripe Ltd, Jeremy
9
 **/
10
class OrderStatusLog_WhitelistCustomer extends OrderStatusLog
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...
11
{
12
    /**
13
     * @var int
14
     */
15
    private static $min_number_of_paid_orders_required = 1;
0 ignored issues
show
Unused Code introduced by
The property $min_number_of_paid_orders_required is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
17
    private static $db = 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...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
18
        'Whitelist' => 'Boolean'
19
    );
20
21
    private static $has_one = 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...
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
        'Member' => 'Member',
23
        'BasedOn' => 'OrderStatusLog_WhitelistCustomer'
24
    );
25
26
    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...
Unused Code introduced by
The property $defaults is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
27
        'InternalUseOnly' => true
28
    );
29
30
    private static $singular_name = 'Whitelist Customer Record';
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...
31
    public function i18n_singular_name()
32
    {
33
        return self::$singular_name;
34
    }
35
36
    private static $plural_name = 'Whitelist Customer Records';
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...
37
    public function i18n_plural_name()
38
    {
39
        return self::$plural_name;
40
    }
41
42
    public function canCreate($member = null)
43
    {
44
        return false;
45
    }
46
47
    public function canEdit($member = null)
48
    {
49
        return parent::canEdit($member);
50
    }
51
52
    public function canDelete($member = null)
53
    {
54
        return false;
55
    }
56
57
    public function getCMSFields() {
58
        $fields = parent::getCMSFields();
59
        $fields->replaceField(
60
            'BasedOnID',
61
            CMSEditLinkField::create(
62
                'BasedOnID',
63
                _t('OrderStatusLog_WhitelistCustomer.BASED_ON', 'Based on'),
64
                $this->BasedOn()
0 ignored issues
show
Documentation Bug introduced by
The method BasedOn does not exist on object<OrderStatusLog_WhitelistCustomer>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
65
            )
66
        );
67
        $fields->replaceField(
68
            'MemberID',
69
            CMSEditLinkField::create(
70
                'MemberID',
71
                _t('OrderStatusLog_WhitelistCustomer.CUSTOMER', 'Customer'),
72
                $this->Member()
0 ignored issues
show
Bug introduced by
The method Member() does not exist on OrderStatusLog_WhitelistCustomer. Did you maybe mean member_is_whitelisted()?

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...
73
            )
74
        );
75
        return $fields;
76
    }
77
78
    /**
79
     *
80
     *
81
     * @param  Member  $member  the member to check
82
     * @return boolean          returns true of the member has been whitelisted before
83
     */
84
    public static function member_is_whitelisted(Member $member)
85
    {
86
        return OrderStatusLog_WhitelistCustomer::get()
87
            ->filter(
88
                array(
89
                    'MemberID' => $member->ID,
90
                    'Whitelist' => 1
91
                )
92
            )->count() ? true : false;
93
    }
94
95
    public function onAfterWrite()
96
    {
97
        parent::onAfterWrite();
98
        if ($this->Whitelist) {
0 ignored issues
show
Documentation introduced by
The property Whitelist does not exist on object<OrderStatusLog_WhitelistCustomer>. 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...
99
            if ($member = $this->Member()) {
0 ignored issues
show
Bug introduced by
The method Member() does not exist on OrderStatusLog_WhitelistCustomer. Did you maybe mean member_is_whitelisted()?

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...
100
                if ($member->exists()) {
101
                    $member->IsWhitelisted = true;
102
                    $member->write();
103
                }
104
            }
105
        }
106
    }
107
108
    public function assessCustomer()
109
    {
110
        //already done ...
111
        if($this->Whitelist) {
0 ignored issues
show
Documentation introduced by
The property Whitelist does not exist on object<OrderStatusLog_WhitelistCustomer>. 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...
112
            return true;
113
        }
114
        $order = $this->Order();
0 ignored issues
show
Documentation Bug introduced by
The method Order does not exist on object<OrderStatusLog_WhitelistCustomer>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
115
        if ($order && $order->exists()) {
116
            if ($order->MemberID) {
117
                $this->MemberID = $order->MemberID;
0 ignored issues
show
Documentation introduced by
The property MemberID does not exist on object<OrderStatusLog_WhitelistCustomer>. 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...
118
                $this->Whitelist = false;
0 ignored issues
show
Documentation introduced by
The property Whitelist does not exist on object<OrderStatusLog_WhitelistCustomer>. 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...
119
                $member = $order->Member();
120
                if ($member && $member->exists()) {
121
                    //check if member has previouly been whitelisted
122
                    $previousOne = OrderStatusLog_WhitelistCustomer::get()
123
                        ->filter(
124
                            array(
125
                                'Whitelist' => 1,
126
                                'MemberID' => $member->ID
127
                            )
128
                        )
129
                        ->exclude(
130
                            array('OrderID' => $order->ID)
131
                        )->first();
132
                    if ($previousOne) {
133
                        $this->Whitelist = true;
0 ignored issues
show
Documentation introduced by
The property Whitelist does not exist on object<OrderStatusLog_WhitelistCustomer>. 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...
134
                        $this->BasedOnID = $previousOne->ID;
0 ignored issues
show
Documentation introduced by
The property BasedOnID does not exist on object<OrderStatusLog_WhitelistCustomer>. 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...
135
                    } else {
136
137
                        //member is already whitelisted
138
                        $previousOne = OrderStatusLog_WhitelistCustomer::get()
139
                            ->filter(
140
                                array(
141
                                    'MemberID' => $member->ID
142
                                )
143
                            )
144
                            ->exclude(
145
                                array('OrderID' => $order->ID)
146
                            )->first();
147
                        if ($previousOne) {
148
                            $this->Whitelist = true;
0 ignored issues
show
Documentation introduced by
The property Whitelist does not exist on object<OrderStatusLog_WhitelistCustomer>. 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...
149
                            $this->BasedOnID = $previousOne->ID;
0 ignored issues
show
Documentation introduced by
The property BasedOnID does not exist on object<OrderStatusLog_WhitelistCustomer>. 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...
150
                        } else {
151
                            //member has placed orders before
152
                            $previousOrders = Order::get()
153
                                ->filter(
154
                                    array(
155
                                        'MemberID' => $member->ID,
156
                                        'CancelledByID' => 0
157
                                    )
158
                                )
159
                                ->exclude(
160
                                    array(
161
                                        'ID' => $order->ID
162
                                    )
163
                                );
164
                            $count = 0;
165
                            $minOrdersRequired = $this->Config()->get('min_number_of_paid_orders_required');
166
                            foreach($previousOrders as $previousOrder) {
167
                                if($previousOrder->IsPaid()) {
168
                                    $count++;
169
                                    if($count >= $minOrdersRequired) {
170
                                        $this->Whitelist = true;
0 ignored issues
show
Documentation introduced by
The property Whitelist does not exist on object<OrderStatusLog_WhitelistCustomer>. 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...
171
                                        break;
172
                                    }
173
                                }
174
                            }
175
                        }
176
                    }
177
                }
178
                $this->write();
179
            }
180
        }
181
    }
182
}
183