Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 31
Dedup
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 31
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 3
 launch
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 23
 getPublishData
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 5
1<?php
2
3namespace Qmp\Laravel\Deduplication\Console\Commands;
4
5use Error;
6use Exception;
7use Illuminate\Support\Facades\Redis;
8use Qmp\Laravel\Deduplication\Models\Session;
9use Qmp\Laravel\ToolsLaravel\Traits\Timer;
10
11class Dedup extends TryCatchCommand
12{
13    use Timer;
14    /**
15     * The name and signature of the console command.
16     *
17     * @var string
18     */
19    protected $signature = 'dedup:run {sessionId} {onlyFiles} {--throw-error : Throw errors if catch}';
20
21    /**
22     * The console command description.
23     *
24     * @var string
25     */
26    protected $description = 'Run all dedup steps';
27
28    /**
29     * Undocumented variable
30     *
31     * @var Session
32     */
33    protected $currentSession;
34
35    /**
36     * The name of the current service
37     *
38     * @var string
39     */
40    protected $serviceName;
41
42    /**
43     * The list of all deduplication commands
44     * in process order
45     *
46     * @var array
47     */
48    protected $commandsList = [
49        'read-files',
50        'blacklist',
51        'file',
52        'editor',
53        'deathmatch',
54        'deathmatch-process',
55        'generate-files-fake',
56        'report'
57    ];
58
59    /**
60     * Undocumented variable
61     *
62     * @var array
63     */
64    protected $commandsListOnlyFiles = [
65        'generate-files',
66        'report'
67    ];
68
69    /**
70     * Undocumented function
71     */
72    public function __construct()
73    {
74        $this->serviceName = getenv('CONTAINER_NAME');
75        parent::__construct();
76    }
77
78    /**
79     * Execute the console command.
80     *
81     * @return mixed
82     */
83    public function launch(): void
84    {
85        $this->startTimer('total');
86        $commandsList = $this->argument('onlyFiles') === "true" ? $this->commandsListOnlyFiles : $this->commandsList;
87        $stepOffset = $this->argument('onlyFiles') === "true" ? count($this->commandsList) - count($this->commandsListOnlyFiles) : 0;
88
89        try {
90            $options = ['sessionId' => $this->argument('sessionId')];
91            $this->currentSession = Session::where('id', $this->argument('sessionId'))->first();
92            $this->currentSession->update(['total_steps' => count($this->commandsList), 'error' => null]);
93
94            foreach ($commandsList as $step => $command) {
95                Redis::publish('deduplication', json_encode(['action' => 'step', 'sessionId' => $this->argument('sessionId'), 'message' => $this->currentSession->name . " - dedup:$command"]));
96                Session::where('id', $this->argument('sessionId'))->update(['step' => $step +  $stepOffset + 1]);
97                $this->info('Command : ' . "dedup:$command");
98                $this->call("dedup:$command", $options);
99            }
100
101            Redis::publish('deduplication', $this->getPublishData('finish-dedup', $this->currentSession->name . " - finish-dedup"));
102        } catch (Exception | Error $e) {
103            $error = "Line : " . $e->getLine() . "\n" . "File : " . $e->getFile() . "\n" . "Error : " . $e->getMessage();
104            $this->error($e->getMessage());
105            $this->error("ExitCode: 1");
106
107            $this->call('dedup:error', [
108                'sessionId' => $this->argument('sessionId'),
109                '--message' => $error
110            ]);
111
112            Redis::publish('deduplication', $this->getPublishData('error-dedup', $e->getMessage()));
113        }
114
115        $this->info('Deduplication done !');
116        $this->info('Total deduplication time : ' . $this->getTimer('total'));
117    }
118
119    /**
120     * Undocumented function
121     *
122     * @param [type] $action
123     * @param [type] $message
124     * @return string
125     */
126    protected function getPublishData($action, $message): string {
127        $return = [
128            'action' => $action,
129            'sessionId' => $this->argument('sessionId'),
130            'serviceName' => $this->serviceName,
131            'message' => $message
132        ];
133        
134        return json_encode($return);
135    }
136}