Completed
Push — master ( fa1418...c78b27 )
by Nate
02:34
created

MinMaxValidator::validateQueryAttribute()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 13
cp 0
rs 8.2222
cc 7
eloc 8
nc 8
nop 3
crap 56
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember
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 $this->validateQueryValue($value);
41
        }
42
43
        return parent::validateValue($value);
44
    }
45
46
    /**
47
     * @param QueryInterface $query
48
     * @return array|null the error message and the parameters to be inserted into the error message.
49
     * Null should be returned if the data is valid.
50
     */
51
    protected function validateQueryValue(QueryInterface $query)
52
    {
53
        /** @var QueryInterface $value */
54
        $count = $query->count();
55
56
        if ($this->min !== null && $count < $this->min) {
57
            return [$this->tooFew, ['min' => $this->min]];
58
        }
59
        if ($this->max !== null && $count > $this->max) {
60
            return [$this->tooMany, ['max' => $this->max]];
61
        }
62
        if ($this->count !== null && $count !== $this->count) {
63
            return [$this->notEqual, ['count' => $this->count]];
64
        }
65
66
        return null;
67
    }
68
69
    /**
70
     * @param $model
71
     * @param $attribute
72
     * @param QueryInterface $query
73
     */
74
    protected function validateQueryAttribute($model, $attribute, QueryInterface $query)
75
    {
76
        $count = $query->count();
77
78
        if ($this->min !== null && $count < $this->min) {
79
            $this->addError($model, $attribute, $this->tooFew, ['min' => $this->min]);
80
        }
81
        if ($this->max !== null && $count > $this->max) {
82
            $this->addError($model, $attribute, $this->tooMany, ['max' => $this->max]);
83
        }
84
        if ($this->count !== null && $count !== $this->count) {
85
            $this->addError($model, $attribute, $this->notEqual, ['count' => $this->count]);
86
        }
87
    }
88
}
89