DownloadMyriadContacts::handle()   B
last analyzed

Complexity

Conditions 9
Paths 5

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 32
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 47
ccs 29
cts 29
cp 1
crap 9
rs 8.0555
1
<?php
2
3
namespace MyriadDataStore\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use MyriadSoap\MyriadSoapException;
7
8
class DownloadMyriadContacts extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'myriad-download:contacts
16
    {start=1 : Start contact ID}
17
    {--count=1000 : Count identifiers}
18
    {--queue=sync : Queue name}
19
    ';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Download/Update myriad contact';
27
28 4
    public function handle()
29
    {
30 4
        $start = (int) $this->argument('start');
31 4
        if ($start <= 0) {
32 1
            $this->error("Invalid start value [$start]");
33
34 1
            return 1;
35
        }
36 4
        $count = (int) $this->option('count');
37 4
        if ($count <= 0 || $count > 100000) {
38 1
            $this->error("Invalid count value [$count]");
39
40 1
            return 1;
41
        }
42 3
        $queue = (string) $this->option('queue');
43
44 3
        if ($queue === 'sync') {
45 2
            $successDownloads = 0;
46 2
            $errorDownloads   = 0;
47
48 2
            for ($i = $start; $i < ($start + $count); $i++) {
49
                try {
50 2
                    $myriadContact = (new \MyriadDataStore\Actions\DownloadMyriadContact())
51 2
                        ->execute($i);
52 1
                    if ($myriadContact) {
53 1
                        $successDownloads++;
54
                    } else {
55 1
                        $errorDownloads++;
56
                    }
57 1
                } catch (MyriadSoapException $e) {
58 1
                    $this->error("Contact [$i] error: " . $e->getMessage());
59 1
                    $errorDownloads++;
60
                }
61
            }
62
63 2
            if ($errorDownloads == 0) {
0 ignored issues
show
introduced by
The condition $errorDownloads == 0 is always true.
Loading history...
64 1
                $this->info("Successful downloaded: {$successDownloads} contacts.");
65
            } else {
66 1
                $this->warn("Successful downloaded: {$successDownloads} contacts.");
67 2
                $this->warn("Downloads fails: {$errorDownloads} contacts.");
68
            }
69
        } else {
70 1
            \MyriadDataStore\Jobs\DownloadMyriadContacts::dispatch($start, $count)->onQueue($queue);
71 1
            $this->info("Job sent to queue [$queue].");
72
        }
73
74 3
        return 0;
75
    }
76
}
77