Completed
Pull Request — master (#4)
by Klochok
10:05
created

MailHelper::getNewUserTokenUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace hiam\tests\_support\Helper;
4
5
use Yii;
6
7
/**
8
 * Class TestMails
9
 *
10
 * @author Andrey Klochok <[email protected]>
11
 */
12
class MailHelper extends \Codeception\Module
13
{
14
    private $mailsDir;
15
16
    public function getMessages(): ?string
17
    {
18
        $ignored = ['.', '..', '.svn', '.htaccess'];
19
        $files = [];
20
        $tries = 0;
21
        while (!file_exists($this->getMailsDir())) {
22
            if ($tries++ > 15) {
23
                return null;
24
            }
25
            sleep(1);
26
        }
27
        foreach (scandir($this->getMailsDir()) as $file) {
28
            if (in_array($file, $ignored)) continue;
29
            $files[$file] = filemtime($this->getMailsDir() . DIRECTORY_SEPARATOR . $file);
30
        }
31
32
        arsort($files);
33
        $files = array_keys($files);
34
35
        return $files ? $files : null;
36
    }
37
38
    public function getLastMessage(): ?string
39
    {
40
        $messages = $this->getMessages();
41
42
        if ($messages) {
43
            $f = $this->getMailsDir() . DIRECTORY_SEPARATOR . reset($messages);
44
            $mime = mailparse_msg_parse_file($f);
45
            $struct = mailparse_msg_get_structure($mime);
46
            if (in_array('1.1', $struct)) {
47
                $info = mailparse_msg_get_part_data(mailparse_msg_get_part($mime, '1'));
48
                ob_start();
49
                mailparse_msg_extract_part_file(mailparse_msg_get_part($mime, '1.2'), $f);
50
                $body = ob_get_contents();
51
                ob_end_clean();
52
53
                return compact('info', 'body');
54
            }
55
        }
56
57
        return null;
58
    }
59
60
    public function getResetTokenUrl($f): ?string
61
    {
62
        if (preg_match("|<a.*(?=href=\"([^\"]*)\")[^>]*>([^<]*)</a>|i", $f['body'], $matches)) {
63
            return $matches[1];
64
        }
65
66
        return null;
67
    }
68
69
    public function getNewUserTokenUrl($f): ?string
0 ignored issues
show
Unused Code introduced by
The parameter $f is not used and could be removed.

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

Loading history...
70
    {
71
        // todo
72
    }
73
74
75
    public function clearMessages(): void
76
    {
77
        $files = glob($this->getMailsDir() . DIRECTORY_SEPARATOR . '*');
78
        foreach ($files as $file) {
79
            if (is_file($file)) {
80
                unlink($file);
81
            }
82
        }
83
    }
84
85
    public function getMailsDir(): string
86
    {
87
        if (!$this->mailsDir) {
88
            $this->mailsDir = Yii::getAlias('@runtime/mail');
89
        }
90
91
        return $this->mailsDir;
92
    }
93
}
94