Completed
Push — 5.x ( 50a512...e1df8b )
by Lars
05:40
created

Swift_MemorySpool::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of SwiftMailer.
5
 * (c) 2011 Fabien Potencier <[email protected]>
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
 * Stores Messages in memory.
13
 *
14
 * @author Fabien Potencier
15
 */
16
class Swift_MemorySpool implements Swift_Spool
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    protected $messages = array();
19
20 2
    public function __construct()
21
    {
22 2
        Swift::$useMemorySpool = true;
23 2
    }
24
25
26
    /**
27
     * Tests if this Transport mechanism has started.
28
     *
29
     * @return bool
30
     */
31
    public function isStarted()
32
    {
33
        return true;
34
    }
35
36
    /**
37
     * Starts this Transport mechanism.
38
     */
39
    public function start()
40
    {
41
    }
42
43
    /**
44
     * Stops this Transport mechanism.
45
     */
46
    public function stop()
47
    {
48
    }
49
50
    /**
51
     * Stores a message in the queue.
52
     *
53
     * @param Swift_Mime_Message $message The message to store
54
     *
55
     * @return bool Whether the operation has succeeded
56
     */
57 2
    public function queueMessage(Swift_Mime_Message $message)
58
    {
59
        // clone the message to make sure it is not changed while in the queue
60 2
        $this->messages[] = clone $message;
61
62 2
        return true;
63
    }
64
65
    /**
66
     * Sends messages using the given transport instance.
67
     *
68
     * @param Swift_Transport $transport        A transport instance
69
     * @param string[]        $failedRecipients An array of failures by-reference
70
     *
71
     * @return int The number of sent emails
72
     */
73 2
    public function flushQueue(Swift_Transport $transport, &$failedRecipients = null)
74
    {
75 2
        if (!$this->messages) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->messages of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
76
            return 0;
77
        }
78
79 2
        if (!$transport->isStarted()) {
80
            $transport->start();
81
        }
82
83 2
        $count = 0;
84 2
        while ($message = array_pop($this->messages)) {
85 2
            $count += $transport->send($message, $failedRecipients);
86
        }
87
88 2
        return $count;
89
    }
90
}
91