CallableGenerator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateCallable() 0 14 1
A generateCallableSignature() 0 3 1
A __construct() 0 3 1
1
<?php declare(strict_types = 1);
2
3
namespace Actiane\EntityChangeWatchBundle\Generator;
4
5
use Symfony\Component\DependencyInjection\ServiceLocator;
6
7
/**
8
 * Class CallableGenerator
9
 */
10
class CallableGenerator
11
{
12
    private ServiceLocator $serviceLocator;
13
14
    /**
15
     * @param ServiceLocator $serviceContainer
16
     */
17
    public function __construct(ServiceLocator $serviceContainer)
18
    {
19
        $this->serviceLocator = $serviceContainer;
20
    }
21
22
    /**
23
     * Generate the arrays used for call_user_func_array
24
     *
25
     * @param array $arrayCallable
26
     * @param       $entity
27
     *
28
     * @param       $changedProperties
29
     *
30
     * @return array
31
     */
32
    public function generateCallable(array $arrayCallable = [], $entity, $changedProperties = null): array
33
    {
34
        $callable = [];
35
36
        $callable[$this->generateCallableSignature($arrayCallable, $entity)] = [
37
            'callable' => [
38
                $this->serviceLocator->get($arrayCallable['name']),
39
                $arrayCallable['method'],
40
            ],
41
            'parameters' => ['entity' => $entity, 'changedProperties' => $changedProperties],
42
            'flush' => $arrayCallable['flush'],
43
        ];
44
45
        return $callable;
46
    }
47
48
    /**
49
     * Generate the signature of a callback to ensure that each callback for the same entity is only called once
50
     *
51
     * @param array $arrayCallable
52
     * @param       $entity
53
     *
54
     * @return string
55
     */
56
    private function generateCallableSignature(array $arrayCallable = [], $entity): string
57
    {
58
        return $arrayCallable['name'].':'.$arrayCallable['method'].':'.spl_object_hash($entity);
59
    }
60
}
61