Completed
Push — master ( aeeaf1...77ad8d )
by Joachim
04:16
created

Report::getUuid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusStockMovementPlugin\Model;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Exception;
10
use InvalidArgumentException;
11
use Ramsey\Uuid\Uuid;
12
use Safe\Exceptions\StringsException;
13
use function Safe\sprintf;
14
use Sylius\Component\Resource\Model\TimestampableTrait;
15
16
class Report implements ReportInterface
17
{
18
    use TimestampableTrait;
19
20
    /** @var int */
21
    protected $id;
22
23
    /** @var string */
24
    protected $uuid;
25
26
    /** @var string */
27
    protected $status = self::STATUS_SUCCESS;
28
29
    /** @var ReportConfigurationInterface */
30
    protected $reportConfiguration;
31
32
    /** @var StockMovementInterface[]|Collection */
33
    protected $stockMovements;
34
35
    /** @var ErrorInterface[]|Collection */
36
    protected $errors;
37
38
    /**
39
     * @throws Exception
40
     */
41
    public function __construct()
42
    {
43
        $this->uuid = Uuid::uuid4()->toString();
44
        $this->stockMovements = new ArrayCollection();
45
        $this->errors = new ArrayCollection();
46
    }
47
48
    public function getId(): ?int
49
    {
50
        return $this->id;
51
    }
52
53
    public function getUuid(): string
54
    {
55
        return $this->uuid;
56
    }
57
58
    public function getStatus(): ?string
59
    {
60
        return $this->status;
61
    }
62
63
    /**
64
     * @throws StringsException
65
     */
66
    public function setStatus(string $status): void
67
    {
68
        if (!in_array($status, self::getStatuses(), true)) {
69
            throw new InvalidArgumentException(sprintf('The status "%s" is not allowed. Allowed statuses are: ["%s"]', $status, implode('", "', self::getStatuses())));
70
        }
71
        $this->status = $status;
72
    }
73
74
    public function isSuccessful(): bool
75
    {
76
        return self::STATUS_SUCCESS === $this->status;
77
    }
78
79
    public function isErrored(): bool
80
    {
81
        return self::STATUS_ERROR === $this->status;
82
    }
83
84
    public static function getStatuses(): array
85
    {
86
        return [
87
            self::STATUS_SUCCESS => self::STATUS_SUCCESS,
88
            self::STATUS_ERROR => self::STATUS_ERROR,
89
        ];
90
    }
91
92
    public function getReportConfiguration(): ?ReportConfigurationInterface
93
    {
94
        return $this->reportConfiguration;
95
    }
96
97
    public function setReportConfiguration(ReportConfigurationInterface $reportConfiguration): void
98
    {
99
        $this->reportConfiguration = $reportConfiguration;
100
    }
101
102
    public function getStockMovements(): Collection
103
    {
104
        return $this->stockMovements;
105
    }
106
107
    public function addStockMovement(StockMovementInterface $stockMovement): void
108
    {
109
        $this->stockMovements->add($stockMovement);
110
    }
111
112
    public function getErrors(): Collection
113
    {
114
        return $this->errors;
115
    }
116
117
    public function addError(ErrorInterface $error): void
118
    {
119
        if ($this->hasError($error)) {
120
            return;
121
        }
122
123
        $this->errors->add($error);
124
        $error->setReport($this);
125
    }
126
127
    public function hasError(ErrorInterface $error): bool
128
    {
129
        return $this->errors->contains($error);
130
    }
131
132
    public function clearErrors(): void
133
    {
134
        foreach ($this->errors as $error) {
135
            $error->setReport(null);
136
        }
137
138
        $this->errors->clear();
139
    }
140
}
141