Completed
Push — master ( cdbec6...220d3b )
by Anton
01:25
created

RunJob::handle()   A

Complexity

Conditions 4
Paths 10

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
cc 4
nc 10
nop 2
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\Jobs;
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, 'InProgress');
43
44
        try {
45
            switch ($this->paketJob->getType()) {
46
                case 'RequirementInstall':
47
                    // TODO: Don't pass `paketJob`
48
                    $composer->install($this->paketJob->getRequirement(), $this->paketJob);
49
                    break;
50
                case 'RequirementUninstall':
51
                    // TODO: Don't pass `paketJob`
52
                    $composer->uninstall($this->paketJob->getRequirement(), $this->paketJob);
53
                    break;
54
                default:
55
                    // TODO: Throw custom exception
56
                    throw new RuntimeException('Unknown type of job');
57
            }
58
59
            $jobs->changeJobStatus($this->paketJob, 'Done');
60
            $jobs->changeJobExitCode($this->paketJob, 0);
61
        } catch (JobFailed $exception) {
62
            $jobs->changeJobStatus($this->paketJob, 'Failed');
63
            $jobs->changeJobExitCode($this->paketJob, $exception->getExitCode());
64
        }
65
66
        event(new JobHasBeenTerminated($this->paketJob));
67
    }
68
}
69