Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

LeadGenerationBundle/Service/PopupManager.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 Kunstmaan\LeadGenerationBundle\Service;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\LeadGenerationBundle\Entity\Popup\AbstractPopup;
7
8
class PopupManager
9
{
10
    /**
11
     * @var array|null
12
     */
13
    private $popups = null;
14
15
    /**
16
     * @var EntityManager
17
     */
18
    private $em;
19
20
    /**
21
     * @param EntityManager $em
22
     */
23
    public function __construct(EntityManager $em)
0 ignored issues
show
You have injected the EntityManager via parameter $em. 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...
24
    {
25
        $this->em = $em;
26
    }
27
28
    /**
29
     * Get a list of popup definitions.
30
     *
31
     * @return array
32
     */
33
    public function getPopups()
34
    {
35
        if (is_null($this->popups)) {
36
            $this->popups = $this->em->getRepository('KunstmaanLeadGenerationBundle:Popup\AbstractPopup')->findAll();
37
        }
38
39
        return $this->popups;
40
    }
41
42
    /**
43
     * Get a list of unique javascript files that should be included in the html.
44
     *
45
     * @return array
46
     */
47
    public function getUniqueJsIncludes()
48
    {
49
        $includes = array();
50
        foreach ($this->getPopups() as $popup) {
51
            foreach ($popup->getRules() as $rule) {
52
                $includes[] = $rule->getJsFilePath();
53
            }
54
        }
55
56
        return array_unique($includes);
57
    }
58
59
    /**
60
     * @param AbstractPopup $popup
61
     *
62
     * @return array
63
     */
64
    public function getAvailableRules(AbstractPopup $popup)
65
    {
66
        if (!is_null($popup->getAvailableRules())) {
67
            return $popup->getAvailableRules();
68
        } else {
69
            return array(
70
                'Kunstmaan\LeadGenerationBundle\Entity\Rule\AfterXSecondsRule',
71
                'Kunstmaan\LeadGenerationBundle\Entity\Rule\AfterXScrollPercentRule',
72
                'Kunstmaan\LeadGenerationBundle\Entity\Rule\MaxXTimesRule',
73
                'Kunstmaan\LeadGenerationBundle\Entity\Rule\RecurringEveryXTimeRule',
74
                'Kunstmaan\LeadGenerationBundle\Entity\Rule\UrlBlacklistRule',
75
                'Kunstmaan\LeadGenerationBundle\Entity\Rule\UrlWhitelistRule',
76
                'Kunstmaan\LeadGenerationBundle\Entity\Rule\OnExitIntentRule',
77
            );
78
        }
79
    }
80
}
81