GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 528332...644335 )
by Jonathan
02:29
created

Swift_MongoSpool::queueMessage()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 3
nop 1
crap 6
1
<?php
2
/**
3
 *
4
 * @copyright 2015 LibreWorks contributors
5
 * @license   http://opensource.org/licenses/MIT MIT License
6
 */
7
8
/**
9
 * A Swift spool that uses legacy Mongo drivers (http://docs.php.net/manual/en/book.mongo.php)
10
 *
11
 * @copyright 2015 LibreWorks contributors
12
 * @license   http://opensource.org/licenses/MIT MIT License
13
 */
14
class Swift_MongoSpool extends Swift_ConfigurableSpool
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...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
15
{
16
    /**
17
     * @var \MongoCollection The collection of queued emails
18
     */
19
    protected $collection;
20
    
21
    /**
22
     * Creates a new MongoSpool
23
     *
24
     * @param \MongoCollection $collection The collection for messages
25
     */
26
    public function __construct(\MongoCollection $collection)
27
    {
28
        $this->collection = $collection;
29
    }
30
31
    /**
32
     * Tests if this Spool mechanism has started.
33
     *
34
     * @return bool
35
     */
36
    public function isStarted()
37
    {
38
        return true;
39
    }
40
41
    /**
42
     * Starts this Spool mechanism.
43
     */
44
    public function start()
45
    {
46
    }
47
48
    /**
49
     * Stops this Spool mechanism.
50
     */
51
    public function stop()
52
    {
53
    }
54
55
    /**
56
     * Queues a message.
57
     *
58
     * @param Swift_Mime_Message $message The message to store
59
     * @return bool
60
     * @throws Swift_IoException
61
     */
62
    public function queueMessage(Swift_Mime_Message $message)
63
    {
64
        try {
65
            $doc = array(
66
                'message' => new \MongoBinData(serialize($message), 0)
67
            );
68
            $this->collection->insert($doc);
69
            return true;
70
        } catch (\Exception $e) {
71
            throw new Swift_IoException("Could not write to database", 0, $e);
72
        }
73
    }
74
75
    /**
76
     * Execute a recovery if for any reason a process is sending for too long.
77
     *
78
     * @param int $timeout in second Defaults is for very slow smtp responses
79
     * @throws Swift_IoException
80
     */
81
    public function recover($timeout = 900)
82
    {
83
        $this->write(
84
            array('sentOn' => array('$lte' => new \MongoDate(time() - $timeout))),
85
            array('$set' => array('sentOn' => null)),
86
            array('multiple' => true)
87
        );
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
     * @return int The number of sent e-mails
96
     * @throws Swift_IoException
97
     */
98
    public function flushQueue(Swift_Transport $transport, &$failedRecipients = null)
99
    {
100
        if (!$transport->isStarted()) {
101
            $transport->start();
102
        }
103
        $limit = $this->getMessageLimit();
104
        $results = $this->collection->find(array('sentOn' => null));
105
        if ($limit > 0) {
106
            $results->limit($limit);
107
        }
108
        $failedRecipients = (array) $failedRecipients;
109
        $count = 0;
110
        $time = time();
111
        $timeLimit = $this->getTimeLimit();
112
        foreach ($results as $result) {
113
            if (!isset($result['message']) || !($result['message'] instanceof \MongoBinData)) {
114
                continue;
115
            }
116
            $id = $result['_id'];
117
            $this->write(
118
                array('_id' => $id),
119
                array('$set' => array('sentOn' => new \MongoDate()))
120
            );
121
            $message = unserialize($result['message']->bin);
122
            $count += $transport->send($message, $failedRecipients);
0 ignored issues
show
Bug introduced by
It seems like $failedRecipients defined by (array) $failedRecipients on line 108 can also be of type array; however, Swift_Transport::send() does only seem to accept array<integer,string>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
123
            try {
124
                $this->collection->remove(array('_id' => $id));
125
            } catch (\Exception $e) {
126
                throw new Swift_IoException("Could not update email sent on date", 0, $e);
127
            }            
128
            if ($timeLimit && (time() - $time) >= $timeLimit) {
129
                break;
130
            }
131
        }
132
        return $count;
133
    }
134
135
    /**
136
     * Executes a bulk write to MongoDB, wrapping exceptions.
137
     *
138
     * @param array $query
139
     * @param array $set
140
     * @param arary $options
141
     * @throws Swift_IoException if things go wrong
142
     */
143
    protected function write(array $query, array $set, array $options = array())
144
    {
145
        try {
146
            return $this->collection->update($query, $set, $options);
147
        } catch (\Exception $e) {
148
            throw new Swift_IoException("Could not write to database", 0, $e);
149
        }
150
    }
151
}
152