Passed
Push — develop ( fe712b...4742a7 )
by Richard
02:43
created

ConditionallyRequired::__invoke()   C

Complexity

Conditions 12
Paths 20

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 24
c 3
b 0
f 0
dl 0
loc 33
rs 6.9666
cc 12
nc 20
nop 3

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 Riclep\StoryblokForms\Rules;
4
5
use Illuminate\Contracts\Validation\DataAwareRule;
6
use Illuminate\Contracts\Validation\InvokableRule;
7
use Illuminate\Support\Collection;
8
9
class ConditionallyRequired implements DataAwareRule, InvokableRule
10
{
11
12
	/**
13
	 * The condition we want to check the validity of.
14
	 *
15
	 * @var array
16
	 */
17
	protected Collection $conditional;
18
19
	public function __construct($conditional) {
20
		$this->conditional = $conditional;
21
	}
22
23
    /**
24
     * Run the validation rule.
25
     *
26
     * @param string $attribute
27
     * @param  mixed  $value
28
     * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
29
     * @return void
30
     */
31
    public function __invoke($attribute, $value, $fail): void
32
    {
33
	    $causationFieldValue = data_get($this->data, $this->conditional['field']);
34
35
	    // $causationFieldValue is an array? always?
36
37
		$condition = $this->conditional['condition'];
38
		$operator = $this->conditional['operator'];
39
40
		$fieldIsRequired = false;
41
42
	    if ($operator === '>') {
43
		    $fieldIsRequired = $causationFieldValue > $condition;
44
	    } else if ($operator === '<') {
45
		    $fieldIsRequired = $causationFieldValue < $condition;
46
	    } else if ($operator === '==') {
47
		    $fieldIsRequired = $causationFieldValue == $condition;
48
	    }  else if ($operator === '===') {
49
		    $fieldIsRequired = $causationFieldValue === $condition;
50
	    } else if ($operator === '!=') {
51
		    $fieldIsRequired = $causationFieldValue != $condition;
52
	    }  else if ($operator === '!==') {
53
		    $fieldIsRequired = $causationFieldValue !== $condition;
54
	    } else if ($operator === '>=') {
55
		    $fieldIsRequired = $causationFieldValue >= $condition;
56
	    } else if ($operator === '<=') {
57
		    $fieldIsRequired = $causationFieldValue <= $condition;
58
	    } else if ($operator === 'one_of') {
59
			$fieldIsRequired = in_array((int) $causationFieldValue[0], $condition); // always array?
60
	    }
61
62
	    if ($fieldIsRequired && !$value) {
63
		    $fail('This field is required.');
64
	    }
65
    }
66
67
	/**
68
	 * Outputs the HTML version of the validation
69
	 *
70
	 * @return string
71
	 */
72
	public function __toString()
73
	{
74
		return '';
75
	}
76
77
	/**
78
	 * Set the data under validation.
79
	 *
80
	 * @param array $data
81
	 * @return $this
82
	 */
83
	public function setData($data)
84
	{
85
		$this->data = $data;
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
86
87
		return $this;
88
	}
89
}
90