-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathEntityState.php
More file actions
83 lines (64 loc) · 2.41 KB
/
Copy pathEntityState.php
File metadata and controls
83 lines (64 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace Code16\Sharp\EntityList\Commands;
use BackedEnum;
use Code16\Sharp\Exceptions\EntityList\SharpInvalidEntityStateException;
use Code16\Sharp\Exceptions\SharpInvalidConfigException;
use InvalidArgumentException;
/**
* Base class for applicative Entity States.
*/
abstract class EntityState extends InstanceCommand
{
protected array $states = [];
public function states(): array
{
$this->buildStates();
return $this->states;
}
protected function addState($key, string $label, ?string $color = null): self
{
if ($key instanceof BackedEnum && ! is_string($key = $key->value)) {
throw new InvalidArgumentException('When using enum as a key, it must be string backed.');
}
$this->states[$key] = [$label, $color];
return $this;
}
protected function view(string $bladeView, array $params = []): array
{
throw new SharpInvalidConfigException('View return type is not supported for a state.');
}
protected function info(string $message, bool $reload = false): array
{
throw new SharpInvalidConfigException('Info return type is not supported for a state.');
}
protected function download(string $filePath, ?string $fileName = null, ?string $diskName = null): array
{
throw new SharpInvalidConfigException('Download return type is not supported for a state.');
}
protected function streamDownload(string $fileContent, string $fileName): array
{
throw new SharpInvalidConfigException('StreamDownload return type is not supported for a state.');
}
protected function link(string $link, bool $openInNewTab = false): array
{
throw new SharpInvalidConfigException('Link return type is not supported for a state.');
}
/**
* @throws SharpInvalidEntityStateException
*/
public function execute($instanceId, array $data = []): array
{
$stateId = $data['value'];
$this->buildStates();
if (! in_array($stateId, array_keys($this->states))) {
throw new SharpInvalidEntityStateException($stateId);
}
return $this->updateState($instanceId, $stateId) ?: $this->refresh($instanceId);
}
public function label(): ?string
{
return null;
}
abstract protected function buildStates(): void;
abstract protected function updateState($instanceId, string $stateId): ?array;
}