Failed Conditions
Push — master ( 2ade86...13f838 )
by Jonathan
18s
created

Tests/EventListener/CacheMetadataListener.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Doctrine\Tests\EventListener;
4
5
use Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs;
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\Mapping\ClassMetadata;
8
9
class CacheMetadataListener
10
{
11
12
    /**
13
     * Tracks which entities we have already forced caching enabled on. This is
14
     * important to avoid some potential infinite-recursion issues.
15
     *
16
     * Key is the name of the entity, payload is unimportant.
17
     *
18
     * @var array
19
     */
20
    protected $enabledItems = [];
21
22
    /**
23
     * @param \Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs $event
24
     */
25
    public function loadClassMetadata(LoadClassMetadataEventArgs $event)
26
    {
27
        $metadata = $event->getClassMetadata();
28
        $em = $event->getObjectManager();
29
30
        /** @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */
31
        if (strstr($metadata->name, 'Doctrine\Tests\Models\Cache')) {
32
            return;
33
        }
34
35
        $this->enableCaching($metadata, $em);
0 ignored issues
show
$em of type object<Doctrine\Common\Persistence\ObjectManager> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\ObjectManager to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
36
    }
37
38
    /**
39
     * @param ClassMetadata $metadata
40
     *
41
     * @return bool
42
     */
43
    private function isVisited(ClassMetadata $metadata)
44
    {
45
        return isset($this->enabledItems[$metadata->getName()]);
46
    }
47
48
    /**
49
     * @param ClassMetadata $metadata
50
     */
51
    private function recordVisit(ClassMetadata $metadata)
52
    {
53
        $this->enabledItems[$metadata->getName()] = true;
54
    }
55
56
    /**
57
     * @param ClassMetadata $metadata
58
     * @param EntityManager $em
59
     */
60
    protected function enableCaching(ClassMetadata $metadata, EntityManager $em)
61
    {
62
        if ($this->isVisited($metadata)) {
63
            return; // Already handled in the past
64
        }
65
66
        $cache = [
67
            'usage' => ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE
68
        ];
69
70
        if ($metadata->isVersioned) {
71
            return;
72
        }
73
74
        $metadata->enableCache($cache);
75
76
        $this->recordVisit($metadata);
77
78
        // only enable association-caching when the target has already been
79
        // given caching settings
80
        foreach ($metadata->associationMappings as $mapping) {
81
            $targetMeta = $em->getClassMetadata($mapping['targetEntity']);
82
            $this->enableCaching($targetMeta, $em);
83
84
            if ($this->isVisited($targetMeta)) {
85
                $metadata->enableAssociationCache($mapping['fieldName'], $cache);
86
            }
87
        }
88
    }
89
}
90