Completed
Push — master ( 8a1851...b55ba8 )
by Alexander
13:15
created

FileMailer::sendMessage()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.0342

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 9
c 2
b 1
f 0
nc 4
nop 1
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
crap 5.0342
rs 9.6111
1
<?php
2
namespace Yiisoft\Mailer;
3
4
use Psr\EventDispatcher\EventDispatcherInterface;
5
use Psr\Log\LoggerInterface;
6
7
/**
8
 * FileMailer is a mock mailer that save email messages in files instead of sending them.
9
 */
10
class FileMailer extends BaseMailer
11
{
12
    /**
13
     * The path where message files located.
14
     * @var string $path
15
     */
16
    private $path;
17
18
    /**
19
     * Returns path.
20
     * @return string
21
     */
22 2
    public function getPath(): string
23
    {
24 2
        return $this->path;
25
    }
26
27
    /**
28
     * Sets path.
29
     * @param string $path
30
     */
31 2
    public function setPath(string $path): void
32
    {
33 2
        $this->path = $path;
34
    }
35
36
37
    /**
38
     * @param MessageFactoryInterface $messageFactory
39
     * @param Composer $composer
40
     * @param EventDispatcherInterface $eventDispatcher
41
     * @param LoggerInterface $logger
42
     * @param string $path
43
     */
44 12
    public function __construct(
45
        MessageFactoryInterface $messageFactory,
46
        Composer $composer,
47
        EventDispatcherInterface $eventDispatcher,
48
        LoggerInterface $logger,
49
        string $path
50
    ) {
51 12
        parent::__construct($messageFactory, $composer, $eventDispatcher, $logger);
52 12
        $this->path = $path;
53
    }
54
55
    /**
56
     * @var callable a PHP callback that return a file name which will be used to
57
     * save the email message.
58
     * If not set, the file name will be generated based on the current timestamp.
59
     *
60
     * The signature of the callback is:
61
     *
62
     * ```php
63
     * function ($mailer, $message)
64
     * ```
65
     */
66
    private $filenameCallback;
67
68
    /**
69
     * Sets filename callback.
70
     * @param callable $callback
71
     */
72 1
    public function setFilenameCallback(callable $callback): void
73
    {
74 1
        $this->filenameCallback = $callback;
75
    }
76
77
    /**
78
     * @return string the filename for saving the message.
79
     * @throws \Exception
80
     */
81 5
    protected function generateMessageFilename(): string
82
    {
83 5
        $time = microtime(true);
84
85 5
        return date('Ymd-His-', $time) . sprintf('%04d', (int) (($time - (int) $time) * 10000)) . '-' . sprintf('%04d', random_int(0, 10000)) . '.eml';
86
    }
87
88 6
    protected function sendMessage(MessageInterface $message): void
89
    {
90 6
        if ($this->filenameCallback !== null) {
91 1
            $filename = call_user_func($this->filenameCallback, $this, $message);
92
        } else {
93 5
            $filename = $this->generateMessageFilename();
94
        }
95 6
        $filename = $this->path . DIRECTORY_SEPARATOR . $filename;
96 6
        $filepath = dirname($filename);
97 6
        if (!is_dir($filepath) && !mkdir($filepath, 0777, true) && !is_dir($filepath)) {
98
            throw new \RuntimeException(sprintf('Directory "%s" was not created', $filepath));
99
        }
100 6
        file_put_contents($filename, $message->toString());
101
    }
102
}
103