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

Receiver   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 88.1%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 42
c 1
b 0
f 0
dl 0
loc 83
rs 10
ccs 37
cts 42
cp 0.881

6 Methods

Rating   Name   Duplication   Size   Complexity  
A _getMails() 0 18 5
A getNow() 0 5 2
A getMails() 0 12 4
A getSearchCriteria() 0 6 2
A createMailFromImap() 0 15 2
A setSearchCriteria() 0 2 1
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
}