Test Setup Failed
Push — master ( e2ccdd...da0315 )
by Gerrit
09:14
created

EntityArgumentFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 55
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\SymfonyGenerics\Arguments\ArgumentFactory;
14
15
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory;
16
use Addiks\SymfonyGenerics\Arguments\Argument;
17
use Addiks\SymfonyGenerics\Arguments\EntityArgument;
18
use Webmozart\Assert\Assert;
19
use Doctrine\Persistence\ObjectManager;
20
21
final class EntityArgumentFactory implements ArgumentFactory
22
{
23
24
    private ObjectManager $objectManager;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
25
26
    private ArgumentFactory $argumentFactory;
27
28
    public function __construct(
29
        ObjectManager $objectManager,
30
        ArgumentFactory $argumentFactory
31
    ) {
32
        $this->objectManager = $objectManager;
33
        $this->argumentFactory = $argumentFactory;
34 15
    }
35
36
    public function understandsString(string $source): bool
37
    {
38 15
        return 1 === preg_match('/^[a-zA-Z0-9_\\\\]+\\#.+/is', $source);
39 15
    }
40 15
41
    public function understandsArray(array $source): bool
42 8
    {
43
        return isset($source['entity-class']) && isset($source['entity-id']);
44 8
    }
45
46
    public function createArgumentFromString(string $source): Argument
47 3
    {
48
        Assert::true($this->understandsString($source));
49 3
50
        /** @var class-string $entityClass */
51
        [$entityClass, $idSource] = explode('#', $source);
52 4
53
        /** @var Argument $id */
54 4
        $id = $this->argumentFactory->createArgumentFromString($idSource);
55
56 1
        return new EntityArgument($this->objectManager, $entityClass, $id);
57
    }
58
59 1
    public function createArgumentFromArray(array $source): Argument
60
    {
61 1
        Assert::keyExists($source, 'entity-class');
62
        Assert::keyExists($source, 'entity-id');
63
64 4
        /** @var class-string $entityClass */
65
        $entityClass = $source['entity-class'];
66 4
67 2
        /** @var Argument $id */
68
        $id = $this->argumentFactory->createArgumentFromString($source['entity-id']);
69
70 1
        return new EntityArgument($this->objectManager, $entityClass, $id);
71
    }
72 1
73
}
74