Passed
Pull Request — master (#9)
by Rafael
03:29
created

FixtureManager::getFixtureGlobalId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
crap 6
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Behat\Fixtures;
12
13
use Doctrine\Common\DataFixtures\ReferenceRepository;
14
use Doctrine\Common\Util\ClassUtils;
15
use Symfony\Component\HttpKernel\KernelInterface;
16
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry;
17
use Ynlo\GraphQLBundle\Model\ID;
18
use Ynlo\GraphQLBundle\Model\NodeInterface;
19
20
class FixtureManager
21
{
22
    /**
23
     * @var KernelInterface
24
     */
25
    protected $kernel;
26
27
    /**
28
     * @var ReferenceRepository
29
     */
30
    protected $repository;
31
32
    /**
33
     * FixtureManager constructor.
34
     *
35
     * @param KernelInterface $kernel
36
     */
37
    public function __construct(KernelInterface $kernel)
38
    {
39
        $this->kernel = $kernel;
40
    }
41
42
    /**
43
     * @return ReferenceRepository
44
     */
45
    public function getRepository(): ReferenceRepository
46
    {
47
        return $this->repository;
48
    }
49
50
    /**
51
     * @param ReferenceRepository $repository
52
     *
53
     * @return FixtureManager
54
     */
55
    public function setRepository(ReferenceRepository $repository): FixtureManager
56
    {
57
        $this->repository = $repository;
58
59
        return $this;
60
    }
61
62
    public function getFixture(string $name)
63
    {
64
        return $this->getRepository()->getReference($name);
65
    }
66
67
    public function getFixtureGlobalId(string $name)
68
    {
69
        $fixture = $this->getFixture($name);
70
        if ($fixture instanceof NodeInterface) {
71
            $nodeType = $this->kernel
72
                ->getContainer()
73
                ->get(DefinitionRegistry::class)
74
                ->getEndpoint()
75
                ->getTypeForClass(ClassUtils::getClass($fixture));
76
77
            $id = $fixture->getId();
78
79
            return ID::encode($nodeType, $id);
80
        }
81
82
        return null;
83
    }
84
}
85