MandatoryWithoutDefault   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 6 2
A ensureMandatoryPropertyIsValizable() 0 9 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 32
    public function check(Resource $resource)
20
    {
21 32
        foreach ($resource->mandatory() as $key => $value) {
22 16
            $this->ensureMandatoryPropertyIsValizable($key, $value, $resource);
23
        }
24 31
    }
25
26 16
    public function ensureMandatoryPropertyIsValizable($key, $value, $resource)
27
    {
28 16
        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 15
    }
35
}
36