Completed
Push — master ( 52aff9...2d40fa )
by Darko
38s queued 10s
created

PostProcessTvPipeline::isValidChar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Services\TvProcessor;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\Log;
8
9
class PostProcessTvPipeline extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'postprocess:tv-pipeline
17
                            {guid : First character of release guid (a-f, 0-9)}
18
                            {renamed? : Process renamed only (optional)}
19
                            {--mode=pipeline : Processing mode: pipeline or parallel}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Post process TV releases by GUID character using pipelined providers';
27
28
    /**
29
     * Execute the console command.
30
     */
31
    public function handle(): int
32
    {
33
        $guid = $this->argument('guid');
34
        $renamed = $this->argument('renamed') ?? '';
35
        $mode = $this->option('mode') ?? 'pipeline';
36
37
        if (! $this->isValidChar($guid)) {
38
            $this->error('GUID character must be a-f or 0-9.');
39
40
            return self::FAILURE;
41
        }
42
43
        if (! in_array($mode, ['pipeline', 'parallel'], true)) {
44
            $this->error('Mode must be either "pipeline" or "parallel".');
45
46
            return self::FAILURE;
47
        }
48
49
        try {
50
            $tvProcessor = new TvProcessor(true); // true = echo output
51
            $tvProcessor->process('', $guid, $renamed, $mode);
52
53
            return self::SUCCESS;
54
        } catch (\Throwable $e) {
55
            Log::error($e->getTraceAsString());
56
            $this->error($e->getMessage());
57
58
            return self::FAILURE;
59
        }
60
    }
61
62
    /**
63
     * Check if the character contains a-f or 0-9.
64
     */
65
    private function isValidChar(string $char): bool
66
    {
67
        return \in_array(
68
            $char,
69
            ['a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
70
            true
71
        );
72
    }
73
}
74