src/App/Entity/ContentDistribution/RecurrentJobPersistentValue.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity\ContentDistribution;
  3. use App\Utility\ReflectionHelper;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Exception;
  6. /**
  7. * @ORM\Entity
  8. *
  9. * @ORM\Table(
  10. * name="recurrent_job_persistent_values"
  11. * )
  12. */
  13. class RecurrentJobPersistentValue
  14. {
  15. public const NAME_AGENTUR_FUER_ARBEIT_SHORT_ID = 'agenturFuerArbeitShortId';
  16. public const TYPE_STRING = 0;
  17. public const TYPE_FLOAT = 1;
  18. public const TYPE_BOOL = 2;
  19. public const NAMES_TO_TYPES = [
  20. self::NAME_AGENTUR_FUER_ARBEIT_SHORT_ID => self::TYPE_STRING,
  21. ];
  22. /** @throws Exception */
  23. public static function createWithStringValue(string $recurrentJobId, string $name, string $stringValue): self
  24. {
  25. if (!ReflectionHelper::hasConstWithValue(self::class, 'NAME_', $name)) {
  26. throw new Exception("Not a name: '{$name}'.");
  27. }
  28. if (self::NAMES_TO_TYPES[$name] !== self::TYPE_STRING) {
  29. throw new Exception("Value type for name {$name} cannot be 'string'.");
  30. }
  31. $distributionValue = new self();
  32. $distributionValue->recurrentJobId = $recurrentJobId;
  33. $distributionValue->name = $name;
  34. $distributionValue->stringValue = $stringValue;
  35. return $distributionValue;
  36. }
  37. /** @throws Exception */
  38. public static function createWithFloatValue(string $recurrentJobId, string $name, float $floatValue): self
  39. {
  40. if (!ReflectionHelper::hasConstWithValue(self::class, 'NAME_', $name)) {
  41. throw new Exception("Not a name: '{$name}'.");
  42. }
  43. if (self::NAMES_TO_TYPES[$name] !== self::TYPE_FLOAT) {
  44. throw new Exception("Value type for name {$name} cannot be 'float'.");
  45. }
  46. $distributionValue = new self();
  47. $distributionValue->recurrentJobId = $recurrentJobId;
  48. $distributionValue->name = $name;
  49. $distributionValue->floatValue = $floatValue;
  50. return $distributionValue;
  51. }
  52. /** @throws Exception */
  53. public static function createWithBoolValue(string $recurrentJobId, string $name, float $boolValue): self
  54. {
  55. if (!ReflectionHelper::hasConstWithValue(self::class, 'NAME_', $name)) {
  56. throw new Exception("Not a name: '{$name}'.");
  57. }
  58. if (self::NAMES_TO_TYPES[$name] !== self::TYPE_BOOL) {
  59. throw new Exception("Value type for name {$name} cannot be 'float'.");
  60. }
  61. $distributionValue = new self();
  62. $distributionValue->recurrentJobId = $recurrentJobId;
  63. $distributionValue->name = $name;
  64. $distributionValue->boolValue = $boolValue;
  65. return $distributionValue;
  66. }
  67. public static function isValidName(string $name): bool
  68. {
  69. return array_key_exists($name, self::NAMES_TO_TYPES);
  70. }
  71. /**
  72. * @ORM\Id
  73. *
  74. * @ORM\Column(type="string", nullable=false)
  75. */
  76. private string $name;
  77. public function getName(): string
  78. {
  79. return $this->name;
  80. }
  81. /**
  82. * @ORM\Column(type="string", name="string_value", nullable=true, length=4096)
  83. */
  84. private ?string $stringValue;
  85. public function getStringValue(): ?string
  86. {
  87. return $this->stringValue;
  88. }
  89. /** @throws Exception */
  90. public function setStringValue(?string $stringValue): void
  91. {
  92. if (self::NAMES_TO_TYPES[$this->name] !== self::TYPE_STRING) {
  93. throw new Exception("Value type for name {$this->name} cannot be 'string'.");
  94. }
  95. $this->stringValue = $stringValue;
  96. }
  97. /**
  98. * @ORM\Column(type="float", name="float_value", nullable=true)
  99. */
  100. private ?float $floatValue;
  101. public function getFloatValue(): ?float
  102. {
  103. return $this->floatValue;
  104. }
  105. /** @throws Exception */
  106. public function setFloatValue(?float $floatValue): void
  107. {
  108. if (self::NAMES_TO_TYPES[$this->name] !== self::TYPE_FLOAT) {
  109. throw new Exception("Value type for name {$this->name} cannot be 'float'.");
  110. }
  111. $this->floatValue = $floatValue;
  112. }
  113. /**
  114. * @ORM\Column(type="boolean", name="bool_value", nullable=true)
  115. */
  116. private ?bool $boolValue;
  117. public function getBoolValue(): ?bool
  118. {
  119. return $this->boolValue;
  120. }
  121. /** @throws Exception */
  122. public function setBoolValue(?bool $boolValue): void
  123. {
  124. if (self::NAMES_TO_TYPES[$this->name] !== self::TYPE_BOOL) {
  125. throw new Exception("Value type for name {$this->name} cannot be 'bool'.");
  126. }
  127. $this->boolValue = $boolValue;
  128. }
  129. /**
  130. * @ORM\Id
  131. *
  132. * @ORM\Column(type="string", name="recurrent_job_id", nullable=true, length=128)
  133. */
  134. private string $recurrentJobId;
  135. public function getRecurrentJobId(): string
  136. {
  137. return $this->recurrentJobId;
  138. }
  139. public function setRecurrentJobId(string $recurrentJobId): void
  140. {
  141. $this->recurrentJobId = $recurrentJobId;
  142. }
  143. }