src/App/Entity/ContentDistribution/ContentDistributor.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity\ContentDistribution;
  3. use App\Entity\ExternalFeedsAdjustableParameter;
  4. use App\Entity\ExternalPartner\ExternalPartnerGauge;
  5. use App\Service\ExternalFeedsService;
  6. use App\Utility\ReflectionHelper;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Exception;
  11. /**
  12. * @ORM\Entity
  13. *
  14. * @ORM\Table(
  15. * name="content_distributors"
  16. * )
  17. */
  18. class ContentDistributor
  19. {
  20. public function __construct(
  21. int $id,
  22. string $name,
  23. bool $isAutomated,
  24. ?string $inFeedValueName = null
  25. ) {
  26. $this->id = $id;
  27. $this->name = $name;
  28. if (is_null($inFeedValueName)) {
  29. $this->inFeedValueName = strtolower($name) . 'InFeedValue';
  30. } else {
  31. $this->inFeedValueName = $inFeedValueName;
  32. }
  33. $this->contentDistributorFeeds = new ArrayCollection();
  34. $this->isAutomated = $isAutomated;
  35. $this->hasFeedsWithIndividualCharacteristics = false;
  36. $this->maxRecurrentJobAgeInDays = ExternalFeedsAdjustableParameter::MAX_RECURRENT_JOB_AGE_IN_DAYS;
  37. $this->maxForwardsToRecurrentJobPerMonth = -1;
  38. $this->minAmountOfWordsForDescriptionTexts = 0;
  39. }
  40. /**
  41. * @ORM\Column(name="id", type="integer")
  42. *
  43. * @ORM\Id
  44. */
  45. private int $id;
  46. public function setId(int $id)
  47. {
  48. $this->id = $id;
  49. }
  50. public function getId(): int
  51. {
  52. return $this->id;
  53. }
  54. /**
  55. * @ORM\Column(name="name", type="string", length=128)
  56. */
  57. protected string $name;
  58. public function getName(): string
  59. {
  60. return $this->name;
  61. }
  62. public function setName(string $name): void
  63. {
  64. $this->name = $name;
  65. }
  66. /**
  67. * @ORM\Column(name="in_feed_value_name", type="string", length=256, nullable=true)
  68. */
  69. private ?string $inFeedValueName;
  70. public function getInFeedValueName(): ?string
  71. {
  72. return $this->inFeedValueName;
  73. }
  74. public function setInFeedValueName(?string $inFeedValueName): void
  75. {
  76. $this->inFeedValueName = $inFeedValueName;
  77. }
  78. /**
  79. * @ORM\Column(name="is_automated", type="boolean", nullable=false)
  80. *
  81. * The naming is not perfect - of course, ALL our feeds are generated by automated processes!
  82. *
  83. * A content distributor that is automated is one whose feed generation can be generalized
  84. * so much that no dedicated implementation is needed, and its feed can be generated
  85. * through generalized logic.
  86. *
  87. * See JanusHercules\ContentDistributionFeedCreation\Domain\Command\StartFeedCreationCommand
  88. * for the main process that generates the feeds for all automated content distributors.
  89. */
  90. private bool $isAutomated;
  91. public function getIsAutomated(): bool
  92. {
  93. return $this->isAutomated;
  94. }
  95. public function setIsAutomated(bool $isAutomated): void
  96. {
  97. $this->isAutomated = $isAutomated;
  98. }
  99. /**
  100. * @ORM\Column(name="has_feeds_with_individual_characteristics", type="boolean", nullable=false)
  101. */
  102. private bool $hasFeedsWithIndividualCharacteristics;
  103. public function hasFeedsWithIndividualCharacteristics(): bool
  104. {
  105. return $this->hasFeedsWithIndividualCharacteristics;
  106. }
  107. public function setHasFeedsWithIndividualCharacteristics(bool $hasFeedsWithIndividualCharacteristics): void
  108. {
  109. $this->hasFeedsWithIndividualCharacteristics = $hasFeedsWithIndividualCharacteristics;
  110. }
  111. /**
  112. * @var Collection<int, ContentDistributorFeed>
  113. *
  114. * @ORM\OneToMany(targetEntity="\App\Entity\ContentDistribution\ContentDistributorFeed", mappedBy="contentDistributor", cascade={"persist", "remove"})
  115. */
  116. private Collection $contentDistributorFeeds;
  117. public function addContentDistributorFeed(ContentDistributorFeed $feed): void
  118. {
  119. foreach ($this->contentDistributorFeeds as $contentDistributorFeed) {
  120. if ($contentDistributorFeed->getCategory() === $feed->getCategory()) {
  121. return;
  122. }
  123. }
  124. $this->contentDistributorFeeds->add($feed);
  125. }
  126. /**
  127. * @return Collection<int, ContentDistributorFeed>
  128. */
  129. public function getContentDistributorFeeds(): Collection
  130. {
  131. return $this->contentDistributorFeeds;
  132. }
  133. /**
  134. * @ORM\Column(name="max_recurrent_job_age_in_days", type="integer", nullable=false)
  135. */
  136. private int $maxRecurrentJobAgeInDays;
  137. public function getMaxRecurrentJobAgeInDays(): int
  138. {
  139. return $this->maxRecurrentJobAgeInDays;
  140. }
  141. /**
  142. * @ORM\Column(name="max_forwards_to_recurrent_job_per_month", type="integer", nullable=false)
  143. */
  144. private int $maxForwardsToRecurrentJobPerMonth;
  145. public function getMaxForwardsToRecurrentJobPerMonth(): int
  146. {
  147. return $this->maxForwardsToRecurrentJobPerMonth;
  148. }
  149. /**
  150. * @ORM\Column(name="min_amount_of_words_for_description_texts", type="integer", nullable=false)
  151. */
  152. private int $minAmountOfWordsForDescriptionTexts;
  153. public function getMinAmountOfWordsForDescriptionTexts(): int
  154. {
  155. return $this->minAmountOfWordsForDescriptionTexts;
  156. }
  157. public function getFeedByCategory(string $category): ?ContentDistributorFeed
  158. {
  159. foreach ($this->contentDistributorFeeds as $feed) {
  160. if (strtolower($feed->getCategory()) === strtolower($category)) {
  161. return $feed;
  162. }
  163. }
  164. return null;
  165. }
  166. public function canStoreExternalPartnerGaugeForNumberOfRecurrentJobsInFeed(): bool
  167. {
  168. return ReflectionHelper::hasConstWithName(
  169. ExternalPartnerGauge::class,
  170. 'GAUGE_TYPE_NUMBER_OF_RECURRENT_JOBS_IN_' . mb_strtoupper($this->getName()) . '_FEED'
  171. );
  172. }
  173. /**
  174. * @throws Exception
  175. */
  176. public function getExternalPartnerGaugeTypeForNumberOfRecurrentJobsInFeed(): int
  177. {
  178. if (!$this->canStoreExternalPartnerGaugeForNumberOfRecurrentJobsInFeed()) {
  179. throw new Exception('Content Distributor ' . $this->getName() . ' cannot store gauge type for number of recurrent jobs in feed');
  180. } else {
  181. return constant(
  182. ExternalPartnerGauge::class . '::GAUGE_TYPE_NUMBER_OF_RECURRENT_JOBS_IN_'
  183. . mb_strtoupper($this->getName())
  184. . '_FEED'
  185. );
  186. }
  187. }
  188. public function canStoreExternalPartnerGaugeForAverageCpcPriceForCFeed(): bool
  189. {
  190. return $this->getIsAutomated()
  191. && $this->getId() !== ExternalFeedsService::EXTERNAL_FEED_EBAY
  192. && ReflectionHelper::hasConstWithName(
  193. ExternalPartnerGauge::class,
  194. 'GAUGE_TYPE_AVERAGE_CPC_PRICE_FOR_' . mb_strtoupper($this->getName()) . '_C_FEED'
  195. );
  196. }
  197. /**
  198. * @throws Exception
  199. */
  200. public function getExternalPartnerGaugeTypeForAverageCpcPriceForCFeed(): int
  201. {
  202. if (!$this->canStoreExternalPartnerGaugeForAverageCpcPriceForCFeed()) {
  203. throw new Exception('Content Distributor ' . $this->getName() . ' cannot store gauge type for average cpc price for c feed');
  204. } else {
  205. return constant(
  206. ExternalPartnerGauge::class . '::GAUGE_TYPE_AVERAGE_CPC_PRICE_FOR_'
  207. . mb_strtoupper($this->getName())
  208. . '_C_FEED'
  209. );
  210. }
  211. }
  212. }