Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
10.53% covered (danger)
10.53%
2 / 19
40.00% covered (warning)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Mailer
10.53% covered (danger)
10.53%
2 / 19
40.00% covered (warning)
40.00%
2 / 5
81.63
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
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
 send
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 sendFromQueue
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 sendFromDir
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
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-2023 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
16/**
17 * Mailer class
18 *
19 * @category   Pop
20 * @package    Pop\Mail
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    3.6.0
25 */
26class Mailer
27{
28
29    /**
30     * Transport object
31     * @var Transport\TransportInterface
32     */
33    protected $transport = null;
34
35    /**
36     * Constructor
37     *
38     * Instantiate the message object
39     *
40     * @param  Transport\TransportInterface $transport
41     */
42    public function __construct(Transport\TransportInterface $transport)
43    {
44        $this->transport = $transport;
45    }
46
47    /**
48     * Get the transport object
49     *
50     * @return Transport\TransportInterface
51     */
52    public function transport()
53    {
54        return $this->transport;
55    }
56
57    /**
58     * Send message
59     *
60     * @param  Message $message
61     * @return mixed
62     */
63    public function send(Message $message)
64    {
65        return $this->transport->send($message);
66    }
67
68    /**
69     * Send messages from mail queue
70     *
71     * @param  Queue $queue
72     * @throws Exception
73     * @return int
74     */
75    public function sendFromQueue(Queue $queue)
76    {
77        $sent     = 0;
78        $messages = $queue->prepare();
79
80        foreach ($messages as $message) {
81            $this->transport->send($message);
82            $sent++;
83        }
84
85        return $sent;
86    }
87
88    /**
89     * Send messages from email messages saved to disk in a directory
90     *
91     * @param  string $dir
92     * @throws Exception
93     * @return int
94     */
95    public function sendFromDir($dir)
96    {
97        if (!file_exists($dir)) {
98            throw new Exception('Error: That directory does not exist');
99        }
100
101        $sent  = 0;
102        $files = scandir($dir);
103
104        foreach ($files as $file) {
105            if (($file != '.') && ($file != '..')) {
106                $message = Message::load($dir . DIRECTORY_SEPARATOR . $file);
107                $this->transport->send($message);
108                $sent++;
109            }
110        }
111
112        return $sent;
113    }
114
115}