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

RunJob::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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