1<?php
2
3/*
4 * This file is part of SwiftMailer.
5 * (c) 2004-2009 Chris Corbyn
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11/**
12 * Does real time reporting of pass/fail for each recipient.
13 *
14 * @author Chris Corbyn
15 */
16class Swift_Plugins_ReporterPlugin implements Swift_Events_SendListener
17{
18    /**
19     * The reporter backend which takes notifications.
20     *
21     * @var Swift_Plugins_Reporter
22     */
23    private $reporter;
24
25    /**
26     * Create a new ReporterPlugin using $reporter.
27     *
28     * @param Swift_Plugins_Reporter $reporter
29     */
30    public function __construct(Swift_Plugins_Reporter $reporter)
31    {
32        $this->reporter = $reporter;
33    }
34
35    /**
36     * Not used.
37     */
38    public function beforeSendPerformed(Swift_Events_SendEvent $evt)
39    {
40    }
41
42    /**
43     * Invoked immediately after the Message is sent.
44     *
45     * @param Swift_Events_SendEvent $evt
46     */
47    public function sendPerformed(Swift_Events_SendEvent $evt)
48    {
49        $message = $evt->getMessage();
50        $failures = array_flip($evt->getFailedRecipients());
51        foreach ((array) $message->getTo() as $address => $null) {
52            $this->reporter->notify($message, $address, (array_key_exists($address, $failures) ? Swift_Plugins_Reporter::RESULT_FAIL : Swift_Plugins_Reporter::RESULT_PASS));
53        }
54        foreach ((array) $message->getCc() as $address => $null) {
55            $this->reporter->notify($message, $address, (array_key_exists($address, $failures) ? Swift_Plugins_Reporter::RESULT_FAIL : Swift_Plugins_Reporter::RESULT_PASS));
56        }
57        foreach ((array) $message->getBcc() as $address => $null) {
58            $this->reporter->notify($message, $address, (array_key_exists($address, $failures) ? Swift_Plugins_Reporter::RESULT_FAIL : Swift_Plugins_Reporter::RESULT_PASS));
59        }
60    }
61}
62