Passed
Push — master ( 994fe7...40271a )
by Gabriel
01:42
created

EmailsSend   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 40.63%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
eloc 30
c 1
b 0
f 1
dl 0
loc 68
ccs 13
cts 32
cp 0.4063
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A sendBatch() 0 5 1
A handle() 0 13 3
A sendEmails() 0 19 4
A getEmailsBatch() 0 7 1
1
<?php
2
3
namespace Nip\MailModule\Console\Commands;
4
5
use Exception;
6
use Nip\Records\Collections\Collection as RecordCollection;
7
use Nip\Records\Locator\ModelLocator;
8
9
/**
10
 * Class EmailsSend
11
 * @package Nip\MailModule\Console\Commands
12
 */
13
class EmailsSend
14
{
15
    /**
16
     * @return int
17
     */
18 1
    public function handle()
19
    {
20 1
        $sent = 0;
21 1
        $batchCount = 1;
22 1
        while ($sent < 10) {
23 1
            $sentEmails = $this->sendBatch($batchCount);
24 1
            if ($sentEmails === false) {
25 1
                return $sent;
26
            }
27 1
            $sent += $this->sendBatch($batchCount);
28 1
            $batchCount++;
29
        }
30
        return $sent;
31
    }
32
33
    /**
34
     * @param int $number
35
     * @return int
36
     */
37 1
    protected function sendBatch($number = 1)
38
    {
39 1
        $emailsCount = 10;
40 1
        $emails = $this->getEmailsBatch($emailsCount, $emailsCount * ($number - 1));
41 1
        return $this->sendEmails($emails);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->sendEmails($emails) could also return false which is incompatible with the documented return type integer. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
42
    }
43
44
    /**
45
     * @param $emails
46
     * @return int|bool
47
     */
48
    protected function sendEmails($emails)
49
    {
50
        if (count($emails) < 1) {
51
            return false;
52
        }
53
        $sent = 0;
54
        $recipients = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $recipients is dead and can be removed.
Loading history...
55
        foreach ($emails as $email) {
56
            echo 'send email [ID:' . $email->id . '][TO:' . implode(',', array_keys($email->getTos())) . ']';
57
            try {
58
                $recipients = $email->send();
59
                $sent++;
60
            } catch (Exception $exception) {
61
                echo $exception->getMessage();
62
//            die('');
63
            }
64
            echo '[R:' . $recipients . ']<br />' . "\n";
65
        }
66
        return $sent;
67
    }
68
69
    /**
70
     * @param int $count
71
     * @param int $offset
72
     * @return RecordCollection
73
     */
74
    protected function getEmailsBatch($count = 10, $offset = 0)
75
    {
76
        $emailsManager = ModelLocator::get('emails');
77
        return $emailsManager->findByParams(
78
            [
79
                'where' => ['`sent` = \'no\' '],
80
                'limit' => $offset . ',' . $count,
81
            ]
82
        );
83
    }
84
}
85