Failed Conditions
Push — 2.7 ( c036c0...266f0d )
by Jonathan
57:23 queued 50:07
created

NonStrictReadWriteCachedCollectionPersister.php (1 issue)

Severity
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license. For more information, see
18
 * <http://www.doctrine-project.org>.
19
 */
20
21
namespace Doctrine\ORM\Cache\Persister\Collection;
22
23
use Doctrine\ORM\Cache\CollectionCacheKey;
24
use Doctrine\ORM\PersistentCollection;
25
26
/**
27
 * @author Fabio B. Silva <[email protected]>
28
 * @since 2.5
29
 */
30
class NonStrictReadWriteCachedCollectionPersister extends AbstractCollectionPersister
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35 66
    public function afterTransactionComplete()
36
    {
37 66
        if (isset($this->queuedCache['update'])) {
38 45
            foreach ($this->queuedCache['update'] as $item) {
39 45
                $this->storeCollectionCache($item['key'], $item['list']);
40
            }
41
        }
42
43 66
        if (isset($this->queuedCache['delete'])) {
44 16
            foreach ($this->queuedCache['delete'] as $key) {
45 16
                $this->region->evict($key);
46
            }
47
        }
48
49 66
        $this->queuedCache = [];
50 66
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    public function afterTransactionRolledBack()
56
    {
57 1
        $this->queuedCache = [];
58 1
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 2
    public function delete(PersistentCollection $collection)
64
    {
65 2
        $ownerId = $this->uow->getEntityIdentifier($collection->getOwner());
66 2
        $key     = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association['fieldName'], $ownerId);
67
68 2
        $this->persister->delete($collection);
69
70 2
        $this->queuedCache['delete'][spl_object_hash($collection)] = $key;
71 2
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 47
    public function update(PersistentCollection $collection)
77
    {
78 47
        $isInitialized = $collection->isInitialized();
79 47
        $isDirty       = $collection->isDirty();
80
81 47
        if ( ! $isInitialized && ! $isDirty) {
82
            return;
83
        }
84
85 47
        $ownerId = $this->uow->getEntityIdentifier($collection->getOwner());
86 47
        $key     = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association['fieldName'], $ownerId);
87
88
       // Invalidate non initialized collections OR ordered collection
89 47
        if ($isDirty && ! $isInitialized || isset($this->association['orderBy'])) {
0 ignored issues
show
Consider adding parentheses for clarity. Current Interpretation: ($isDirty && ! $isInitialized) || IssetNode, Probably Intended Meaning: $isDirty && (! $isInitialized || IssetNode)
Loading history...
90 16
            $this->persister->update($collection);
91
92 16
            $this->queuedCache['delete'][spl_object_hash($collection)] = $key;
93
94 16
            return;
95
        }
96
97 47
        $this->persister->update($collection);
98
99 47
        $this->queuedCache['update'][spl_object_hash($collection)] = [
100 47
            'key'   => $key,
101 47
            'list'  => $collection
102
        ];
103 47
    }
104
}
105