getMessageSummaryCount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 1
f 0
1
<?php
2
3
namespace Azine\MailgunWebhooksBundle\Entity\Repositories;
4
5
use Azine\MailgunWebhooksBundle\Entity\MailgunEvent;
6
use Azine\MailgunWebhooksBundle\Entity\MailgunMessageSummary;
7
8
/**
9
 * MailgunMessageSummaryRepository.
10
 *
11
 * This class was generated by the Doctrine ORM. Add your own custom
12
 * repository methods below.
13
 */
14
class MailgunMessageSummaryRepository extends \Doctrine\ORM\EntityRepository
15
{
16
    public function getFieldsToOrderBy()
17
    {
18
        return array(
19
            'lastOpened',
20
            'sendDate',
21
            'firstOpened',
22
            'openCount',
23
            'fromAddress',
24
            'toAddress',
25
            'deliveryStatis',
26
            'subject',
27
            'senderIp',
28
        );
29
    }
30
31
    /**
32
     * Get distinct list of email recipients.
33
     *
34
     * @return array of string
35
     */
36
    public function getRecipients()
37
    {
38
        $q = $this->getEntityManager()->createQueryBuilder()
39
            ->select('m.toAddress as recipient')
40
            ->from($this->getEntityName(), 'm')
41
            ->distinct()
42
            ->orderBy('m.toAddress ', 'asc')
43
            ->getQuery();
44
45
        $result = array();
46
        foreach ($q->execute() as $next) {
47
            $result[] = $next['recipient'];
48
        }
49
50
        return $result;
51
    }
52
53
    /**
54
     * Get distinct list of email senders.
55
     *
56
     * @return array of string
57
     */
58
    public function getSenders()
59
    {
60
        $q = $this->getEntityManager()->createQueryBuilder()
61
            ->select('m.fromAddress as sender')
62
            ->from($this->getEntityName(), 'm')
63
            ->distinct()
64
            ->orderBy('m.fromAddress ', 'asc')
65
            ->getQuery();
66
67
        $result = array();
68
        foreach ($q->execute() as $next) {
69
            $result[] = $next['sender'];
70
        }
71
72
        return $result;
73
    }
74
75
    public function getMessageSummaries($criteria, $orderBy, $limit, $offset)
76
    {
77
        $qb = $this->getMessageSummaryQuery($criteria);
78
79
        $orderField = key($orderBy);
80
        $orderDirection = $orderBy[$orderField];
81
        $qb->orderBy('m.'.$orderField, $orderDirection);
82
        if (-1 != $limit) {
83
            $qb->setMaxResults($limit);
84
            $qb->setFirstResult($offset);
85
        }
86
87
        $result = $qb->getQuery()->execute();
88
89
        return $result;
90
    }
91
92
    public function getMessageSummaryCount($criteria)
93
    {
94
        return $this->getMessageSummaryQuery($criteria)->select('count(m.id)')->getQuery()->getSingleScalarResult();
95
    }
96
97
    private function getMessageSummaryQuery($criteria)
98
    {
99
        $qb = $this->createQueryBuilder('m');
100
        if (array_key_exists('fromAddress', $criteria) && '' != $criteria['fromAddress']) {
101
            $qb->andWhere('m.fromAddress = :fromAddress')
102
                ->setParameter('fromAddress', $criteria['fromAddress']);
103
        }
104
105
        if (array_key_exists('toAddress', $criteria) && '' != $criteria['toAddress']) {
106
            $qb->andWhere('m.toAddress like :toAddress')
107
                ->setParameter('toAddress', '%'.$criteria['toAddress'].'%');
108
        }
109
110
        if (array_key_exists('search', $criteria) && '' != $criteria['search']) {
111
            $qb->andWhere('(m.toAddress like :search OR m.subject like :search OR m.fromAddress like :search )')
112
                ->setParameter('search', '%'.$criteria['search'].'%');
113
        }
114
115
        return $qb;
116
    }
117
118
    public function findSummary($fromAddress, $toAddresses, $sendTime, $subject)
0 ignored issues
show
Unused Code introduced by
The parameter $sendTime is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

118
    public function findSummary($fromAddress, $toAddresses, /** @scrutinizer ignore-unused */ $sendTime, $subject)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
    {
120
        // extract email-address part
121
        $from = mailparse_rfc822_parse_addresses($fromAddress)[0];
122
123
        $qb = $this->createQueryBuilder('m');
124
        $qb->where('m.sendDate < :higherBound AND m.sendDate > :lowerBound AND m.fromAddress like :fromAddress')
125
            ->setParameters('lowerBound', $lowerBound)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $lowerBound seems to be never defined.
Loading history...
Unused Code introduced by
The call to Doctrine\ORM\QueryBuilder::setParameters() has too many arguments starting with $lowerBound. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
            ->/** @scrutinizer ignore-call */ setParameters('lowerBound', $lowerBound)

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
'lowerBound' of type string is incompatible with the type Doctrine\Common\Collections\ArrayCollection|array expected by parameter $parameters of Doctrine\ORM\QueryBuilder::setParameters(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
            ->setParameters(/** @scrutinizer ignore-type */ 'lowerBound', $lowerBound)
Loading history...
126
            ->setParameters('higherBound', $higherBound)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $higherBound seems to be never defined.
Loading history...
127
            ->setParameter('fromAddress', $from);
128
129
        // extract email-address parts
130
        $to = '';
131
        foreach (mailparse_rfc822_parse_addresses($toAddresses) as $nextMatch) {
132
            $to += "'".$nextMatch['address']."',";
133
        }
134
        $to = '('.trim($to, ', ').')';
135
136
        if (strlen($to) > 0) {
137
            $qb->andWhere('m.toAddress in :to')->setParameters('to', $to);
138
        }
139
140
        if (strlen($to) > 0) {
141
            $qb->andWhere('m.subject like :subject')->setParameters('subject', $subject);
142
        }
143
    }
144
145
    public function createOrUpdateMessageSummary(MailgunEvent $event)
146
    {
147
        $messageSummary = $this->findOneBy(array('id' => $event->getMessageId()));
148
        if (null == $messageSummary) {
149
            $ip = null != $event->getIp() ? $event->getIp() : 'unknown';
150
            $messageSummary = new MailgunMessageSummary($event->getMessageId(), $event->getDateTime(), $event->getSender(), $event->getRecipient(), 'no subject found yet', $ip);
151
        }
152
        $event->setEventSummary($messageSummary);
153
        $messageSummary->updateDeliveryStatus($event->getEvent());
154
155
        if ('opened' == $event->getEvent()) {
156
            if (null == $messageSummary->getFirstOpened() || $messageSummary->getFirstOpened() > $event->getDateTime()) {
157
                $messageSummary->setFirstOpened($event->getDateTime());
158
            }
159
            if (null == $messageSummary->getLastOpened() || $messageSummary->getLastOpened() < $event->getDateTime()) {
160
                $messageSummary->setLastOpened($event->getDateTime());
161
            }
162
            $messageSummary->increaseOpenCount();
163
        }
164
165
        foreach ($event->getMessageHeaders() as $header) {
166
            if ('X-Mailgun-Sending-Ip' == $header[0]) {
167
                $messageSummary->setSenderIp($header[1]);
168
            } elseif ('Subject' == $header[0]) {
169
                $messageSummary->setSubject($header[1]);
170
            } elseif ('Sender' == $header[0]) {
171
                $messageSummary->setFromAddress($header[1]);
172
            } elseif ('To' == $header[0]) {
173
                $messageSummary->appendToToAddress($header[1]);
174
            } elseif ('Cc' == $header[0]) {
175
                $messageSummary->appendToToAddress($header[1]);
176
            } elseif ('Bcc' == $header[0]) {
177
                $messageSummary->appendToToAddress($header[1]);
178
            }
179
        }
180
181
        $manager = $this->getEntityManager();
182
        $manager->persist($messageSummary);
183
184
        return $messageSummary;
185
    }
186
}
187