Completed
Push — master ( d05ad0...91c7f9 )
by Rafael
03:53
created

EmailRepository::getEmailQueue()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
rs 8.5806
cc 4
eloc 18
nc 4
nop 1
1
<?php
2
3
namespace Citrax\Bundle\DatabaseSwiftMailerBundle\Entity;
4
5
use Doctrine\ORM\EntityRepository;
6
7
/**
8
 * EmailRepository
9
 *
10
 * This class was generated by the Doctrine ORM. Add your own custom
11
 * repository methods below.
12
 */
13
class EmailRepository extends EntityRepository
14
{
15
    public function addEmail(Email $email)
16
    {
17
        $em = $this->getEntityManager();
18
        $email->setStatus(Email::STATUS_READY);
19
        $email->setRetries(0);
20
        $em->persist($email);
21
        $em->flush();
22
    }
23
24
    public function getAllEmails()
25
    {
26
        $qb = $this->createQueryBuilder('e');
27
28
        $qb->addOrderBy('e.createdAt', 'DESC');
29
        return $qb->getQuery();
30
    }
31
32
    public function getEmailQueue($limit = 100)
33
    {
34
        $qb = $this->createQueryBuilder('e');
35
36
        $qb->where($qb->expr()->eq('e.status', ':status'))->setParameter(':status', Email::STATUS_READY);
37
        $qb->orWhere($qb->expr()->eq('e.status', ':status_1'))->setParameter(':status_1', Email::STATUS_FAILED);
38
        $qb->andWhere($qb->expr()->lt('e.retries', ':retries'))->setParameter(':retries', 10);
39
40
41
        $qb->addOrderBy('e.retries', 'ASC');
42
        $qb->addOrderBy('e.createdAt', 'ASC');
43
        if (empty($limit) === false) {
44
            $qb->setMaxResults($limit);
45
        }
46
47
        $emails = $qb->getQuery()->getResult();
48
        if ($emails->count() > 0) {
49
            $ids = [];
50
            foreach ($emails as $email) {
51
                $ids[] = $email->getId();
52
            }
53
            $query = $this->_em->createQuery("UPDATE CitraxDatabaseSwiftMailerBundle:Email e SET e.status = '" . Email::STATUS_PROCESSING . "' WHERE e.id IN (:ids)");
54
            $query->setParameter(':ids', $ids);
55
            $query->execute();
56
        }
57
58
        return $emails;
59
    }
60
61
    public function markFailedSending(Email $email, \Exception $ex)
62
    {
63
        $email->setErrorMessage($ex->getMessage());
64
        $email->setStatus(Email::STATUS_FAILED);
65
        $email->setRetries($email->getRetries() + 1);
66
        $em = $this->getEntityManager();
67
        $em->persist($email);
68
        $em->flush();
69
    }
70
71
    public function markCompleteSending(Email $email)
72
    {
73
        $email->setStatus(Email::STATUS_COMPLETE);
74
        $email->setSentAt(new \DateTime());
75
        $email->setErrorMessage('');
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a object<Swift_Message>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
        $em = $this->getEntityManager();
77
        $em->persist($email);
78
        $em->flush();
79
    }
80
81
82
}
83