Passed
Push — master ( 95067b...1b0a5c )
by Simone
02:01
created

ensureMandatoryPropertyIsValizable()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 2
nop 3
crap 4
1
<?php
2
3
/**
4
 * This file is part of sensorario/resources repository
5
 *
6
 * (c) Simone Gentili <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sensorario\Resources\Validators\Validators;
13
14
use Sensorario\Resources\Resource;
15
use Sensorario\Resources\Validators\Interfaces\Validator;
16
17
final class MandatoryWithoutDefault implements Validator
18
{
19 28
    public function check(Resource $resource)
20
    {
21 28
        foreach ($resource->mandatory() as $key => $value) {
22 15
            $this->ensureMandatoryPropertyIsValizable($key, $value, $resource);
23
        }
24 27
    }
25
26 15
    public function ensureMandatoryPropertyIsValizable($key, $value, $resource)
27
    {
28 15
        if (is_numeric($key) && $resource->hasNotProperty($value) && !isset($resource->defaults()[$value])) {
29 1
            throw new \Sensorario\Resources\Exceptions\PropertyException(
30 1
                "Property `" . get_class($resource) . "::\$$value` is mandatory but not set. "
31 1
                . "Mandatory fields are: " . join(',', $resource->mandatory()) . "."
32
            );
33
        }
34 14
    }
35
}
36