chrif /
cocotte
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace Cocotte\DigitalOcean; |
||
| 4 | |||
| 5 | use Assert\Assertion; |
||
| 6 | use Cocotte\Environment\FromEnvLazyFactory; |
||
| 7 | use Cocotte\Environment\LazyEnvironmentValue; |
||
| 8 | use Cocotte\Environment\LazyExportableOption; |
||
| 9 | use Cocotte\Shell\Env; |
||
| 10 | |||
| 11 | class ApiToken implements LazyExportableOption, FromEnvLazyFactory |
||
| 12 | { |
||
| 13 | const DIGITAL_OCEAN_API_TOKEN = 'DIGITAL_OCEAN_API_TOKEN'; |
||
| 14 | const OPTION_NAME = 'digital-ocean-api-token'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | private $value; |
||
| 20 | |||
| 21 | public function __construct(string $value) |
||
| 22 | { |
||
| 23 | Assertion::notEmpty($value, "The API token is empty."); |
||
| 24 | $this->value = $value; |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param Env $env |
||
| 29 | * @return LazyEnvironmentValue|ApiToken |
||
| 30 | */ |
||
| 31 | public static function fromEnv(Env $env): LazyEnvironmentValue |
||
| 32 | { |
||
| 33 | return new self($env->get(self::DIGITAL_OCEAN_API_TOKEN, "")); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 34 | } |
||
| 35 | |||
| 36 | public static function toEnv(string $value, Env $env): void |
||
| 37 | { |
||
| 38 | $env->put(self::DIGITAL_OCEAN_API_TOKEN, $value); |
||
| 39 | } |
||
| 40 | |||
| 41 | public static function optionName(): string |
||
| 42 | { |
||
| 43 | return self::OPTION_NAME; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function toString(): string |
||
| 47 | { |
||
| 48 | return $this->value; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function __toString() |
||
| 52 | { |
||
| 53 | return $this->toString(); |
||
| 54 | } |
||
| 55 | |||
| 56 | } |
||
| 57 |