Completed
Push — master ( 652184...cef032 )
by Anton
01:24
created

RunJob   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 26 4
1
<?php
2
3
/*
4
 * This file is part of Laravel Paket.
5
 *
6
 * (c) Anton Komarev <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cog\Laravel\Paket\Job\QueueJobs;
15
16
use Cog\Contracts\Paket\Job\Entities\Job as JobContract;
17
use Cog\Contracts\Paket\Job\Exceptions\JobFailed;
18
use Cog\Contracts\Paket\Job\Repositories\Job as JobRepositoryContract;
19
use Cog\Laravel\Paket\Job\Events\JobHasBeenTerminated;
20
use Cog\Laravel\Paket\Support\Composer;
21
use Illuminate\Bus\Queueable;
22
use Illuminate\Queue\InteractsWithQueue;
23
use Illuminate\Contracts\Queue\ShouldQueue;
24
use Illuminate\Foundation\Bus\Dispatchable;
25
use RuntimeException;
26
27
final class RunJob implements ShouldQueue
28
{
29
    use Dispatchable;
30
    use InteractsWithQueue;
31
    use Queueable;
32
33
    private $paketJob;
34
35
    public function __construct(JobContract $paketJob)
36
    {
37
        $this->paketJob = $paketJob;
38
    }
39
40
    public function handle(JobRepositoryContract $jobs, Composer $composer): void
41
    {
42
        $jobs->changeJobStatus($this->paketJob, 'Running');
43
44
        try {
45
            switch ($this->paketJob->getType()) {
46
                case 'RequirementInstall':
47
                    $composer->install($this->paketJob->getRequirement(), $this->paketJob);
48
                    break;
49
                case 'RequirementUninstall':
50
                    $composer->uninstall($this->paketJob->getRequirement(), $this->paketJob);
51
                    break;
52
                default:
53
                    // TODO: Throw custom exception
54
                    throw new RuntimeException('Unknown type of job');
55
            }
56
57
            $jobs->changeJobStatus($this->paketJob, 'Success');
58
            $jobs->changeJobExitCode($this->paketJob, 0);
59
        } catch (JobFailed $exception) {
60
            $jobs->changeJobStatus($this->paketJob, 'Failed');
61
            $jobs->changeJobExitCode($this->paketJob, $exception->getExitCode());
62
        }
63
64
        event(new JobHasBeenTerminated($this->paketJob));
65
    }
66
}
67