Completed
Push — master ( f24189...ede2db )
by Samuel
26:59 queued 11:50
created

Uuid4Behaviour   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 25
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A beforeInsert() 0 7 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleMapper\Behaviour;
6
7
use Ramsey\Uuid\Uuid;
8
9
class Uuid4Behaviour extends AbstractBehaviour
10
{
11
    /** @var string */
12
    private $field;
13
14
    /**
15
     * @param string $field
16
     */
17
    public function __construct(string $field = 'id')
18
    {
19
        $this->field = $field;
20
    }
21
22
    /**
23
     * @param array $data
24
     * @return array
25
     */
26
    public function beforeInsert(array $data): array
27
    {
28
        if (!array_key_exists($this->field, $data) || !$data[$this->field]) {
29
            $data[$this->field] = Uuid::uuid4();
30
        }
31
        return $data;
32
    }
33
}
34