Lines
91.66%
11 / 12
Functions and Methods
83.33%
5 / 6
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| AppId | 91.66% | 11 / 12 | 83.33% | 5 / 6 | 9.05 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | |||||
| tryFromString | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | |||||
| equals | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| collidesWith | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| toEnvironmentKey | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| __toString | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Contracts\App; | |
| 6 | ||
| 7 | use Stewart\Contracts\Exception\IdentifierError; | |
| 8 | use Stewart\Contracts\Exception\IdentifierException; | |
| 9 | use Stringable; | |
| 10 | ||
| 11 | final readonly class AppId implements Stringable | |
| 12 | { | |
| 13 | private const string PATTERN = '/\A[a-z][a-z0-9_-]*\z/'; | |
| 14 | ||
| 15 | private const int MAX_LENGTH_LEAVING_ROOM_IN_STORE_KEYS = 100; | |
| 16 | ||
| 17 | public string $value; | |
| 18 | ||
| 19 | /** @throws IdentifierException */ | |
| 20 | public function __construct(string $value) | |
| 21 | { | |
| 22 | if (preg_match(self::PATTERN, $value) !== 1) { | |
| 23 | throw IdentifierException::createForReason(IdentifierError::AppIdInvalid, appId: $value); | |
| 24 | } | |
| 25 | ||
| 26 | if (\strlen($value) > self::MAX_LENGTH_LEAVING_ROOM_IN_STORE_KEYS) { | |
| 27 | throw IdentifierException::createForReason(IdentifierError::AppIdTooLong, appId: $value, limit: self::MAX_LENGTH_LEAVING_ROOM_IN_STORE_KEYS); | |
| 28 | } | |
| 29 | ||
| 30 | $this->value = $value; | |
| 31 | } | |
| 32 | ||
| 33 | public static function tryFromString(string $value): ?self | |
| 34 | { | |
| 35 | try { | |
| 36 | return new self($value); | |
| 37 | } catch (IdentifierException) { | |
| 38 | return null; | |
| 39 | } | |
| 40 | } | |
| 41 | ||
| 42 | public function equals(self $other): bool | |
| 43 | { | |
| 44 | return $this->value === $other->value; | |
| 45 | } | |
| 46 | ||
| 47 | public function collidesWith(self $other): bool | |
| 48 | { | |
| 49 | return $this->toEnvironmentKey() === $other->toEnvironmentKey(); | |
| 50 | } | |
| 51 | ||
| 52 | public function toEnvironmentKey(): string | |
| 53 | { | |
| 54 | // Environment variable names cannot tell "-" from "_". | |
| 55 | return strtr($this->value, '-', '_'); | |
| 56 | } | |
| 57 | ||
| 58 | public function __toString(): string | |
| 59 | { | |
| 60 | return $this->value; | |
| 61 | } | |
| 62 | } |