Issues (58)

Services/AzineMailgunService.php (2 issues)

1
<?php
2
3
namespace Azine\MailgunWebhooksBundle\Services;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\ORM\EntityManager;
7
8
/**
9
 * @author Dominik Businger
10
 */
11
class AzineMailgunService
12
{
13
    /**
14
     * @var ManagerRegistry
15
     */
16
    private $managerRegistry;
17
18 3
    public function __construct(ManagerRegistry $managerRegistry)
19
    {
20 3
        $this->managerRegistry = $managerRegistry;
21 3
    }
22
23
    /**
24
     * Deletes all MailgunEvents that are older than the ageLimit.
25
     *
26
     * @param \DateTime $ageLimit
27
     *
28
     * @return int
29
     */
30 1
    public function removeOldEventEntries(\DateTime $ageLimit)
31
    {
32 1
        return $this->removeEvents(null, $ageLimit);
33
    }
34
35
    /**
36
     * Deletes all MailgunEvents of the given type(s) that are older than the ageLimit.
37
     *
38
     * Possible Types are:
39
     *
40
     * Event-Type		Description
41
     * ---------------------------------------------------------------------
42
     * accepted 		Mailgun accepted the request to send/forward the email and the message has been placed in queue.
43
     * rejected 		Mailgun rejected the request to send/forward the email.
44
     * delivered 		Mailgun sent the email and it was accepted by the recipient email server.
45
     * failed 			Mailgun could not deliver the email to the recipient email server.
46
     * opened 			The email recipient opened the email and enabled image viewing. Open tracking must be enabled in the Mailgun control panel, and the CNAME record must be pointing to mailgun.org.
47
     * clicked 			The email recipient clicked on a link in the email. Click tracking must be enabled in the Mailgun control panel, and the CNAME record must be pointing to mailgun.org.
48
     * unsubscribed 	The email recipient clicked on the unsubscribe link. Unsubscribe tracking must be enabled in the Mailgun control panel.
49
     * complained 		The email recipient clicked on the spam complaint button within their email client. Feedback loops enable the notification to be received by Mailgun.
50
     * stored 			Mailgun has stored an incoming message
51
     * dropped			Mailgun has dropped the message due to some error.
52
     *
53
     * @param string|array of string|null $type
54
     * @param \DateTime                   $ageLimit
55
     *
56
     * @return int number of deleted records
57
     */
58 3
    public function removeEvents($type = null, \DateTime $ageLimit)
59
    {
60 3
        $qb = $this->getManager()->createQueryBuilder()->delete("Azine\MailgunWebhooksBundle\Entity\MailgunEvent", 'e')->andWhere('e.timestamp < :age')
61 3
                ->setParameter('age', $ageLimit->getTimestamp());
62
63 3
        if (is_string($type)) {
64 1
            $qb->andWhere('e.event = :type')->setParameter('type', $type);
65 2
        } elseif (is_array($type)) {
66 1
            $qb->andWhere('e.event in (:type)')->setParameter('type', $type);
67
        }
68
69 3
        return $qb->getQuery()->execute();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->getQuery()->execute() also could return the type array which is incompatible with the documented return type integer.
Loading history...
70
    }
71
72
    /**
73
     * Delete all MailgunEvents that match the given criteria (added as "andWhere" clauses).
74
     *
75
     * @param array $criteria
76
     *
77
     * @return \Doctrine\ORM\mixed
78
     */
79
    public function removeEventsBy(array $criteria)
80
    {
81
        $qb = $this->getManager()->createQueryBuilder()
82
            ->delete("Azine\MailgunWebhooksBundle\Entity\MailgunEvent", 'e');
83
        foreach ($criteria as $field => $value) {
84
            if (null == $value) {
85
                $qb->andWhere("e.$field is null");
86
            } else {
87
                $qb->andWhere("e.$field = :$field")
88
                    ->setParameter($field, $value);
89
            }
90
        }
91
92
        return $qb->getQuery()->execute();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->getQuery()->execute() also could return the type array which is incompatible with the documented return type Doctrine\ORM\mixed.
Loading history...
93
    }
94
95
    /**
96
     * @return EntityManager
97
     */
98 3
    private function getManager()
99
    {
100 3
        return $this->managerRegistry->getManager();
101
    }
102
}
103