Receiver   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 87.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 38
c 1
b 0
f 0
dl 0
loc 76
rs 10
ccs 34
cts 39
cp 0.8718

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setSearchCriteria() 0 2 1
A getNow() 0 5 2
A getMails() 0 12 4
A getSearchCriteria() 0 6 2
A createMailFromImap() 0 15 2
A _getMails() 0 12 3
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
    protected $searchCriteria = null;
26
    protected $mails;
27
28
    public function setSearchCriteria($criteria) {
29
        $this->searchCriteria = $criteria;
30
    }
31
32 1
    public function getSearchCriteria() {
33 1
        if ($this->searchCriteria === null) {
34 1
            return 'UNSEEN SINCE "' . $this->getNow() . '"';
35
        }
36
37
        return $this->searchCriteria;
38
    }
39
40 1
    protected function getNow() {
41 1
        if ($this->now !== null) {
42 1
            return $this->now;
43
        }
44
        return date('d F Y', time() - 3600 * 24 * 10);
45
    }
46
47 1
    protected function _getMails() {
48 1
        if ($this->mails !== null) {
49 1
            return $this->mails;
50
        }
51
52 1
        $mailsIds = $this->imap->searchMailBox($this->getSearchCriteria());
53 1
        $mails = [];
54 1
        foreach ($mailsIds as $id) {
55 1
            $mails[] = self::createMailFromImap($this->imap->getMail($id, false));
56
        }
57
58 1
        return $this->mails = $mails;
59
    }
60
61
    /**
62
     * @return Mail[];
63
     */
64 3
    public function getMails() {
65 3
        $mails = $this->_getMails();
66 3
        if ($this->filter) {
67 1
            $mails = $this->filter->filtrate($mails);
68 1
            if ($this->imap !== null) {
69 1
                foreach ($mails as $mail) {
70 1
                    $this->imap->markMailAsRead($mail->id);
71
                }
72
            }
73
        }
74
75 3
        return $mails;
76
    }
77
78 2
    public static function createMailFromImap(IncomingMail $imapMail) {
79 2
        $attachments = [];
80 2
        foreach ($imapMail->getAttachments() as $imapAttachment) {
81 1
            $attachment = new File([
82 1
                'fileName' => $imapAttachment->name,
83 1
                'filePath' => $imapAttachment->filePath,
84
            ]);
85 1
            $attachments[] = $attachment;
86
        }
87
88 2
        return new Mail([
89 2
            'id' => $imapMail->id,
90 2
            'sender' => $imapMail->fromAddress,
91 2
            'subject' => $imapMail->subject,
92 2
            'attachments' => $attachments,
93
        ]);
94
    }
95
}