Completed
Push — master ( 20d2a7...83241b )
by Anton
01:20
created

src/Job/Entities/Job.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 Ramsey\Uuid\Uuid;
22
23
final class Job implements JobContract
24
{
25
    private $type;
26
27
    private $id;
28
29
    private $status;
30
31
    private $process;
32
33
    private $requirement;
34
35
    public function __construct(
36
        string $type,
37
        string $id,
38
        string $status,
39
        ProcessContract $process,
40
        ?RequirementContract $requirement = null
41
    )
42
    {
43
        $this->type = $type;
44
        $this->id = $id;
45
        $this->status = $status;
46
        $this->process = $process;
47
        $this->requirement = $requirement;
48
    }
49
50
    public static function ofType(string $type): JobContract
51
    {
52
        return new self(
53
            $type,
54
            Uuid::uuid4()->toString(), // TODO: Generate uuid5 identifier
0 ignored issues
show
Deprecated Code introduced by
The method Ramsey\Uuid\UuidInterface::toString() has been deprecated with message: In ramsey/uuid 4.0.0, this method will be replaced with the __toString() magic method, which is currently available in the Uuid concrete class. The new recommendation is to cast Uuid objects to string, rather than calling `toString()`.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
55
            'Waiting',
56
            new Process('')
57
        );
58
    }
59
60
    public static function fromArray(array $job): JobContract
61
    {
62
        return new self(
63
            $job['type'],
64
            $job['id'],
65
            $job['status'],
66
            Process::fromArray($job['process']),
67
            Requirement::fromArray($job['requirement'])
68
        );
69
    }
70
71
    public function toArray(): array
72
    {
73
        return [
74
            'type' => $this->getType(),
75
            'id' => $this->getId(),
76
            'status' => $this->status,
77
            'requirement' => $this->requirement->toArray(),
78
            'process' => $this->process->toArray(),
79
        ];
80
    }
81
82
    public function getType(): string
83
    {
84
        return $this->type;
85
    }
86
87
    public function getId(): string
88
    {
89
        return $this->id;
90
    }
91
}
92