ModelMutator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
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