CacheCleaner   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 53
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addCacheStorage() 0 4 1
A invalidate() 0 20 3
1
<?php
2
3
/**
4
 * This file is part of the Kdyby (http://www.kdyby.org)
5
 *
6
 * Copyright (c) 2008 Filip Procházka ([email protected])
7
 *
8
 * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
9
 */
10
11
namespace Kdyby\Doctrine\Tools;
12
13
use Doctrine;
14
use Doctrine\Common\Cache\CacheProvider;
15
use Doctrine\Common\Cache\ClearableCache;
16
use Kdyby;
17
use Nette;
18
19
20
21
/**
22
 * @author Filip Procházka <[email protected]>
23
 */
24
class CacheCleaner
25
{
26
27
	use \Kdyby\StrictObjects\Scream;
28
29
	/**
30
	 * @var \Doctrine\ORM\EntityManager
31
	 */
32
	private $entityManager;
33
34
	/**
35
	 * @var (ClearableCache|Doctrine\Common\Cache\Cache|null)[]
36
	 */
37
	private $cacheStorages = [];
38
39
40
41
	public function __construct(Doctrine\ORM\EntityManager $entityManager)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. 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...
42
	{
43
		$this->entityManager = $entityManager;
44
	}
45
46
47
48
	public function addCacheStorage(ClearableCache $storage)
49
	{
50
		$this->cacheStorages[] = $storage;
51
	}
52
53
54
55
	public function invalidate()
56
	{
57
		$ormConfig = $this->entityManager->getConfiguration();
58
		$dbalConfig = $this->entityManager->getConnection()->getConfiguration();
59
60
		$cache = $this->cacheStorages;
61
		$cache[] = $ormConfig->getHydrationCacheImpl();
62
		$cache[] = $ormConfig->getMetadataCacheImpl();
63
		$cache[] = $ormConfig->getQueryCacheImpl();
64
		$cache[] = $ormConfig->getResultCacheImpl();
65
		$cache[] = $dbalConfig->getResultCacheImpl();
66
67
		foreach ($cache as $impl) {
68
			if (!$impl instanceof ClearableCache) {
69
				continue;
70
			}
71
72
			$impl->deleteAll();
73
		}
74
	}
75
76
}
77