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.

Swift_PdoSpool::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 5
crap 1
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 PDO.
10
 *
11
 * @copyright 2015 LibreWorks contributors
12
 * @license   http://opensource.org/licenses/MIT MIT License
13
 */
14
class Swift_PdoSpool 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 \PDO The database driver
18
     */
19
    protected $pdo;
20
    protected $table;
21
    protected $pkey;
22
    protected $messageField;
23
    protected $timeField;
24
25
    /**
26
     * Creates a new PdoSpool
27
     *
28
     * @param \PDO $pdo The database driver
29
     * @param string $table The table name
30
     * @param string $pkey The primary key field
31
     * @param string $messageField The field to add serialized message data
32
     * @param string $timeField The integer field to store message timestamp
33
     */
34 2
    public function __construct(\PDO $pdo, $table, $pkey, $messageField, $timeField)
35
    {
36 2
        $this->pdo = $pdo;
37 2
        $this->table = $this->checkBlank($table);
38 2
        $this->pkey = $this->checkBlank($pkey);
39 2
        $this->messageField = $this->checkBlank($messageField);
40 2
        $this->timeField = $this->checkBlank($timeField);
41 1
    }
42
43 1
    private function checkBlank($value)
44
    {
45 1
        $value = trim($value);
46 1
        if (strlen($value) == 0) {
47 1
            throw new \InvalidArgumentException("This parameter cannot be blank");
48
        }
49 1
        return $value;
50
    }
51
52
    /**
53
     * Tests if this Spool mechanism has started.
54
     *
55
     * @return bool
56
     */
57 1
    public function isStarted()
58
    {
59 1
        return true;
60
    }
61
62
    /**
63
     * Starts this Spool mechanism.
64
     */
65
    public function start()
66
    {
67
    }
68
69
    /**
70
     * Stops this Spool mechanism.
71
     */
72
    public function stop()
73
    {
74
    }
75
76
    /**
77
     * Queues a message.
78
     *
79
     * @param Swift_Mime_SimpleMessage $message The message to store
80
     *
81
     * @throws Swift_IoException
82
     *
83
     * @return bool
84
     */
85 1
    public function queueMessage(Swift_Mime_SimpleMessage $message)
86
    {
87
        try {
88 1
            $stmt = $this->pdo->prepare("INSERT INTO " . $this->table . " ("
89 1
                . $this->messageField . ") VALUES (?)");
90 1
            $stmt->bindValue(1, serialize($message), PDO::PARAM_STR);
91 1
            $stmt->execute();
92 1
            return true;
93
        } catch (\Exception $e) {
94
            throw new Swift_IoException("Could not enqueue message", 0, $e);
95
        }
96
    }
97
98
    /**
99
     * Execute a recovery if for any reason a process is sending for too long.
100
     *
101
     * @param int $timeout in second Defaults is for very slow smtp responses
102
     */
103 1
    public function recover($timeout = 900)
0 ignored issues
show
Unused Code introduced by
The parameter $timeout is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
    {
105
        try {
106 1
            $stmt = $this->pdo->prepare('UPDATE ' . $this->table . ' SET '
107 1
                . $this->timeField . ' = NULL WHERE ' . $this->timeField . ' <= ?');
108 1
            $stmt->bindValue(1, time() - 900, PDO::PARAM_INT);
109 1
            $stmt->execute();
110
        } catch (\Exception $e) {
111
            throw new Swift_IoException("Could not update email sent on date", 0, $e);
112
        }
113 1
    }
114
115
    /**
116
     * Sends messages using the given transport instance.
117
     *
118
     * @param Swift_Transport $transport        A transport instance
119
     * @param string[]        $failedRecipients An array of failures by-reference
120
     *
121
     * @return int The number of sent e-mails
122
     */
123 1
    public function flushQueue(Swift_Transport $transport, &$failedRecipients = null)
124
    {
125 1
        if (!$transport->isStarted()) {
126
            $transport->start();
127
        }
128 1
        $limit = $this->getMessageLimit();
129 1
        $limit = $limit > 0 ? $limit : null;
130 1
        $failedRecipients = (array) $failedRecipients;
131 1
        $count = 0;
132 1
        $time = time();
133 1
        $timeLimit = $this->getTimeLimit();
134
        try {
135 1
            $results = $this->pdo->query('SELECT ' . $this->pkey . ' as pkey, '
136 1
                . $this->messageField . ' as email FROM ' . $this->table . ' WHERE '
137 1
                . $this->timeField . ' IS NULL');
138 1
            $ustmt = $this->pdo->prepare('UPDATE ' . $this->table . ' SET '
139 1
                . $this->timeField . ' = ? WHERE ' . $this->pkey . ' = ?');
140 1
            $dstmt = $this->pdo->prepare('DELETE FROM ' . $this->table . ' WHERE '
141 1
                . $this->pkey . ' = ?');
142 1
            foreach ($results->fetchAll(PDO::FETCH_COLUMN) as $result) {
143 1
                $id = $result[0];
144 1
                $ustmt->execute(array(time(), $result[0]));
145 1
                $message = unserialize($result[1]);
146 1
                $count += $transport->send($message, $failedRecipients);
0 ignored issues
show
Bug introduced by
It seems like $failedRecipients defined by (array) $failedRecipients on line 130 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...
147 1
                $dstmt->execute(array($id));
148 1
                if ($limit && $count >= $limit) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $limit of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
149
                    break;
150
                }
151 1
                if ($timeLimit && (time() - $time) >= $timeLimit) {
152 1
                    break;
153
                }
154
            }
155 1
            return $count;
156
        } catch (\Exception $e) {
157
            throw new Swift_IoException("Could not access database", 0, $e);
158
        }
159
    }
160
}
161