Passed
Pull Request — master (#6)
by Joachim
05:35
created

Report::setStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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