DoctrineTargetResolver   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 64
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A factoryFindOneBy() 0 6 1
A factoryCall() 0 10 3
A __construct() 0 3 1
A resolve() 0 25 5
A factoryFind() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Darkilliant\ImportBundle\TargetResolver;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
9
/**
10
 * @internal
11
 */
12
class DoctrineTargetResolver
13
{
14
    /** @var EntityManagerInterface */
15
    private $em;
16
17 4
    public function __construct(EntityManagerInterface $em)
18
    {
19 4
        $this->em = $em;
20 4
    }
21
22 4
    public function resolve(array $config)
23
    {
24 4
        $repository = $this->em->getRepository($config['entityClass']);
25 4
        $call = $this->factoryCall($config['strategy']['name'], $config['entityClass'], $config['strategy']['options']);
26
27
        // When not strategy found
28 4
        if (null === $call) {
29 1
            throw new \Exception('factory fail');
30
        }
31
32
        // When method of repository or entityManager with params
33 3
        $entity = call_user_func_array(
34
            [
35 3
                ('repository' === $call['in'])
36 2
                    ? $repository
37 3
                    : $this->em, $call['method'], ],
38 3
            $call['params']
39
        );
40
41
        // When not entity already exist in database
42 3
        if (null === $entity) {
43 1
            return ($config['create']) ? new $config['entityClass']() : null;
44
        }
45
46 2
        return $entity;
47
    }
48
49 4
    private function factoryCall(string $strategy, string $entityClass, array $options)
50
    {
51
        switch ($strategy) {
52 4
            case 'findOneBy':
53 2
                return $this->factoryFindOneBy($options);
54 2
            case 'find':
55 1
                return $this->factoryFind($entityClass, $options);
56
        }
57
58 1
        return null;
59
    }
60
61 2
    private function factoryFindOneBy(array $options)
62
    {
63
        return [
64 2
            'in' => 'repository',
65 2
            'method' => 'findOneBy',
66 2
            'params' => [$options],
67
        ];
68
    }
69
70 1
    private function factoryFind(string $entityClass, array $options)
71
    {
72
        return [
73 1
            'in' => 'entity_manager',
74 1
            'method' => 'getReference',
75 1
            'params' => [$entityClass, $options[0]],
76
        ];
77
    }
78
}
79