<?php
namespace App\Entity\ContentDistribution;
use App\Entity\ExternalFeedsAdjustableParameter;
use App\Entity\ExternalPartner\ExternalPartnerGauge;
use App\Service\ExternalFeedsService;
use App\Utility\ReflectionHelper;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Exception;
/**
* @ORM\Entity
*
* @ORM\Table(
* name="content_distributors"
* )
*/
class ContentDistributor
{
public function __construct(
int $id,
string $name,
bool $isAutomated,
?string $inFeedValueName = null
) {
$this->id = $id;
$this->name = $name;
if (is_null($inFeedValueName)) {
$this->inFeedValueName = strtolower($name) . 'InFeedValue';
} else {
$this->inFeedValueName = $inFeedValueName;
}
$this->contentDistributorFeeds = new ArrayCollection();
$this->isAutomated = $isAutomated;
$this->hasFeedsWithIndividualCharacteristics = false;
$this->maxRecurrentJobAgeInDays = ExternalFeedsAdjustableParameter::MAX_RECURRENT_JOB_AGE_IN_DAYS;
$this->maxForwardsToRecurrentJobPerMonth = -1;
$this->minAmountOfWordsForDescriptionTexts = 0;
}
/**
* @ORM\Column(name="id", type="integer")
*
* @ORM\Id
*/
private int $id;
public function setId(int $id)
{
$this->id = $id;
}
public function getId(): int
{
return $this->id;
}
/**
* @ORM\Column(name="name", type="string", length=128)
*/
protected string $name;
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @ORM\Column(name="in_feed_value_name", type="string", length=256, nullable=true)
*/
private ?string $inFeedValueName;
public function getInFeedValueName(): ?string
{
return $this->inFeedValueName;
}
public function setInFeedValueName(?string $inFeedValueName): void
{
$this->inFeedValueName = $inFeedValueName;
}
/**
* @ORM\Column(name="is_automated", type="boolean", nullable=false)
*
* The naming is not perfect - of course, ALL our feeds are generated by automated processes!
*
* A content distributor that is automated is one whose feed generation can be generalized
* so much that no dedicated implementation is needed, and its feed can be generated
* through generalized logic.
*
* See JanusHercules\ContentDistributionFeedCreation\Domain\Command\StartFeedCreationCommand
* for the main process that generates the feeds for all automated content distributors.
*/
private bool $isAutomated;
public function getIsAutomated(): bool
{
return $this->isAutomated;
}
public function setIsAutomated(bool $isAutomated): void
{
$this->isAutomated = $isAutomated;
}
/**
* @ORM\Column(name="has_feeds_with_individual_characteristics", type="boolean", nullable=false)
*/
private bool $hasFeedsWithIndividualCharacteristics;
public function hasFeedsWithIndividualCharacteristics(): bool
{
return $this->hasFeedsWithIndividualCharacteristics;
}
public function setHasFeedsWithIndividualCharacteristics(bool $hasFeedsWithIndividualCharacteristics): void
{
$this->hasFeedsWithIndividualCharacteristics = $hasFeedsWithIndividualCharacteristics;
}
/**
* @var Collection<int, ContentDistributorFeed>
*
* @ORM\OneToMany(targetEntity="\App\Entity\ContentDistribution\ContentDistributorFeed", mappedBy="contentDistributor", cascade={"persist", "remove"})
*/
private Collection $contentDistributorFeeds;
public function addContentDistributorFeed(ContentDistributorFeed $feed): void
{
foreach ($this->contentDistributorFeeds as $contentDistributorFeed) {
if ($contentDistributorFeed->getCategory() === $feed->getCategory()) {
return;
}
}
$this->contentDistributorFeeds->add($feed);
}
/**
* @return Collection<int, ContentDistributorFeed>
*/
public function getContentDistributorFeeds(): Collection
{
return $this->contentDistributorFeeds;
}
/**
* @ORM\Column(name="max_recurrent_job_age_in_days", type="integer", nullable=false)
*/
private int $maxRecurrentJobAgeInDays;
public function getMaxRecurrentJobAgeInDays(): int
{
return $this->maxRecurrentJobAgeInDays;
}
/**
* @ORM\Column(name="max_forwards_to_recurrent_job_per_month", type="integer", nullable=false)
*/
private int $maxForwardsToRecurrentJobPerMonth;
public function getMaxForwardsToRecurrentJobPerMonth(): int
{
return $this->maxForwardsToRecurrentJobPerMonth;
}
/**
* @ORM\Column(name="min_amount_of_words_for_description_texts", type="integer", nullable=false)
*/
private int $minAmountOfWordsForDescriptionTexts;
public function getMinAmountOfWordsForDescriptionTexts(): int
{
return $this->minAmountOfWordsForDescriptionTexts;
}
public function getFeedByCategory(string $category): ?ContentDistributorFeed
{
foreach ($this->contentDistributorFeeds as $feed) {
if (strtolower($feed->getCategory()) === strtolower($category)) {
return $feed;
}
}
return null;
}
public function canStoreExternalPartnerGaugeForNumberOfRecurrentJobsInFeed(): bool
{
return ReflectionHelper::hasConstWithName(
ExternalPartnerGauge::class,
'GAUGE_TYPE_NUMBER_OF_RECURRENT_JOBS_IN_' . mb_strtoupper($this->getName()) . '_FEED'
);
}
/**
* @throws Exception
*/
public function getExternalPartnerGaugeTypeForNumberOfRecurrentJobsInFeed(): int
{
if (!$this->canStoreExternalPartnerGaugeForNumberOfRecurrentJobsInFeed()) {
throw new Exception('Content Distributor ' . $this->getName() . ' cannot store gauge type for number of recurrent jobs in feed');
} else {
return constant(
ExternalPartnerGauge::class . '::GAUGE_TYPE_NUMBER_OF_RECURRENT_JOBS_IN_'
. mb_strtoupper($this->getName())
. '_FEED'
);
}
}
public function canStoreExternalPartnerGaugeForAverageCpcPriceForCFeed(): bool
{
return $this->getIsAutomated()
&& $this->getId() !== ExternalFeedsService::EXTERNAL_FEED_EBAY
&& ReflectionHelper::hasConstWithName(
ExternalPartnerGauge::class,
'GAUGE_TYPE_AVERAGE_CPC_PRICE_FOR_' . mb_strtoupper($this->getName()) . '_C_FEED'
);
}
/**
* @throws Exception
*/
public function getExternalPartnerGaugeTypeForAverageCpcPriceForCFeed(): int
{
if (!$this->canStoreExternalPartnerGaugeForAverageCpcPriceForCFeed()) {
throw new Exception('Content Distributor ' . $this->getName() . ' cannot store gauge type for average cpc price for c feed');
} else {
return constant(
ExternalPartnerGauge::class . '::GAUGE_TYPE_AVERAGE_CPC_PRICE_FOR_'
. mb_strtoupper($this->getName())
. '_C_FEED'
);
}
}
}