Completed
Push — master ( 922897...9fac8c )
by Ryota
08:39
created

DoctrineStorage::removeForce()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
namespace Tavii\SQSJobQueueBundle\Storage;
3
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityRepository;
7
use Tavii\SQSJobQueue\Queue\QueueName;
8
use Tavii\SQSJobQueue\Storage\EntityInterface;
9
use Tavii\SQSJobQueue\Storage\StorageInterface;
10
use Tavii\SQSJobQueueBundle\Entity\SqsWorker;
11
12
13
class DoctrineStorage implements StorageInterface
14
{
15
    /**
16
     * @var EntityManager
17
     */
18
    private $entityManager;
19
20
    /**
21
     * @var EntityRepository
22
     */
23
    private $repository;
24
25
    /**
26
     * @param EntityManager $em
27
     * @param EntityRepository $repository
28
     */
29
    public function __construct(
30
        EntityManager $em,
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. 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...
31
        EntityRepository $repository
32
    )
33
    {
34
        $this->entityManager = $em;
35
        $this->repository = $repository;
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    public function all()
42
    {
43
        return $this->repository->findAll();
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function find(QueueName $queueName, $server = null, $procId = null)
50
    {
51
        return $this->repository->findBy($this->createCriteria($queueName, $server, $procId));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->repository...me, $server, $procId)); (array) is incompatible with the return type declared by the interface Tavii\SQSJobQueue\Storage\StorageInterface::find of type Tavii\SQSJobQueue\Storage\EntityInterface.

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...
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function set(QueueName $queueName, $server, $procId)
58
    {
59
        $entity = new SqsWorker();
60
        $entity->setQueueName($queueName)
61
            ->setServer($server)
62
            ->setProcId($procId)
63
        ;
64
        $this->entityManager->persist($entity);
65
        $this->entityManager->flush();
66
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function get(QueueName $queueName, $server, $procId)
73
    {
74
        return $this->repository->findOneBy($this->createCriteria($queueName, $server, $procId));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->repository->findO...me, $server, $procId)); of type object|null adds the type object to the return on line 74 which is incompatible with the return type declared by the interface Tavii\SQSJobQueue\Storage\StorageInterface::get of type array.
Loading history...
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function remove(QueueName $queueName, $server, $procId)
81
    {
82
        $entity = $this->get($queueName, $server, $procId);
83
        if ($entity instanceof EntityInterface) {
84
            $this->entityManager->remove($entity);
85
            $this->entityManager->flush();
86
        }
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function removeForce(QueueName $queueName, $server)
93
    {
94
        /** @var SqsWorker $entity */
95
        foreach ($this->find($queueName, $server) as $entity) {
96
            $this->entityManager->remove($entity);
97
            $this->entityManager->flush();
98
        }
99
    }
100
101
102
    /**
103
     * @param QueueName $queueName
104
     * @param string|null $server
105
     * @param string|null $procId
106
     * @return array
107
     */
108
    protected function createCriteria(QueueName $queueName, $server = null, $procId = null)
109
    {
110
        $criteria = array(
111
            'queue' => $queueName->getName(),
112
        );
113
114
        if (!empty($queueName->getPrefix())) {
115
            $criteria['prefix'] = $queueName->getPrefix();
116
        }
117
118
        if ($server !== null) {
119
            $criteria['server'] = $server;
120
        }
121
122
        if ($procId !== null) {
123
            $criteria['procId'] = $procId;
124
        }
125
126
        return $criteria;
127
    }
128
129
}