ModelMutator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 26
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A mutate() 0 17 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Blackmine\Mutator;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
9
class ModelMutator
10
{
11
    protected ArrayCollection $mutations;
12
13
    public function __construct(protected MutableInterface $model)
14
    {
15
        $this->mutations = new ArrayCollection($this->model->getMutations());
16
    }
17
18
    public function mutate(): ?array
19
    {
20
        if ($this->model->isMutable()) {
21
            $payload = $this->model->toArray();
22
            foreach ($this->mutations as $key => $mutations) {
23
                foreach ($mutations as $mutation_class => $mutation_args) {
24
                    $mutation = new $mutation_class();
25
                    array_unshift($mutation_args, $key);
26
27
                    $payload = $mutation($payload, $mutation_args);
28
                }
29
            }
30
31
            return $payload;
32
        }
33
34
        return null;
35
    }
36
}
37