Completed
Push — master ( 09cee9...95067b )
by Simone
01:57
created

RewriteValues::check()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 3
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 RewriteValues implements Validator
18
{
19 25
    public function check(Resource $resource)
20
    {
21 25
        foreach ($resource->properties() as $key => $value) {
22 17
            if (isset($resource->rewrites()[$key])) {
23 1
                $when = $resource->rewrites()[$key]['when']; 
24
25 1
                $this->overwriteValueIfGreaterThan($when, $resource, $key);
26
            }
27
        }
28 25
    }
29
30 1
    public function overwriteValueIfGreaterThan($when, $resource, $propertyToBeReplaced)
31
    {
32 1
        if (isset($when['greater_than']) && $resource->get($propertyToBeReplaced) > $resource->get($when['greater_than'])) {
33 1
            $resource->set(
34 1
                $propertyToBeReplaced,
35 1
                $resource->get($resource->rewrites()[$propertyToBeReplaced]['set']['equals_to'])
36
            );
37
        }
38 1
    }
39
}
40