Passed
Push — master ( e5adf9...504346 )
by Alexander
01:40
created

FileMailer::setPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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