Completed
Push — master ( 0668bb...e0da03 )
by Adrien
01:44
created

EntityIDType::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Doctrine\Definition;
6
7
use Doctrine\ORM\EntityManager;
8
use GraphQL\Doctrine\Utils;
9
use GraphQL\Type\Definition\IDType;
10
11
/**
12
 * A specialized ID type that allows to fetch entity from DB
13
 */
14
class EntityIDType extends IDType
15
{
16
    /**
17
     * @var EntityManager
18
     */
19
    private $entityManager;
20
21
    /**
22
     * The entity class name
23
     *
24
     * @var string
25
     */
26
    private $className;
27
28 9
    public function __construct(EntityManager $entityManager, string $className)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
29
    {
30 9
        $this->entityManager = $entityManager;
31 9
        $this->className = $className;
32 9
        $this->name = Utils::getIDTypeName($className);
33 9
        $this->description = 'Automatically generated type to be used as input where an object of type `' . Utils::getTypeName($className) . '` is needed';
34
35 9
        parent::__construct();
36 9
    }
37
38
    /**
39
     * Serializes an internal value to include in a response.
40
     *
41
     * @param mixed $value
42
     *
43
     * @return string
44
     */
45 1
    public function serialize($value)
46
    {
47 1
        $id = $this->entityManager->getClassMetadata($this->className)->getIdentifierValues($value);
48
49 1
        return (string) reset($id);
50
    }
51
52
    /**
53
     * Parses an externally provided value (query variable) to use as an input
54
     *
55
     * @param mixed $value
56
     *
57
     * @return mixed A Doctrine entity
58
     */
59 2
    public function parseValue($value)
60
    {
61 2
        $value = parent::parseValue($value);
62
63 2
        return $this->createEntityID($value);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createEntityID($value); (GraphQL\Doctrine\Definition\EntityID) is incompatible with the return type of the parent method GraphQL\Type\Definition\IDType::parseValue of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
64
    }
65
66
    /**
67
     * Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input
68
     *
69
     * @param \GraphQL\Language\AST\Node $valueNode
70
     *
71
     * @return mixed
72
     */
73 2
    public function parseLiteral($valueNode)
74
    {
75 2
        $value = parent::parseLiteral($valueNode);
76
77 2
        return $this->createEntityID($value);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createEntityID($value); (GraphQL\Doctrine\Definition\EntityID) is incompatible with the return type of the parent method GraphQL\Type\Definition\IDType::parseLiteral of type null|string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
78
    }
79
80
    /**
81
     * Create EntityID to retrieve entity from DB later on
82
     *
83
     * @return mixed entity
84
     */
85 4
    private function createEntityID(string $id)
86
    {
87 4
        return new EntityID($this->entityManager, $this->className, $id);
88
    }
89
}
90