src/Products/NotificationsBundle/Entity/AbstractContactAttempt.php line 50

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Entity;
  3. use App\Entity\Content\Common\DiscriminatorInterface;
  4. use App\Entity\Content\Common\DiscriminatorTrait;
  5. use Cms\CoreBundle\Util\DateTimeUtils;
  6. use Cms\TenantBundle\Entity\TenantedEntity;
  7. use DateInterval;
  8. use DateTime;
  9. use Doctrine\ORM\Mapping as ORM;
  10. /**
  11.  * Class AbstractContactAttempt
  12.  * @package Products\NotificationsBundle\Entity
  13.  *
  14.  * @ORM\Entity(
  15.  *     repositoryClass = "Products\NotificationsBundle\Doctrine\Repository\ContactAttemptRepository",
  16.  * )
  17.  * @ORM\InheritanceType("SINGLE_TABLE")
  18.  * @ORM\DiscriminatorColumn(
  19.  *     name = "discr",
  20.  *     type = "string",
  21.  * )
  22.  * @ORM\DiscriminatorMap({
  23.  *     ContactAttempts\EmailContactAttempt::DISCR = "Products\NotificationsBundle\Entity\ContactAttempts\EmailContactAttempt",
  24.  *     ContactAttempts\SmsContactAttempt::DISCR = "Products\NotificationsBundle\Entity\ContactAttempts\SmsContactAttempt",
  25.  *     ContactAttempts\VoiceContactAttempt::DISCR = "Products\NotificationsBundle\Entity\ContactAttempts\VoiceContactAttempt",
  26.  *     ContactAttempts\AppContactAttempt::DISCR = "Products\NotificationsBundle\Entity\ContactAttempts\AppContactAttempt",
  27.  *     ContactAttempts\WebsiteContactAttempt::DISCR = "Products\NotificationsBundle\Entity\ContactAttempts\WebsiteContactAttempt",
  28.  *     ContactAttempts\FacebookContactAttempt::DISCR = "Products\NotificationsBundle\Entity\ContactAttempts\FacebookContactAttempt",
  29.  *     ContactAttempts\TwitterContactAttempt::DISCR = "Products\NotificationsBundle\Entity\ContactAttempts\TwitterContactAttempt",
  30.  *     ContactAttempts\InstagramContactAttempt::DISCR = "Products\NotificationsBundle\Entity\ContactAttempts\InstagramContactAttempt",
  31.  *
  32.  * })
  33.  * @ORM\Table(
  34.  *     name = "notis__contact_attempt",
  35.  *     uniqueConstraints = {
  36.  *     },
  37.  *       indexes = {
  38.  *         @ORM\Index(
  39.  *             name = "idx__external_id",
  40.  *             columns = {
  41.  *                 "externalId",
  42.  *             },
  43.  *         ),
  44.  *     },
  45.  * )
  46.  */
  47. abstract class AbstractContactAttempt extends TenantedEntity implements DiscriminatorInterface
  48. {
  49.     const DISCRS = [
  50.         ContactAttempts\EmailContactAttempt::DISCR => ContactAttempts\EmailContactAttempt::class,
  51.         ContactAttempts\SmsContactAttempt::DISCR => ContactAttempts\SmsContactAttempt::class,
  52.         ContactAttempts\VoiceContactAttempt::DISCR => ContactAttempts\VoiceContactAttempt::class,
  53.         ContactAttempts\AppContactAttempt::DISCR => ContactAttempts\AppContactAttempt::class,
  54.         ContactAttempts\WebsiteContactAttempt::DISCR => ContactAttempts\WebsiteContactAttempt::class,
  55.         ContactAttempts\FacebookContactAttempt::DISCR => ContactAttempts\FacebookContactAttempt::class,
  56.         ContactAttempts\TwitterContactAttempt::DISCR => ContactAttempts\TwitterContactAttempt::class,
  57.         ContactAttempts\InstagramContactAttempt::DISCR => ContactAttempts\InstagramContactAttempt::class,
  58.     ];
  59.     const DISCR null;
  60.     const ALL_STATUSES = [
  61.         ...self::STATUSES,
  62.         ...ContactAttempts\EmailContactAttempt::STATUSES,
  63.         ...ContactAttempts\SmsContactAttempt::STATUSES,
  64.         ...ContactAttempts\VoiceContactAttempt::STATUSES,
  65.         ...ContactAttempts\AppContactAttempt::STATUSES,
  66.         ...ContactAttempts\WebsiteContactAttempt::STATUSES,
  67.         ...ContactAttempts\FacebookContactAttempt::STATUSES,
  68.         ...ContactAttempts\TwitterContactAttempt::STATUSES,
  69.         ...ContactAttempts\InstagramContactAttempt::STATUSES,
  70.     ];
  71.     const STATUSES = [
  72.         ...self::PENDING_STATUSES,
  73.         ...self::SUCCESSFUL_STATUSES,
  74.         ...self::FAILED_STATUSES,
  75.     ];
  76.     const ALL_PENDING_STATUSES = [
  77.         ...self::PENDING_STATUSES,
  78.         ...ContactAttempts\EmailContactAttempt::PENDING_STATUSES,
  79.         ...ContactAttempts\SmsContactAttempt::PENDING_STATUSES,
  80.         ...ContactAttempts\VoiceContactAttempt::PENDING_STATUSES,
  81.         ...ContactAttempts\AppContactAttempt::PENDING_STATUSES,
  82.         ...ContactAttempts\WebsiteContactAttempt::PENDING_STATUSES,
  83.         ...ContactAttempts\FacebookContactAttempt::PENDING_STATUSES,
  84.         ...ContactAttempts\TwitterContactAttempt::PENDING_STATUSES,
  85.         ...ContactAttempts\InstagramContactAttempt::PENDING_STATUSES,
  86.     ];
  87.     const PENDING_STATUSES = [
  88.         'attempt.queued',
  89.         'attempt.attempted',
  90.     ];
  91.     const ALL_SUCCESSFUL_STATUSES = [
  92.         ...self::SUCCESSFUL_STATUSES,
  93.         ...ContactAttempts\EmailContactAttempt::SUCCESSFUL_STATUSES,
  94.         ...ContactAttempts\SmsContactAttempt::SUCCESSFUL_STATUSES,
  95.         ...ContactAttempts\VoiceContactAttempt::SUCCESSFUL_STATUSES,
  96.         ...ContactAttempts\AppContactAttempt::SUCCESSFUL_STATUSES,
  97.         ...ContactAttempts\WebsiteContactAttempt::SUCCESSFUL_STATUSES,
  98.         ...ContactAttempts\FacebookContactAttempt::SUCCESSFUL_STATUSES,
  99.         ...ContactAttempts\TwitterContactAttempt::SUCCESSFUL_STATUSES,
  100.         ...ContactAttempts\InstagramContactAttempt::SUCCESSFUL_STATUSES,
  101.     ];
  102.     const SUCCESSFUL_STATUSES = [
  103.         'attempt.successful',
  104.     ];
  105.     const ALL_FAILED_STATUSES = [
  106.         ...self::FAILED_STATUSES,
  107.         ...ContactAttempts\EmailContactAttempt::FAILED_STATUSES,
  108.         ...ContactAttempts\SmsContactAttempt::FAILED_STATUSES,
  109.         ...ContactAttempts\VoiceContactAttempt::FAILED_STATUSES,
  110.         ...ContactAttempts\AppContactAttempt::FAILED_STATUSES,
  111.         ...ContactAttempts\WebsiteContactAttempt::FAILED_STATUSES,
  112.         ...ContactAttempts\FacebookContactAttempt::FAILED_STATUSES,
  113.         ...ContactAttempts\TwitterContactAttempt::FAILED_STATUSES,
  114.         ...ContactAttempts\InstagramContactAttempt::FAILED_STATUSES,
  115.     ];
  116.     const FAILED_STATUSES = [
  117.         'attempt.failed',
  118.     ];
  119.     use DiscriminatorTrait;
  120.     /**
  121.      * @var string|null
  122.      *
  123.      * @ORM\Column(
  124.      *     type = "string",
  125.      *     nullable = true,
  126.      * )
  127.      */
  128.     protected ?string $externalId null;
  129.     /**
  130.      * @var string|null
  131.      *
  132.      * @ORM\Column(
  133.      *     type = "string",
  134.      *     nullable = false,
  135.      *     options = {
  136.      *         "default" = "attempt.queued",
  137.      *     },
  138.      * )
  139.      */
  140.     protected string $event 'attempt.queued';
  141.     /**
  142.      * @var array|null
  143.      *
  144.      * @ORM\Column(
  145.      *     type = "json",
  146.      *     nullable = true,
  147.      * )
  148.      */
  149.     protected ?array $error null;
  150.     /**
  151.      * @var DateTime|null
  152.      *
  153.      * @ORM\Column(
  154.      *     type = "datetime",
  155.      *     nullable = true,
  156.      * )
  157.      */
  158.     protected ?DateTime $triggeredAt null;
  159.     /**
  160.      * @var AbstractNotification|null
  161.      *
  162.      * @ORM\ManyToOne(
  163.      *     targetEntity = "Products\NotificationsBundle\Entity\AbstractNotification",
  164.      * )
  165.      * @ORM\JoinColumn(
  166.      *     name = "message",
  167.      *     referencedColumnName = "id",
  168.      *     nullable = false,
  169.      *     onDelete = "CASCADE",
  170.      * )
  171.      */
  172.     protected ?AbstractNotification $message null;
  173.     /**
  174.      * @var Job|null
  175.      *
  176.      * @ORM\ManyToOne(
  177.      *     targetEntity = "Products\NotificationsBundle\Entity\Job",
  178.      * )
  179.      * @ORM\JoinColumn(
  180.      *     name = "job",
  181.      *     referencedColumnName = "id",
  182.      *     nullable = true,
  183.      *     onDelete = "SET NULL",
  184.      *
  185.      * )
  186.      */
  187.     protected ?Job $job null;
  188.     /**
  189.      * @var int|null
  190.      *
  191.      * @ORM\Column(
  192.      *     type = "integer",
  193.      *     nullable = true,
  194.      * )
  195.      */
  196.     protected ?int $apiWait null;
  197.     /**
  198.      * @var AbstractRecipient|null
  199.      *
  200.      * @ORM\ManyToOne(
  201.      *     targetEntity = "Products\NotificationsBundle\Entity\AbstractRecipient",
  202.      * )
  203.      * @ORM\JoinColumn(
  204.      *     name = "recipient",
  205.      *     referencedColumnName = "id",
  206.      *     nullable = true,
  207.      *     onDelete = "SET NULL",
  208.      * )
  209.      *
  210.      * NOTE:
  211.      * While the join column allows nulls, that is only to support the other attempt classes in the hierarchy.
  212.      * Attempts of this kind should always have the recipient set...
  213.      */
  214.     protected ?AbstractRecipient $recipient null;
  215.     /**
  216.      * @var Student|null
  217.      *
  218.      * @ORM\ManyToOne(
  219.      *     targetEntity = "Products\NotificationsBundle\Entity\Student",
  220.      * )
  221.      * @ORM\JoinColumn(
  222.      *     name = "student",
  223.      *     referencedColumnName = "id",
  224.      *     nullable = true,
  225.      *     onDelete = "SET NULL",
  226.      * )
  227.      */
  228.     protected ?Student $student null;
  229.     /**
  230.      * @var Profile|null
  231.      *
  232.      * @ORM\ManyToOne(
  233.      *     targetEntity = "Products\NotificationsBundle\Entity\Profile",
  234.      * )
  235.      * @ORM\JoinColumn(
  236.      *     name = "profile",
  237.      *     referencedColumnName = "id",
  238.      *     nullable = true,
  239.      *     onDelete = "SET NULL",
  240.      * )
  241.      */
  242.     protected ?Profile $profile null;
  243.     /**
  244.      * @return AbstractRecipient|null
  245.      */
  246.     public function getRecipient(): ?AbstractRecipient
  247.     {
  248.         return $this->recipient;
  249.     }
  250.     /**
  251.      * @param AbstractRecipient $recipient
  252.      * @return $this
  253.      */
  254.     public function setRecipient(AbstractRecipient $recipient): self
  255.     {
  256.         $this->recipient $recipient;
  257.         return $this;
  258.     }
  259.     /**
  260.      * @return bool
  261.      */
  262.     public function isPending(): bool
  263.     {
  264.         return in_array($this->getEvent(), static::PENDING_STATUSES);
  265.     }
  266.     /**
  267.      * @return bool
  268.      */
  269.     public function isSuccessful(): bool
  270.     {
  271.         return in_array($this->getEvent(), static::SUCCESSFUL_STATUSES);
  272.     }
  273.     /**
  274.      * @return bool
  275.      */
  276.     public function isFailed(): bool
  277.     {
  278.         return in_array($this->getEvent(), static::FAILED_STATUSES);
  279.     }
  280.     /**
  281.      * @return DateInterval
  282.      */
  283.     public function getTimespan(): DateInterval
  284.     {
  285.         $start $this->getTriggeredAt();
  286.         if (empty($start)) {
  287.             $start DateTimeUtils::now();
  288.         }
  289.         return date_diff($start$this->getCreatedAt());
  290.     }
  291.     /**
  292.      * @return DateTime|null
  293.      */
  294.     public function getTriggeredAt(): ?DateTime
  295.     {
  296.         return $this->triggeredAt;
  297.     }
  298.     /**
  299.      * @param DateTime $triggeredAt
  300.      * @return $this
  301.      */
  302.     public function setTriggeredAt(DateTime $triggeredAt): self
  303.     {
  304.         $this->triggeredAt $triggeredAt;
  305.         return $this;
  306.     }
  307.     /**
  308.      * @return string|null
  309.      */
  310.     public function getExternalId(): ?string
  311.     {
  312.         return $this->externalId;
  313.     }
  314.     /**
  315.      * @param string $externalId
  316.      * @return $this
  317.      */
  318.     public function setExternalId(string $externalId): self
  319.     {
  320.         $this->externalId $externalId;
  321.         return $this;
  322.     }
  323.     /**
  324.      * @return array|null
  325.      */
  326.     public function getError(): ?array
  327.     {
  328.         return $this->error ?: null;
  329.     }
  330.     /**
  331.      * @param array|null $error
  332.      * @return $this
  333.      */
  334.     public function setError(?array $error): self
  335.     {
  336.         $this->error $error ?: null;
  337.         return $this;
  338.     }
  339.     /**
  340.      * @return string|null
  341.      */
  342.     public function getEvent(): ?string
  343.     {
  344.         return $this->event;
  345.     }
  346.     /**
  347.      * @param string $event
  348.      * @return $this
  349.      */
  350.     public function setEvent(string $event): self
  351.     {
  352.         $this->event $event;
  353.         return $this;
  354.     }
  355.     /**
  356.      * @return AbstractNotification
  357.      */
  358.     public function getMessage(): AbstractNotification
  359.     {
  360.         return $this->message;
  361.     }
  362.     /**
  363.      * @return Job
  364.      */
  365.     public function getJob(): Job
  366.     {
  367.         return $this->job;
  368.     }
  369.     /**
  370.      * @param Job $job
  371.      * @return $this
  372.      */
  373.     public function setJob(Job $job): self
  374.     {
  375.         $this->job $job;
  376.         $this->message $job->getMessage();// forcing the message to be tied to the job
  377.         return $this;
  378.     }
  379.     /**
  380.      * @return array|string[]
  381.      */
  382.     public function getExtras(): array
  383.     {
  384.         return [];
  385.     }
  386.     /**
  387.      * @return int|null
  388.      */
  389.     public function getApiWait(): ?int
  390.     {
  391.         return $this->apiWait;
  392.     }
  393.     /**
  394.      * @param int $apiWait
  395.      * @return $this
  396.      */
  397.     public function setApiWait(int $apiWait): self
  398.     {
  399.         $this->apiWait $apiWait;
  400.         return $this;
  401.     }
  402.     /**
  403.      * @return Student|null
  404.      */
  405.     public function getStudent(): ?Student
  406.     {
  407.         return $this->student;
  408.     }
  409.     /**
  410.      * @param Student|null $student
  411.      * @return self
  412.      */
  413.     public function setStudent(?Student $student): self
  414.     {
  415.         $this->student $student;
  416.         return $this;
  417.     }
  418.     /**
  419.      * @return Profile|null
  420.      */
  421.     public function getProfile(): ?Profile
  422.     {
  423.         return $this->profile;
  424.     }
  425.     /**
  426.      * @param Profile|null $profile
  427.      * @return self
  428.      */
  429.     public function setProfile(?Profile $profile): self
  430.     {
  431.         $this->profile $profile;
  432.         return $this;
  433.     }
  434. }