Completed
Push — master ( d1535f...93b502 )
by Leny
06:44
created

ViewCacheWarmer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 3
c 2
b 1
f 0
lcom 1
cbo 3
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A warmUp() 0 10 2
1
<?php
2
3
namespace Victoire\Bundle\ViewReferenceBundle\Cache;
4
5
use Doctrine\ORM\EntityManager;
6
use Victoire\Bundle\CoreBundle\Helper\ViewHelper;
7
use Victoire\Bundle\ViewReferenceBundle\Cache\Xml\ViewReferenceXmlCacheDriver;
8
use Victoire\Bundle\ViewReferenceBundle\Cache\Xml\ViewReferenceXmlCacheManager;
9
10
/**
11
 * Called (for example on kernel request) to create the viewsReference cache file
12
 * ref. victoire_view_reference.cache_warmer.
13
 */
14
class ViewCacheWarmer
15
{
16
    private $viewHelper;
17
    private $viewCacheDriver;
18
    private $viewCacheManager;
19
20
    /**
21
     * @param ViewHelper                   $viewHelper       @victoire_page.page_helper
22
     * @param ViewReferenceXmlCacheDriver  $viewCacheDriver  @victoire_view_reference.cache.driver
23
     * @param ViewReferenceXmlCacheManager $viewCacheManager @victoire_view_reference.cache.manager
24
     */
25
    public function __construct(
26
        ViewHelper $viewHelper,
27
        ViewReferenceXmlCacheDriver $viewCacheDriver,
28
        ViewReferenceXmlCacheManager $viewCacheManager,
29
        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...
30
    ) {
31
        $this->viewHelper = $viewHelper;
32
        $this->viewCacheDriver = $viewCacheDriver;
33
        $this->viewCacheManager = $viewCacheManager;
34
        $this->entityManager = $entityManager;
0 ignored issues
show
Bug introduced by
The property entityManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
    }
36
37
    /**
38
     * Warm the view cache file (if needed or force mode).
39
     *
40
     * @param string $cacheDir Where does the viewsReferences file should take place
41
     */
42
    public function warmUp($cacheDir)
0 ignored issues
show
Unused Code introduced by
The parameter $cacheDir is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
    {
44
        if (!$this->viewCacheDriver->fileExists()) {
45
            $this->viewCacheDriver->writeFile(
46
                $this->viewCacheManager->generateXml(
47
                    $this->viewHelper->buildViewsReferences()
48
                )
49
            );
50
        }
51
    }
52
}
53