Completed
Push — master ( 533532...ee214c )
by Rafael
04:23
created

DoctrineHelperTrait::findOneByGlobalId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
crap 1
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\Test\Helper;
12
13
use Doctrine\Bundle\DoctrineBundle\Registry;
14
use Doctrine\Common\Persistence\ObjectRepository;
15
use Doctrine\ORM\EntityRepository;
16
use Symfony\Bundle\FrameworkBundle\Client;
17
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry;
18
use Ynlo\GraphQLBundle\Model\ID;
19
20
/**
21
 * @method Client getClient()
22
 */
23
trait DoctrineHelperTrait
24
{
25 14
    public static function getDoctrine(): Registry
26
    {
27 14
        return static::getClient()->getKernel()->getContainer()->get('doctrine');
0 ignored issues
show
Bug Best Practice introduced by
The method Ynlo\GraphQLBundle\Test\...elperTrait::getClient() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        return static::/** @scrutinizer ignore-call */ getClient()->getKernel()->getContainer()->get('doctrine');
Loading history...
28
    }
29
30
    /**
31
     * @param string $class
32
     *
33
     * @return ObjectRepository|EntityRepository
34
     */
35 14
    public static function getRepository(string $class): ObjectRepository
36
    {
37 14
        return static::getDoctrine()->getRepository($class);
38
    }
39
40
    /**
41
     * Find a database record from global id
42
     *
43
     * findOneByGlobalId('VXNlcjox') => object
44
     *
45
     * @param mixed $id
46
     *
47
     * @return mixed
48
     */
49 1
    public static function findOneByGlobalId($id)
50
    {
51 1
        $id = ID::createFromString($id);
52 1
        $databaseId = $id->getDatabaseId();
53 1
        $class = static::getClient()
0 ignored issues
show
Bug Best Practice introduced by
The method Ynlo\GraphQLBundle\Test\...elperTrait::getClient() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        $class = static::/** @scrutinizer ignore-call */ getClient()
Loading history...
54 1
                       ->getContainer()
55 1
                       ->get(DefinitionRegistry::class)
56 1
                       ->getEndpoint()
57 1
                       ->getClassForType($id->getNodeType());
58
59 1
        return static::getRepository($class)->find($databaseId);
60
    }
61
}