Completed
Push — master ( b64fc1...5651bd )
by Raffael
16:20 queued 08:39
created

Map::map()   C

Complexity

Conditions 12
Paths 6

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 21
cp 0
rs 6.9666
c 0
b 0
f 0
cc 12
nc 6
nop 4
crap 156

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\Workflow;
13
14
use MongoDB\BSON\UTCDateTimeInterface;
15
use Tubee\AttributeMap\AttributeMapInterface;
16
use Tubee\Helper;
17
18
class Map
19
{
20
    /**
21
     * Map.
22
     */
23
    public static function map(AttributeMapInterface $map, array $object, array $mongodb_object, UTCDateTimeInterface $ts): iterable
0 ignored issues
show
Unused Code introduced by
The parameter $ts is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
    {
25
        $object = Helper::associativeArrayToPath($object);
26
        $mongodb_object = Helper::associativeArrayToPath($mongodb_object);
27
28
        foreach ($map->getMap() as $name => $value) {
29
            $name = $value['name'];
30
31
            if ($value['skip'] === true) {
32
                continue;
33
            }
34
35
            $exists = isset($mongodb_object[$name]);
36
            if ($value['ensure'] === WorkflowInterface::ENSURE_EXISTS && $exists === true) {
37
                continue;
38
            }
39
            if (($value['ensure'] === WorkflowInterface::ENSURE_LAST || $value['ensure'] === WorkflowInterface::ENSURE_EXISTS) && isset($object[$name])) {
40
                $mongodb_object[$name] = $object[$name];
41
            } elseif ($value['ensure'] === WorkflowInterface::ENSURE_ABSENT && isset($mongodb_object[$name]) || !isset($object[$name]) && isset($mongodb_object[$name])) {
42
                unset($mongodb_object[$name]);
43
            }
44
        }
45
46
        return Helper::pathArrayToAssociative($mongodb_object);
47
    }
48
}
49