Completed
Push — master ( 463d73...47ba96 )
by Gerrit
02:04
created

EntityArgumentFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 55
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A understandsString() 0 4 1
A understandsArray() 0 4 2
A createArgumentFromString() 0 11 1
A createArgumentFromArray() 0 10 1
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 Doctrine\Common\Persistence\ObjectManager;
19
use Webmozart\Assert\Assert;
20
21
final class EntityArgumentFactory implements ArgumentFactory
22
{
23
24
    /**
25
     * @var ObjectManager
26
     */
27
    private $objectManager;
28
29
    /**
30
     * @var ArgumentFactory
31
     */
32
    private $argumentFactory;
33
34
    public function __construct(
35
        ObjectManager $objectManager,
36
        ArgumentFactory $argumentFactory
37
    ) {
38
        $this->objectManager = $objectManager;
39
        $this->argumentFactory = $argumentFactory;
40
    }
41
42
    public function understandsString(string $source): bool
43
    {
44
        return strpos($source, '#') > 0;
45
    }
46
47
    public function understandsArray(array $source): bool
48
    {
49
        return isset($source['entity-class']) && isset($source['entity-id']);
50
    }
51
52
    public function createArgumentFromString(string $source): Argument
53
    {
54
        Assert::contains($source, '#');
55
56
        [$entityClass, $idSource] = explode('#', $source);
0 ignored issues
show
Bug introduced by
The variable $entityClass does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $idSource does not exist. Did you mean $source?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
57
58
        /** @var Argument $id */
59
        $id = $this->argumentFactory->createArgumentFromString($idSource);
0 ignored issues
show
Bug introduced by
The variable $idSource does not exist. Did you mean $source?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
60
61
        return new EntityArgument($this->objectManager, $entityClass, $id);
62
    }
63
64
    public function createArgumentFromArray(array $source): Argument
65
    {
66
        Assert::keyExists($source, 'entity-class');
67
        Assert::keyExists($source, 'entity-id');
68
69
        /** @var Argument $id */
70
        $id = $this->argumentFactory->createArgumentFromString($source['entity-id']);
71
72
        return new EntityArgument($this->objectManager, $source['entity-class'], $id);
73
    }
74
75
}
76