Completed
Push — master ( 016a0b...25fc98 )
by Franco
12s
created

EmailRecipientCondition::matches()   C

Complexity

Conditions 12
Paths 30

Size

Total Lines 42
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 36
nc 30
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\UserForms\Model\Recipient;
4
5
use LogicException;
6
use SilverStripe\CMS\Controllers\CMSMain;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\UserForms\Model\Recipient\EmailRecipient;
10
use SilverStripe\UserForms\Model\EditableFormField;
11
12
/**
13
 * Declares a condition that determines whether an email can be sent to a given recipient
14
 *
15
 * @method EmailRecipient Parent()
16
 *
17
 * @property Enum ConditionOption
18
 * @property Varchar ConditionValue
19
 *
20
 * @method EditableFormField ConditionField
21
 */
22
class EmailRecipientCondition extends DataObject
23
{
24
    /**
25
     * List of options
26
     *
27
     * @config
28
     * @var array
29
     */
30
    private static $condition_options = [
0 ignored issues
show
Unused Code introduced by
The property $condition_options 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...
31
        'IsBlank' => 'Is blank',
32
        'IsNotBlank' => 'Is not blank',
33
        'Equals' => 'Equals',
34
        'NotEquals' => "Doesn't equal",
35
        'ValueLessThan' => 'Less than',
36
        'ValueLessThanEqual' => 'Less than or equal',
37
        'ValueGreaterThan' => 'Greater than',
38
        'ValueGreaterThanEqual' => 'Greater than or equal'
39
    ];
40
41
    private static $db = [
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...
42
        'ConditionOption' => 'Enum("IsBlank,IsNotBlank,Equals,NotEquals,ValueLessThan,ValueLessThanEqual,ValueGreaterThan,ValueGreaterThanEqual")',
43
        'ConditionValue' => 'Varchar'
44
    ];
45
46
    private static $has_one = [
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...
47
        'Parent' => EmailRecipient::class,
48
        'ConditionField' => EditableFormField::class
49
    ];
50
51
    private static $table_name = 'UserDefinedForm_EmailRecipientCondition';
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 $table_name 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...
52
53
    /**
54
     *
55
     * Determine if this rule matches the given condition
56
     *
57
     * @param $data
58
     *
59
     * @return bool|null
60
     * @throws LogicException
61
     */
62
    public function matches($data)
63
    {
64
        $fieldName = $this->ConditionField()->Name;
0 ignored issues
show
Documentation Bug introduced by
The method ConditionField does not exist on object<SilverStripe\User...mailRecipientCondition>? 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
        $fieldValue = isset($data[$fieldName]) ? $data[$fieldName] : null;
66
        $conditionValue = $this->ConditionValue;
67
        $result = null;
0 ignored issues
show
Unused Code introduced by
$result 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...
68
        switch ($this->ConditionOption) {
69
            case 'IsBlank':
70
                $result = empty($fieldValue);
71
                break;
72
            case 'IsNotBlank':
73
                $result = !empty($fieldValue);
74
                break;
75
            case 'ValueLessThan':
76
                $result = ($fieldValue < $conditionValue);
77
                break;
78
            case 'ValueLessThanEqual':
79
                $result = ($fieldValue <= $conditionValue);
80
                break;
81
            case 'ValueGreaterThan':
82
                $result = ($fieldValue > $conditionValue);
83
                break;
84
            case 'ValueGreaterThanEqual':
85
                $result = ($fieldValue >= $conditionValue);
86
                break;
87
            case 'NotEquals':
88
            case 'Equals':
89
                $result = is_array($fieldValue)
90
                    ? in_array($conditionValue, $fieldValue)
91
                    : $fieldValue == $conditionValue;
92
93
                if ($this->ConditionOption == 'NotEquals') {
94
                    $result = !($result);
95
                }
96
                break;
97
            default:
98
                throw new LogicException("Unhandled rule {$this->ConditionOption}");
99
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
100
        }
101
102
        return $result;
103
    }
104
105
        /**
106
     * Return whether a user can create an object of this type
107
     *
108
     * @param Member $member
109
     * @param array $context Virtual parameter to allow context to be passed in to check
110
     * @return bool
111
     */
112 View Code Duplication
    public function canCreate($member = null, $context = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114
        // Check parent page
115
        $parent = $this->getCanCreateContext(func_get_args());
116
        if ($parent) {
117
            return $parent->canEdit($member);
118
        }
119
120
        // Fall back to secure admin permissions
121
        return parent::canCreate($member);
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::canCreate($member); of type boolean|string adds the type string to the return on line 121 which is incompatible with the return type documented by SilverStripe\UserForms\M...entCondition::canCreate of type boolean.
Loading history...
122
    }
123
124
    /**
125
     * Helper method to check the parent for this object
126
     *
127
     * @param array $args List of arguments passed to canCreate
128
     * @return SiteTree Parent page instance
129
     */
130 View Code Duplication
    protected function getCanCreateContext($args)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132
        // Inspect second parameter to canCreate for a 'Parent' context
133
        if (isset($args[1]['Parent'])) {
134
            return $args[1]['Parent'];
135
        }
136
        // Hack in currently edited page if context is missing
137
        if (Controller::has_curr() && Controller::curr() instanceof CMSMain) {
138
            return Controller::curr()->currentPage();
139
        }
140
141
        // No page being edited
142
        return null;
143
    }
144
145
    /**
146
     * @param Member
147
     *
148
     * @return boolean
149
     */
150
    public function canView($member = null)
151
    {
152
        return $this->Parent()->canView($member);
153
    }
154
155
    /**
156
     * @param Member
157
     *
158
     * @return boolean
159
     */
160
    public function canEdit($member = null)
161
    {
162
        return $this->Parent()->canEdit($member);
163
    }
164
165
    /**
166
     * @param Member
167
     *
168
     * @return boolean
169
     */
170
    public function canDelete($member = null)
171
    {
172
        return $this->canEdit($member);
173
    }
174
}
175