Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.48% covered (success)
81.48%
22 / 27
75.00% covered (success)
75.00%
6 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Mailer
81.48% covered (success)
81.48%
22 / 27
75.00% covered (success)
75.00%
6 / 8
16.43
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 transport
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDefaultFrom
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getDefaultFrom
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasDefaultFrom
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 send
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 sendFromQueue
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
2.15
 sendFromDir
72.73% covered (success)
72.73%
8 / 11
0.00% covered (danger)
0.00%
0 / 1
5.51
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Mail;
15
16use Pop\Mail\Transport\TransportInterface;
17
18/**
19 * Mailer class
20 *
21 * @category   Pop
22 * @package    Pop\Mail
23 * @author     Nick Sagona, III <dev@nolainteractive.com>
24 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    4.0.0
27 */
28class Mailer
29{
30
31    /**
32     * Transport object
33     * @var ?TransportInterface
34     */
35    protected ?TransportInterface $transport = null;
36
37
38    /**
39     * Default from address
40     * @var ?string
41     */
42    protected ?string $defaultFrom = null;
43
44    /**
45     * Constructor
46     *
47     * Instantiate the message object
48     *
49     * @param TransportInterface $transport
50     * @param ?string            $defaultFrom
51     */
52    public function __construct(TransportInterface $transport, ?string $defaultFrom = null)
53    {
54        $this->transport   = $transport;
55        $this->defaultFrom = $defaultFrom;
56    }
57
58    /**
59     * Get the transport object
60     *
61     * @return TransportInterface
62     */
63    public function transport(): TransportInterface
64    {
65        return $this->transport;
66    }
67
68    /**
69     * Set default from address
70     *
71     * @param  string $from
72     * @return Mailer
73     */
74    public function setDefaultFrom(string $from): Mailer
75    {
76        $this->defaultFrom = $from;
77        return $this;
78    }
79
80    /**
81     * Get default from address
82     *
83     * @return ?string
84     */
85    public function getDefaultFrom(): ?string
86    {
87        return $this->defaultFrom;
88    }
89
90    /**
91     * Has default from address
92     *
93     * @return bool
94     */
95    public function hasDefaultFrom(): bool
96    {
97        return ($this->defaultFrom !== null);
98    }
99
100    /**
101     * Send message
102     *
103     * @param  Message $message
104     * @return mixed
105     */
106    public function send(Message $message): mixed
107    {
108        if ((!$message->hasFrom()) && ($this->hasDefaultFrom())) {
109            $message->setFrom($this->defaultFrom);
110        }
111
112        return $this->transport->send($message);
113    }
114
115    /**
116     * Send messages from mail queue
117     *
118     * @param  Queue $queue
119     * @return int
120     */
121    public function sendFromQueue(Queue $queue): int
122    {
123        $sent     = 0;
124        $messages = $queue->prepare();
125
126        foreach ($messages as $message) {
127            $this->transport->send($message);
128            $sent++;
129        }
130
131        return $sent;
132    }
133
134    /**
135     * Send messages from email messages saved to disk in a directory
136     *
137     * @param  string $dir
138     * @throws Exception
139     * @return int
140     */
141    public function sendFromDir(string $dir): int
142    {
143        if (!file_exists($dir)) {
144            throw new Exception('Error: That directory does not exist');
145        }
146
147        $sent  = 0;
148        $files = array_filter(scandir($dir), function($value) {
149            return (($value != '.') && ($value != '..') && ($value != '.empty'));
150        });
151
152        foreach ($files as $file) {
153            $message = Message::load($dir . DIRECTORY_SEPARATOR . $file);
154            $this->transport->send($message);
155            $sent++;
156        }
157
158        return $sent;
159    }
160
161}