Failed Conditions
Push — master ( a3e53b...559253 )
by Guilherme
14:58
created

Tests/EventListener/CacheMetadataListener.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\EventListener;
6
7
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
8
use Doctrine\ORM\Mapping\AssociationMetadata;
9
use Doctrine\ORM\Mapping\CacheMetadata;
10
use Doctrine\ORM\Mapping\CacheUsage;
11
use Doctrine\ORM\Mapping\ClassMetadata;
12
use function sprintf;
13
use function str_replace;
14
use function strstr;
15
use function strtolower;
16
17
class CacheMetadataListener
18
{
19
    /**
20
     * Tracks which entities we have already forced caching enabled on. This is
21
     * important to avoid some potential infinite-recursion issues.
22
     *
23
     * Key is the name of the entity, payload is unimportant.
24
     *
25
     * @var array
26
     */
27
    protected $enabledItems = [];
28
29
    public function loadClassMetadata(LoadClassMetadataEventArgs $event)
30
    {
31
        $metadata = $event->getClassMetadata();
32
        $em       = $event->getEntityManager();
33
34
        /** @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */
35
        if (strstr($metadata->getClassName(), 'Doctrine\Tests\Models\Cache')) {
36
            return;
37
        }
38
39
        $this->enableCaching($metadata, $em);
40
    }
41
42
    /**
43
     * @return bool
44
     */
45
    private function isVisited(ClassMetadata $metadata)
46
    {
47
        return isset($this->enabledItems[$metadata->getClassName()]);
48
    }
49
50
    private function recordVisit(ClassMetadata $metadata)
51
    {
52
        $this->enabledItems[$metadata->getClassName()] = true;
53
    }
54
55
    protected function enableCaching(ClassMetadata $metadata, EntityManagerInterface $em)
0 ignored issues
show
The type Doctrine\Tests\EventList...\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
    {
57
        if ($this->isVisited($metadata)) {
58
            return; // Already handled in the past
59
        }
60
61
        if ($metadata->isVersioned()) {
62
            return;
63
        }
64
65
        $region        = strtolower(str_replace('\\', '_', $metadata->getRootClassName()));
66
        $cacheMetadata = new CacheMetadata(CacheUsage::NONSTRICT_READ_WRITE, $region);
67
68
        $metadata->setCache($cacheMetadata);
69
70
        $this->recordVisit($metadata);
71
72
        // only enable association-caching when the target has already been
73
        // given caching settings
74
        foreach ($metadata->getPropertiesIterator() as $property) {
75
            if (! ($property instanceof AssociationMetadata)) {
76
                continue;
77
            }
78
79
            $targetMetadata = $em->getClassMetadata($property->getTargetEntity());
80
81
            $this->enableCaching($targetMetadata, $em);
82
83
            if ($this->isVisited($targetMetadata)) {
84
                $region        = sprintf('%s__%s', $region, $property->getName());
85
                $cacheMetadata =  new CacheMetadata(CacheUsage::NONSTRICT_READ_WRITE, $region);
86
87
                $property->setCache($cacheMetadata);
88
            }
89
        }
90
    }
91
}
92