Completed
Push — develop ( 43936d...b3a70f )
by Nate
02:02
created

MinMaxValidator   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 2
dl 0
loc 65
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateAttribute() 0 10 2
D validateValue() 0 25 9
B validateQueryAttribute() 0 14 7
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/domains/license
6
 * @link       https://www.flipboxfactory.com/software/domains/
7
 */
8
9
namespace flipbox\domains\validators;
10
11
use craft\validators\ArrayValidator;
12
use yii\db\QueryInterface;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 1.0.0
17
 */
18
class MinMaxValidator extends ArrayValidator
19
{
20
    /**
21
     * @inheritdoc
22
     */
23
    public function validateAttribute($model, $attribute)
24
    {
25
        $value = $model->$attribute;
26
27
        if($value instanceof QueryInterface) {
28
            return $this->validateQueryAttribute($model, $attribute, $value);
29
        }
30
31
        return parent::validateAttribute($model, $attribute);
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    protected function validateValue($value)
38
    {
39
        if(!$value instanceof QueryInterface) {
40
            return parent::validateValue($value);
41
        }
42
43
        /** @var $value QueryInterface */
44
        if (!is_string($value)) {
45
            return [$this->message, []];
46
        }
47
48
        $count = $value->count();
0 ignored issues
show
Bug introduced by
The method count cannot be called on $value (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
49
50
        if ($this->min !== null && $count < $this->min) {
51
            return [$this->tooFew, ['min' => $this->min]];
52
        }
53
        if ($this->max !== null && $count > $this->max) {
54
            return [$this->tooMany, ['max' => $this->max]];
55
        }
56
        if ($this->count !== null && $count !== $this->count) {
57
            return [$this->notEqual, ['count' => $this->count]];
58
        }
59
60
        return null;
61
    }
62
63
    /**
64
     * @param $model
65
     * @param $attribute
66
     * @param QueryInterface $query
67
     */
68
    protected function validateQueryAttribute($model, $attribute, QueryInterface $query)
69
    {
70
        $count = $query->count();
71
72
        if ($this->min !== null && $count < $this->min) {
73
            $this->addError($model, $attribute, $this->tooFew, ['min' => $this->min]);
74
        }
75
        if ($this->max !== null && $count > $this->max) {
76
            $this->addError($model, $attribute, $this->tooMany, ['max' => $this->max]);
77
        }
78
        if ($this->count !== null && $count !== $this->count) {
79
            $this->addError($model, $attribute, $this->notEqual, ['count' => $this->count]);
80
        }
81
    }
82
}
83