Passed
Pull Request — master (#84)
by kenny
02:05
created

PhotoAttachmentsValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkHasRatio() 0 14 5
A validate() 0 3 2
1
<?php declare(strict_types=1);
2
3
namespace One\Validator;
4
5
use One\Model\Photo;
6
7
class PhotoAttachmentsValidator extends AbstractValidator implements ValidatorInterface
8
{
9
    /**
10
     * Check if photo has
11
     * certain ratio
12
     * @return self|void
13
     * @throws \Exception
14
     */
15
    public function checkHasRatio(string $ratio, bool $throwException = false)
16
    {
17
        foreach ($this->value as $item) {
18
            if (! empty($item->offsetGet('ratio'))
19
                && $item->offsetGet('ratio') === $ratio
20
            ) {
21
                return $this;
22
            }
23
        }
24
25
        $this->setErrorMessage("Doesn't have vertical which is " . $ratio . ' ratio');
26
27
        if ($throwException) {
28
            throw new \Exception($this->getErrorMessage(), 1);
29
        }
30
    }
31
32
    /**
33
     * @inheritDoc
34
     **/
35
    public function validate(): bool
36
    {
37
        return empty($this->errorMessage) ? true : false;
38
    }
39
}
40