Completed
Pull Request — master (#2)
by Anton
01:22
created

Job::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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\Entities;
15
16
use Cog\Contracts\Paket\Job\Entities\Job as JobContract;
17
use Cog\Contracts\Paket\Process\Entities\Process as ProcessContract;
18
use Cog\Contracts\Paket\Requirement\Entities\Requirement as RequirementContract;
19
use Cog\Laravel\Paket\Process\Entities\Process;
20
use Cog\Laravel\Paket\Requirement\Entities\Requirement;
21
use DateTimeInterface;
22
use Illuminate\Support\Carbon;
23
24
final class Job implements JobContract
25
{
26
    private $type;
27
28
    private $id;
29
30
    private $status;
31
32
    private $process;
33
34
    private $requirement;
35
36
    private $createdAt;
37
38
    public function __construct(
39
        string $type,
40
        string $id,
41
        string $status,
42
        DateTimeInterface $createdAt,
43
        ProcessContract $process,
44
        ?RequirementContract $requirement = null
45
    )
46
    {
47
        $this->type = $type;
48
        $this->id = $id;
49
        $this->status = $status;
50
        $this->createdAt = $createdAt;
51
        $this->process = $process;
52
        $this->requirement = $requirement;
53
    }
54
55
    public static function fromArray(array $job): JobContract
56
    {
57
        return new self(
58
            $job['type'],
59
            $job['id'],
60
            $job['status'],
61
            Carbon::createFromFormat(DATE_RFC3339_EXTENDED, $job['createdAt']),
62
            Process::fromArray($job['process']),
63
            Requirement::fromArray($job['requirement'])
64
        );
65
    }
66
67
    public function toArray(): array
68
    {
69
        return [
70
            'type' => $this->getType(),
71
            'id' => $this->getId(),
72
            'status' => $this->status,
73
            'requirement' => $this->requirement->toArray(),
74
            'process' => $this->process->toArray(),
75
            'createdAt' => $this->createdAt->format(DATE_RFC3339_EXTENDED),
76
        ];
77
    }
78
79
    public function getType(): string
80
    {
81
        return $this->type;
82
    }
83
84
    public function getId(): string
85
    {
86
        return $this->id;
87
    }
88
}
89