DatabaseSpool::stop()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Rafael
5
 * Date: 02/05/2015
6
 * Time: 22:16
7
 */
8
9
namespace Citrax\Bundle\DatabaseSwiftMailerBundle\Spool;
10
11
12
use Citrax\Bundle\DatabaseSwiftMailerBundle\Entity\Email;
13
use Citrax\Bundle\DatabaseSwiftMailerBundle\Entity\EmailRepository;
14
use Swift_Mime_Message;
15
use Swift_Transport;
16
17
class DatabaseSpool extends \Swift_ConfigurableSpool {
18
    /**
19
     * @var EmailRepository
20
     */
21
    private $repository;
22
23
    private $parameters;
24
25
    public function __construct(EmailRepository $repository, $parameters)
26
    {
27
        $this->repository = $repository;
28
        $this->parameters = $parameters;
29
    }
30
31
32
    /**
33
     * Starts this Spool mechanism.
34
     */
35
    public function start()
36
    {
37
        // TODO: Implement start() method.
38
    }
39
40
    /**
41
     * Stops this Spool mechanism.
42
     */
43
    public function stop()
44
    {
45
        // TODO: Implement stop() method.
46
    }
47
48
    /**
49
     * Tests if this Spool mechanism has started.
50
     *
51
     * @return bool
52
     */
53
    public function isStarted()
54
    {
55
        return true;
56
    }
57
58
    /**
59
     * Queues a message.
60
     *
61
     * @param Swift_Mime_Message $message The message to store
62
     *
63
     * @return bool    Whether the operation has succeeded
64
     */
65
    public function queueMessage(Swift_Mime_Message $message)
66
    {
67
        $email = new Email();
68
        $email->setFromEmail(implode('; ', array_keys($message->getFrom())) );
69
70
        if($message->getTo() !== null ){
71
            $email->setToEmail(implode('; ', array_keys($message->getTo())) );
72
        }
73
        if($message->getCc() !== null ){
74
            $email->setCcEmail(implode('; ', array_keys($message->getCc())) );
75
        }
76
        if($message->getBcc() !== null ){
77
            $email->setBccEmail(implode('; ', array_keys($message->getBcc())) );
78
        }
79
        if($message->getReplyTo() !== null ){
80
            $email->setReplyToEmail(implode('; ', array_keys($message->getReplyTo())) );
81
        }
82
83
        $email->setBody($message->getBody());
84
        $email->setSubject($message->getSubject());
85
        $email->setMessage($message);
86
87
        $this->repository->addEmail($email);
88
    }
89
90
    /**
91
     * Sends messages using the given transport instance.
92
     *
93
     * @param Swift_Transport $transport A transport instance
94
     * @param string[] $failedRecipients An array of failures by-reference
95
     *
96
     * @return int     The number of sent emails
97
     */
98
    public function flushQueue(Swift_Transport $transport, &$failedRecipients = null)
99
    {
100
        if (!$transport->isStarted())
101
        {
102
            $transport->start();
103
        }
104
105
        $count = 0;
106
        $emails = $this->repository->getEmailQueue($this->getMessageLimit());
107
108
        foreach($emails as $email){
109
            /*@var $message \Swift_Mime_Message */
110
            $message = $email->getMessage();
111
            try{
112
                $count_= $transport->send($message, $failedRecipients);
113
                if($count_ > 0){
114
                    $this->repository->markCompleteSending($email);
115
                    $count += $count_;
116
                }else{
117
                    throw new \Swift_SwiftException('The email was not sent.');
118
                }
119
            }catch(\Swift_SwiftException $ex){
120
                $this->repository->markFailedSending($email, $ex);
121
            }
122
        }
123
124
        return $count;
125
    }
126
}
127