DownloadLatestMyriadContacts   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 57
ccs 23
cts 25
cp 0.92
rs 10
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 38 9
1
<?php
2
3
namespace MyriadDataStore\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use MyriadDataStore\MyriadDataDownloader;
7
use MyriadSoap\MyriadSoapException;
8
9
class DownloadLatestMyriadContacts extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'myriad-download:contacts:latest
17
    {start? : Start contact ID, if empty - will be used latest from database}
18
    {--buffer=10 : Buffer items count}
19
    ';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Download/Update latest myriad contacts';
27
28 1
    public function handle()
29
    {
30 1
        $start = (int) $this->argument('start');
31 1
        if ($start <= 0) {
32 1
            $modelClass = MyriadDataDownloader::$contactModel;
33 1
            $start      = $modelClass::select(['id'])->orderBy('id', 'DESC')->first()?->id ?: 0;
34 1
            $start += 1;
35
        }
36 1
        $buffer = (int) $this->option('buffer');
37 1
        if ($buffer < 0 || $buffer > 500) {
38
            $this->error("Invalid buffer value [$buffer]");
39
40
            return 1;
41
        }
42
43 1
        $successDownloads = 0;
44 1
        $errors           = 0;
45 1
        for ($i = $start; $i <= ($start + 1000); $i++) {
46
            try {
47 1
                $myriadContact = (new \MyriadDataStore\Actions\DownloadMyriadContact())
48 1
                    ->execute($i);
49 1
                if ($myriadContact) {
50 1
                    $successDownloads++;
51 1
                    (new \MyriadDataStore\Actions\DownloadMyriadContactOrdersBasic())
52 1
                        ->execute($i);
53
                }
54 1
            } catch (MyriadSoapException $e) {
55 1
                $errors++;
56
            }
57
58 1
            if ($errors >= $buffer) {
59 1
                break;
60
            }
61
        }
62
63 1
        $this->info("Successful downloaded: {$successDownloads} contacts.");
64
65 1
        return 0;
66
    }
67
}
68