Completed
Pull Request — 5.x (#24)
by Lars
06:58
created

Swift_MemorySpool   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 46.88%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 101
ccs 15
cts 32
cp 0.4688
rs 10
wmc 12
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A start() 0 3 1
A stop() 0 3 1
A setFlushRetries() 0 4 1
C flushQueue() 0 33 7
A isStarted() 0 4 1
A queueMessage() 0 7 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
    /**
19
     * @var array
20
     */
21
    protected $messages = array();
22
23
    /**
24
     * @var int
25
     */
26
    private $flushRetries = 3;
27
28
    /**
29
     * Tests if this Transport mechanism has started.
30
     *
31
     * @return bool
32
     */
33
    public function isStarted()
34
    {
35
        return true;
36
    }
37
38
    /**
39
     * Starts this Transport mechanism.
40
     */
41
    public function start()
42
    {
43
    }
44
45
    /**
46
     * Stops this Transport mechanism.
47
     */
48
    public function stop()
49
    {
50
    }
51
52
    /**
53
     * @param int $retries
54
     */
55
    public function setFlushRetries($retries)
56
    {
57
        $this->flushRetries = $retries;
58
    }
59
60
    /**
61
     * Stores a message in the queue.
62
     *
63
     * @param Swift_Mime_Message $message The message to store
64
     *
65
     * @return bool Whether the operation has succeeded
66
     */
67 2
    public function queueMessage(Swift_Mime_Message $message)
68
    {
69
        //clone the message to make sure it is not changed while in the queue
70 2
        $this->messages[] = clone $message;
71
72 2
        return true;
73
    }
74
75
    /**
76
     * Sends messages using the given transport instance.
77
     *
78
     * @param Swift_Transport $transport        A transport instance
79
     * @param string[]        $failedRecipients An array of failures by-reference
80
     *
81
     * @return int The number of sent emails
82
     */
83 2
    public function flushQueue(Swift_Transport $transport, &$failedRecipients = null)
84
    {
85 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...
86
            return 0;
87
        }
88
89 2
        if (!$transport->isStarted()) {
90
            $transport->start();
91
        }
92
93 2
        $count = 0;
94 2
        $retries = $this->flushRetries;
95 2
        while ($retries--) {
96
            try {
97 2
                while ($message = array_pop($this->messages)) {
98 2
                    $count += $transport->send($message, $failedRecipients);
99 2
                }
100 2
            } catch (Swift_TransportException $exception) {
101
                if ($retries) {
102
                    // re-queue the message at the end of the queue to give a chance
103
                    // to the other messages to be sent, in case the failure was due to
104
                    // this message and not just the transport failing
105
                    array_unshift($this->messages, $message);
0 ignored issues
show
Bug introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
106
                    // wait half a second before we try again
107
                    usleep(500000);
108
                } else {
109
                    throw $exception;
110
                }
111
            }
112 2
        }
113
114 2
        return $count;
115
    }
116
}
117