Failed Conditions
Pull Request — master (#7085)
by Guilherme
14:34
created

Entity/ReadWriteCachedEntityPersister.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Cache\Persister\Entity;
6
7
use Doctrine\ORM\Cache\ConcurrentRegion;
8
use Doctrine\ORM\Cache\EntityCacheKey;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Doctrine\ORM\Mapping\ClassMetadata;
11
use Doctrine\ORM\Persisters\Entity\EntityPersister;
12
13
/**
14
 * Specific read-write entity persister
15
 */
16
class ReadWriteCachedEntityPersister extends AbstractEntityPersister
17
{
18
    /**
19
     * @param EntityPersister        $persister The entity persister to cache.
20
     * @param ConcurrentRegion       $region    The entity cache region.
21
     * @param EntityManagerInterface $em        The entity manager.
22
     * @param ClassMetadata          $class     The entity metadata.
23
     */
24 31
    public function __construct(EntityPersister $persister, ConcurrentRegion $region, EntityManagerInterface $em, ClassMetadata $class)
25
    {
26 31
        parent::__construct($persister, $region, $em, $class);
27 31
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    public function afterTransactionComplete()
33
    {
34 1
        $isChanged = true;
35
36 1
        if (isset($this->queuedCache['update'])) {
37 1
            foreach ($this->queuedCache['update'] as $item) {
38 1
                $this->region->evict($item['key']);
39
40 1
                $isChanged = true;
41
            }
42
        }
43
44 1
        if (isset($this->queuedCache['delete'])) {
45 1
            foreach ($this->queuedCache['delete'] as $item) {
46 1
                $this->region->evict($item['key']);
47
48 1
                $isChanged = true;
49
            }
50
        }
51
52 1
        if ($isChanged) {
0 ignored issues
show
The condition $isChanged can never be false.
Loading history...
53 1
            $this->timestampRegion->update($this->timestampKey);
54
        }
55
56 1
        $this->queuedCache = [];
57 1
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 3
    public function afterTransactionRolledBack()
63
    {
64 3
        if (isset($this->queuedCache['update'])) {
65 2
            foreach ($this->queuedCache['update'] as $item) {
66 2
                $this->region->evict($item['key']);
67
            }
68
        }
69
70 3
        if (isset($this->queuedCache['delete'])) {
71 2
            foreach ($this->queuedCache['delete'] as $item) {
72 2
                $this->region->evict($item['key']);
73
            }
74
        }
75
76 3
        $this->queuedCache = [];
77 3
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 6
    public function delete($entity)
83
    {
84 6
        $uow     = $this->em->getUnitOfWork();
85 6
        $key     = new EntityCacheKey($this->class->getRootClassName(), $uow->getEntityIdentifier($entity));
86 6
        $lock    = $this->region->lock($key);
87 6
        $deleted = $this->persister->delete($entity);
88
89 6
        if ($deleted) {
90
            $this->region->evict($key);
91
        }
92
93 6
        if ($lock === null) {
94 2
            return $deleted;
95
        }
96
97 4
        $this->queuedCache['delete'][] = [
98 4
            'lock'   => $lock,
99 4
            'key'    => $key,
100
        ];
101
102 4
        return $deleted;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 6
    public function update($entity)
109
    {
110 6
        $uow  = $this->em->getUnitOfWork();
111 6
        $key  = new EntityCacheKey($this->class->getRootClassName(), $uow->getEntityIdentifier($entity));
112 6
        $lock = $this->region->lock($key);
113
114 6
        $this->persister->update($entity);
115
116 6
        if ($lock === null) {
117 2
            return;
118
        }
119
120 4
        $this->queuedCache['update'][] = [
121 4
            'lock'   => $lock,
122 4
            'key'    => $key,
123
        ];
124 4
    }
125
}
126