Passed
Push — master ( 8294c3...31b270 )
by eXeCUT
03:49
created

Receiver::createMailFromImap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 15
ccs 12
cts 12
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: execut
5
 * Date: 9/27/16
6
 * Time: 2:26 PM
7
 */
8
9
namespace execut\import\components\source\adapter\email;
10
11
12
use execut\import\components\source\File;
13
use roopz\imap\Imap;
14
use roopz\imap\IncomingMail;
15
use yii\base\Component;
16
17
class Receiver extends Component
18
{
19
    public $filter = null;
20
    /**
21
     * @var Imap
22
     */
23
    public $imap = null;
24
    public $now = null;
25
    public $cache = null;
26
    protected $searchCriteria = null;
27
    protected $mails;
28
29
    public function setSearchCriteria($criteria) {
30
        $this->searchCriteria = $criteria;
31
    }
32
33 1
    public function getSearchCriteria() {
34 1
        if ($this->searchCriteria === null) {
35 1
            return 'UNSEEN SINCE "' . $this->getNow() . '"';
36
        }
37
38
        return $this->searchCriteria;
39
    }
40
41 1
    protected function getNow() {
42 1
        if ($this->now !== null) {
43 1
            return $this->now;
44
        }
45
        return date('d F Y', time() - 3600 * 24 * 10);
46
    }
47
48 1
    protected function _getMails() {
49 1
        if ($this->cache !== null) {
50 1
            if ($mails = $this->cache->get()) {
51 1
                return $mails;
52
            }
53
        }
54
55 1
        $mailsIds = $this->imap->searchMailBox($this->getSearchCriteria());
56 1
        $mails = [];
57 1
        foreach ($mailsIds as $id) {
58 1
            $mails[] = self::createMailFromImap($this->imap->getMail($id, false));
59
        }
60
61 1
        if ($this->cache !== null) {
62 1
            $this->cache->set($mails);
63
        }
64
65 1
        return $this->mails = $mails;
66
    }
67
68
    /**
69
     * @return Mail[];
70
     */
71 3
    public function getMails() {
72 3
        $mails = $this->_getMails();
73 3
        if ($this->filter) {
74 1
            $mails = $this->filter->filtrate($mails);
75 1
            if ($this->imap !== null) {
76 1
                foreach ($mails as $mail) {
77 1
                    $this->imap->markMailAsRead($mail->id);
78
                }
79
            }
80
        }
81
82 3
        return $mails;
83
    }
84
85 2
    public static function createMailFromImap(IncomingMail $imapMail) {
86 2
        $attachments = [];
87 2
        foreach ($imapMail->getAttachments() as $imapAttachment) {
88 1
            $attachment = new File([
89 1
                'fileName' => $imapAttachment->name,
90 1
                'filePath' => $imapAttachment->filePath,
91
            ]);
92 1
            $attachments[] = $attachment;
93
        }
94
95 2
        return new Mail([
96 2
            'id' => $imapMail->id,
97 2
            'sender' => $imapMail->fromAddress,
98 2
            'subject' => $imapMail->subject,
99 2
            'attachments' => $attachments,
100
        ]);
101
    }
102
}