NotificationLog::save()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Monitor\Service;
3
4
use Monitor\Model\NotificationLog as NotificationLogModel;
5
use Doctrine\ORM\EntityManager;
6
7
class NotificationLog
8
{
9
10
    /**
11
     * @var \Doctrine\ORM\EntityManager
12
     */
13
    private $em;
14
    /**
15
     * @var integer
16
     */
17
    private $notificationDelayInHours;
18
19
    public function __construct(EntityManager $em, $notificationDelayInHours)
0 ignored issues
show
Bug introduced by
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...
20
    {
21
        $this->em = $em;
22
        $this->notificationDelayInHours = $notificationDelayInHours;
23
    }
24
25
    /**
26
     * Save NotificationLog
27
     *
28
     * @param \Monitor\Model\NotificationLog $notificationLog
29
     */
30
    public function save(NotificationLogModel $notificationLog)
31
    {
32
        $this->em->persist($notificationLog);
33
        $this->em->flush();
34
    }
35
36
    /**
37
     * Get last fired up specific type of trigger for concret server
38
     *
39
     * @param int $triggerId
40
     * @param int $serverId
41
     */
42
    public function getLastForTrigger($triggerId, $serverId)
43
    {
44
        $queryBuilder = $this->em->createQueryBuilder();
45
        $queryBuilder->select('nl.created')
46
            ->from('Monitor\Model\NotificationLog', 'nl')
47
            ->where('nl.trigger_id = ?1')
48
            ->andWhere('nl.server_id = ?2')
49
            ->orderBy('nl.created', 'DESC')
50
            ->setMaxResults(1)
51
            ->setParameters(
52
                [
53
                    '1' => $triggerId,
54
                    '2' => $serverId
55
                ]
56
            );
57
        $query = $queryBuilder->getQuery();
58
        $queryResult = $query->getResult();
59
        return $queryResult;
60
    }
61
    /**
62
     * Check if same type of notification for concret server has been sent already
63
     *
64
     * @access private
65
     * @param  int $triggerId
66
     * @param  int $serverId
67
     * @param  int $msDelay
68
     * @return boolean
69
     */
70
    public function hasNotificationDelayExpired($triggerId, $serverId, $msDelay)
71
    {
72
        $queryResult = $this->getLastForTrigger(
73
            $triggerId,
74
            $serverId
75
        );
76
        if (! $queryResult) {
77
            return true;
78
        }
79
        $timeOfLastFiredUpTrigger = $queryResult[0]['created'];
80
        $timeDiff = $timeOfLastFiredUpTrigger - time();
81
        return ($this->notificationDelayInHours * $msDelay + $timeDiff >= 0) ? false : true;
82
    }
83
}
84