Passed
Push — master ( 2ba3ff...f87bcf )
by Dmitry
02:49
created

EmailReceiveCheck::removeOldItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 12
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
crap 2
1
<?php
2
3
namespace TonicHealthCheck\Check\Email\Receive;
4
5
use DateTime;
6
use PhpImap\Mailbox;
7
use TonicHealthCheck\Check\Email\AbstractEmailCheck;
8
use TonicHealthCheck\Check\Email\Entity\EmailSendReceive;
9
use PhpImap\Exception as ImapException;
10
use TonicHealthCheck\Check\Email\Entity\EmailSendReceiveCollection;
11
use TonicHealthCheck\Check\Email\Persist\PersistCollectionInterface;
12
13
/**
14
 * Class EmailReceiveCheck
15
 * @package TonicHealthCheck\Check\Email\Send
16
 */
17
class EmailReceiveCheck extends AbstractEmailCheck
18
{
19
    const CHECK = 'email-receive-check';
20
    const RECEIVE_MAX_TIME = 300;
21
22
    const COLLECTION_KEEP_TIME = 3600;
23
24
    /**
25
     * @var int
26
     */
27
    private $receiveMaxTime;
28
29
    /**
30
     * @var Mailbox;
31
     */
32
    private $mailbox;
33
34
35
    /**
36
     * @var PersistCollectionInterface
37
     */
38
    private $persistCollection;
39
40
    /**
41
     * @var EmailSendReceiveCollection
42
     */
43
    private $emailSendReceiveCollection;
44
45
    /**
46
     * @param string                     $checkNode
47
     * @param Mailbox                    $mailbox
48
     * @param PersistCollectionInterface $persistCollection
49
     * @param int                        $receiveMaxTime
50
     */
51 8
    public function __construct(
52
        $checkNode,
53
        Mailbox $mailbox,
54
        PersistCollectionInterface $persistCollection,
55
        $receiveMaxTime = self::RECEIVE_MAX_TIME
56
    ) {
57 8
        parent::__construct($checkNode);
58
59 8
        $this->setMailbox($mailbox);
60 8
        $this->setPersistCollection($persistCollection);
61 8
        $this->setReceiveMaxTime($receiveMaxTime);
62 8
    }
63
64
    /**
65
     * Check email can send and receive messages
66
     * @return bool|void
67
     * @throws EmailReceiveCheckException
68
     */
69 7
    public function check()
70
    {
71 7
        $this->setEmailSendReceiveColl($this->getPersistCollection()->load());
72
73
        /** @var EmailSendReceive $emailSendCheckI */
74 7
        foreach ($this->getEmailSendReceiveColl()->findAll($this->findEmailForReceiveCheckCallback()) as $emailSendCheckI) {
75 5
            $this->performReceive($emailSendCheckI);
76 4
        }
77 4
        $this->removeOldItems();
78 4
        $this->getPersistCollection()->flush();
79 4
    }
80
81
    /**
82
     * @return Mailbox
83
     */
84 5
    public function getMailbox()
85
    {
86 5
        return $this->mailbox;
87
    }
88
89
    /**
90
     * @return PersistCollectionInterface
91
     */
92 7
    public function getPersistCollection()
93
    {
94 7
        return $this->persistCollection;
95
    }
96
97
    /**
98
     * @return int
99
     */
100 4
    public function getReceiveMaxTime()
101
    {
102 4
        return $this->receiveMaxTime;
103
    }
104
105
    /**
106
     * @return EmailSendReceiveCollection
107
     */
108 7
    public function getEmailSendReceiveColl()
109
    {
110 7
        return $this->emailSendReceiveCollection;
111
    }
112
113
    /**
114
     * @param Mailbox $mailbox
115
     */
116 8
    protected function setMailbox(Mailbox $mailbox)
117
    {
118 8
        $this->mailbox = $mailbox;
119 8
    }
120
121
    /**
122
     * @param PersistCollectionInterface $persistCollection
123
     */
124 8
    protected function setPersistCollection(PersistCollectionInterface $persistCollection)
125
    {
126 8
        $this->persistCollection = $persistCollection;
127 8
    }
128
129
    /**
130
     * @param int $receiveMaxTime
131
     */
132 8
    protected function setReceiveMaxTime($receiveMaxTime)
133
    {
134 8
        $this->receiveMaxTime = $receiveMaxTime;
135 8
    }
136
137
    /**
138
     * @param EmailSendReceiveCollection $emailSendReceiveC
139
     */
140 7
    protected function setEmailSendReceiveColl(EmailSendReceiveCollection $emailSendReceiveC)
141
    {
142 7
        $this->emailSendReceiveCollection = $emailSendReceiveC;
143 7
    }
144
145
    /**
146
     * @param EmailSendReceive $emailSendCheckI
147
     * @throws EmailReceiveCheckException
148
     */
149 2
    protected function timeReceiveCheck(EmailSendReceive $emailSendCheckI)
150
    {
151 2
        $timeLeft = time() - $emailSendCheckI->getSentAt()->getTimestamp();
152 2
        if ($timeLeft > $this->getReceiveMaxTime()) {
153 1
            $emailSendCheckI->setStatus(EmailSendReceive::STATUS_EXPIRED);
154 1
            $emailSendCheckI->setReceivedAt(new DateTime());
155 1
            throw EmailReceiveCheckException::receivingMaxTimeExpire(
156 1
                $emailSendCheckI->getSubject(),
157 1
                $timeLeft,
158 1
                $this->getReceiveMaxTime()
159 1
            );
160
        }
161 2
    }
162
163
    /**
164
     * @param $mails
165
     * @param EmailSendReceive $emailSendCheckI
166
     */
167 2
    private function deleteReceivedEmails($mails, EmailSendReceive $emailSendCheckI)
168
    {
169 2
        foreach ($mails as $mailId) {
170 2
            $this->getMailbox()->deleteMail($mailId);
171 2
        }
172 2
        $emailSendCheckI->setStatus(EmailSendReceive::STATUS_RECEIVED);
173 2
    }
174
175
    /**
176
     * @param EmailSendReceive $emailSendCheckI
177
     * @throws EmailReceiveCheckException
178
     */
179 5
    private function performReceive(EmailSendReceive $emailSendCheckI)
180
    {
181
        try {
182 5
                $mails = $this->getMailbox()->searchMailbox(
183 5
                    'FROM '.$emailSendCheckI->getFrom().' SUBJECT "'.$emailSendCheckI->getSubject().'"'
184 5
                );
185
186 2
                $this->timeReceiveCheck($emailSendCheckI);
187
188 2
                if (count($mails) > 0) {
189 2
                    $this->deleteReceivedEmails($mails, $emailSendCheckI);
190 2
                }
191 5
        } catch (ImapException $e) {
192 2
            $emailSendCheckI->setStatus(EmailSendReceive::STATUS_RECEIVED_ERROR);
193 2
            throw EmailReceiveCheckException::internalProblem($e);
194
        }
195 2
    }
196
197
    /**
198
     * remove old items
199
     */
200 4
    private function removeOldItems()
201
    {
202 4
        $emailSendReceiveOld = $this
203 4
            ->getEmailSendReceiveColl()
204 4
            ->findAll($this->findOldEmailReceiveOldCallback());
205
        /** @var EmailSendReceive $emailSendCheckI */
206 4
        foreach ($emailSendReceiveOld as $emailSendCheckI) {
207 3
            $this->getEmailSendReceiveColl()->remove(
208 3
                $this->findSameItemCallback($emailSendCheckI)
209 3
            );
210 4
        }
211 4
    }
212
213
    /**
214
     * @param EmailSendReceive $emailSendCheckI
215
     * @return \Closure
216
     */
217 3
    private function findSameItemCallback(EmailSendReceive $emailSendCheckI)
218
    {
219
        return function (EmailSendReceive $emailSendCheckItem) use ($emailSendCheckI) {
220 3
            return $emailSendCheckItem === $emailSendCheckI;
221 3
        };
222
    }
223
224
    /**
225
     * @return \Closure
226
     */
227 4
    private function findOldEmailReceiveOldCallback()
228
    {
229
        return function (EmailSendReceive $emailSendCheckItem) {
230 4
            return $emailSendCheckItem->getSentAt()->getTimestamp() + $this->getReceiveMaxTime() + self::COLLECTION_KEEP_TIME - time() >= 0;
231 4
        };
232
    }
233
234
    /**
235
     * @return \Closure
236
     */
237
    private function findEmailForReceiveCheckCallback()
238
    {
239 7
        return function (EmailSendReceive $emailSendCheckItem) {
240 7
            return $emailSendCheckItem->getStatus() == EmailSendReceive::STATUS_SANDED
241 7
            || $emailSendCheckItem->getStatus() == EmailSendReceive::STATUS_RECEIVED_ERROR;
242 7
        };
243
    }
244
}
245