|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Farbcode\StatefulResources; |
| 4 | + |
| 5 | +use Farbcode\StatefulResources\Concerns\ResolvesState; |
| 6 | +use Farbcode\StatefulResources\Contracts\ResourceState; |
| 7 | + |
| 8 | +/** |
| 9 | + * ActiveState manages which state is currently active for resources. |
| 10 | + */ |
| 11 | +class ActiveState |
| 12 | +{ |
| 13 | + use ResolvesState; |
| 14 | + |
| 15 | + private ?string $sharedState; |
| 16 | + |
| 17 | + private array $resourceStates = []; |
| 18 | + |
| 19 | + public function __construct() |
| 20 | + { |
| 21 | + $this->sharedState = null; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Set the shared state for all resources. |
| 26 | + */ |
| 27 | + public function setShared(string|ResourceState $state): void |
| 28 | + { |
| 29 | + $state = $this->resolveState($state); |
| 30 | + $this->sharedState = $state; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Get the shared state. |
| 35 | + */ |
| 36 | + public function getShared(): string |
| 37 | + { |
| 38 | + return $this->sharedState ?? app(StateRegistry::class)->getDefaultState(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Check if a specific resource class has a state set. |
| 43 | + */ |
| 44 | + public function matchesShared(string|ResourceState $state): bool |
| 45 | + { |
| 46 | + $state = $this->resolveState($state); |
| 47 | + |
| 48 | + return $this->getShared() === $state; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Set the state for a specific resource class. |
| 53 | + */ |
| 54 | + public function setForResource(string $resourceClass, string|ResourceState $state): void |
| 55 | + { |
| 56 | + $state = $this->resolveState($state); |
| 57 | + $this->resourceStates[$resourceClass] = $state; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Get the state for a specific resource class. |
| 62 | + */ |
| 63 | + public function getForResource(string $resourceClass): string |
| 64 | + { |
| 65 | + return $this->resourceStates[$resourceClass] ?? app(StateRegistry::class)->getDefaultState(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Check if the current state matches the resource's state. |
| 70 | + */ |
| 71 | + public function matchesResource(string $resourceClass, string|ResourceState $state): bool |
| 72 | + { |
| 73 | + $state = $this->resolveState($state); |
| 74 | + |
| 75 | + return $this->getForResource($resourceClass) === $state; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Get the current state for a resource. |
| 80 | + * If no resource class is provided, returns the shared state. |
| 81 | + */ |
| 82 | + public function get(?string $resourceClass = null): string |
| 83 | + { |
| 84 | + if ($resourceClass === null) { |
| 85 | + return $this->getShared(); |
| 86 | + } |
| 87 | + |
| 88 | + return $this->getForResource($resourceClass); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Set the current state for a resource. |
| 93 | + * If no resource class is provided, sets the shared state. |
| 94 | + */ |
| 95 | + public function set(string|ResourceState $state, ?string $resourceClass = null): void |
| 96 | + { |
| 97 | + $state = $this->resolveState($state); |
| 98 | + |
| 99 | + if ($resourceClass === null) { |
| 100 | + $this->setShared($state); |
| 101 | + } else { |
| 102 | + $this->setForResource($resourceClass, $state); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Check if the current state matches the given state for a resource. |
| 108 | + * If no resource class is provided, checks against the shared state. |
| 109 | + */ |
| 110 | + public function matches(string|ResourceState $state, ?string $resourceClass = null): bool |
| 111 | + { |
| 112 | + $state = $this->resolveState($state); |
| 113 | + |
| 114 | + if ($resourceClass === null) { |
| 115 | + return $this->matchesShared($state); |
| 116 | + } |
| 117 | + |
| 118 | + return $this->matchesResource($resourceClass, $state); |
| 119 | + } |
| 120 | +} |
0 commit comments