Completed
Pull Request — master (#191)
by Serhii
02:32
created

AppExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 46
ccs 12
cts 14
cp 0.8571
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A allTagsAsString() 0 10 2
A allTags() 0 4 1
A getName() 0 4 1
A __construct() 0 4 1
A getFunctions() 0 6 1
1
<?php
2
3
namespace App\Twig;
4
5
use App\Entity\Tag;
6
use Doctrine\ORM\EntityManager;
7
use Twig\TwigFunction;
8
9
class AppExtension extends \Twig_Extension
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Extension has been deprecated with message: since Twig 2.7, use "Twig\Extension\AbstractExtension" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
10
{
11
    /**
12
     * @var EntityManager
13
     */
14
    protected $em;
15
16 41
    public function __construct(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...
17
    {
18 41
        $this->em = $entityManager;
19 41
    }
20
21 23
    public function getFunctions()
22
    {
23
        return array(
24 23
            new TwigFunction('allTagsAsString', array($this, 'allTagsAsString')),
25
        );
26
    }
27
28
    /**
29
     * @return string
30
     */
31 7
    public function allTagsAsString()
32
    {
33 7
        $tagsAsString = '';
34
35 7
        foreach ($this->allTags() as $tag) {
36 7
            $tagsAsString .= sprintf('"%s",', str_replace('"', '\"', $tag->getTitle()));
37
        }
38
39 7
        return trim($tagsAsString, ',');
40
    }
41
42
    /**
43
     * @return Tag[]|array
44
     */
45 7
    protected function allTags()
46
    {
47 7
        return $this->em->getRepository(Tag::class)->findAll();
48
    }
49
50
    public function getName()
51
    {
52
        return 'app_extension';
53
    }
54
}
55