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

RewriteValues   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
c 1
b 1
f 0
lcom 0
cbo 1
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 10 3
A overwriteValueIfGreaterThan() 0 9 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