src/Cms/CoreBundle/Entity/OneRosterJob.php line 24

Open in your IDE?
  1. <?php
  2. namespace Cms\CoreBundle\Entity;
  3. use Cms\TenantBundle\Entity\TenantedEntity;
  4. use DateTime;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\Common\Collections\Criteria;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Reinder83\BinaryFlags\Bits;
  10. /**
  11.  * Class OneRosterJob
  12.  * @package Cms\CoreBundle\Entity
  13.  *
  14.  * @ORM\Entity(
  15.  *     repositoryClass = "Cms\CoreBundle\Doctrine\OneRosterJobRepository"
  16.  * )
  17.  * @ORM\Table(
  18.  *      name = "cms__one_roster__job",
  19.  * )
  20.  */
  21. class OneRosterJob extends TenantedEntity
  22. {
  23.     const PHASES = [
  24.         'init' => self::PHASES__INIT,
  25.         'stash' => self::PHASES__STASH,
  26.         'fix' => self::PHASES__FIX,
  27.         'prepare' => self::PHASES__PREPARE,
  28.         'process' => self::PHASES__PROCESS,
  29.         'link' => self::PHASES__LINK,
  30.         'tweak' => self::PHASES__TWEAK,
  31.         'tidy' => self::PHASES__TIDY,
  32.     ];
  33.     const PHASES__INIT Bits::BIT_1;
  34.     const PHASES__STASH Bits::BIT_2;
  35.     const PHASES__FIX Bits::BIT_3;
  36.     const PHASES__PREPARE Bits::BIT_4;
  37.     const PHASES__PROCESS Bits::BIT_5;
  38.     const PHASES__LINK Bits::BIT_6;
  39.     const PHASES__TWEAK Bits::BIT_7;
  40.     const PHASES__TIDY Bits::BIT_8;
  41.     const STATUSES__READY 0;
  42.     const STATUSES__RUNNING Bits::BIT_1;
  43.     const STATUSES__COMPLETE Bits::BIT_2;
  44.     const STATUSES__ERROR = ~0;
  45.     /**
  46.      * @var OneRosterSync|null
  47.      *
  48.      * @ORM\ManyToOne(
  49.      *     targetEntity = "Cms\CoreBundle\Entity\OneRosterSync",
  50.      *     inversedBy = "jobs",
  51.      * )
  52.      * @ORM\JoinColumn(
  53.      *     name = "sync",
  54.      *     referencedColumnName = "id",
  55.      *     nullable = false,
  56.      *     onDelete = "CASCADE",
  57.      * )
  58.      */
  59.     protected ?OneRosterSync $sync null;
  60.     /**
  61.      * @var Collection|OneRosterLog[]
  62.      *
  63.      * @ORM\OneToMany(
  64.      *     targetEntity = "Cms\CoreBundle\Entity\OneRosterLog",
  65.      *     mappedBy = "job",
  66.      *     fetch = "EXTRA_LAZY",
  67.      * )
  68.      * @ORM\OrderBy({
  69.      *     "createdAt" = "DESC",
  70.      * })
  71.      */
  72.     protected Collection $logs;
  73.     /**
  74.      * @var int
  75.      *
  76.      * @ORM\Column(
  77.      *     type = "integer",
  78.      *     nullable = false,
  79.      *     options = {
  80.      *         "default" = self::STATUSES__READY,
  81.      *     },
  82.      * )
  83.      */
  84.     protected int $status self::STATUSES__READY;
  85.     /**
  86.      * @var int
  87.      *
  88.      * @ORM\Column(
  89.      *     type = "integer",
  90.      *     nullable = false,
  91.      *     options = {
  92.      *         "default" = 0,
  93.      *     },
  94.      * )
  95.      */
  96.     protected int $startPhase 0;
  97.     /**
  98.      * @var int
  99.      *
  100.      * @ORM\Column(
  101.      *     type = "integer",
  102.      *     nullable = false,
  103.      *     options = {
  104.      *         "default" = 0,
  105.      *     },
  106.      * )
  107.      */
  108.     protected int $phasesReady 0;
  109.     /**
  110.      * @var int
  111.      *
  112.      * @ORM\Column(
  113.      *     type = "integer",
  114.      *     nullable = false,
  115.      *     options = {
  116.      *         "default" = 0,
  117.      *     },
  118.      * )
  119.      */
  120.     protected int $phasesRunning 0;
  121.     /**
  122.      * @var int
  123.      *
  124.      * @ORM\Column(
  125.      *     type = "integer",
  126.      *     nullable = false,
  127.      *     options = {
  128.      *         "default" = 0,
  129.      *     },
  130.      * )
  131.      */
  132.     protected int $phasesComplete 0;
  133.     /**
  134.      * @var DateTime|null
  135.      *
  136.      * @ORM\Column(
  137.      *     type = "datetime",
  138.      *     nullable = true,
  139.      * )
  140.      */
  141.     protected ?DateTime $firstActivityAt null;
  142.     /**
  143.      * @var DateTime|null
  144.      *
  145.      * @ORM\Column(
  146.      *     type = "datetime",
  147.      *     nullable = true,
  148.      * )
  149.      */
  150.     protected ?DateTime $lastActivityAt null;
  151.     /**
  152.      * Used to track progress of the syncing process.
  153.      * This number reflects the number of outstanding MQ messages that are related to this sync.
  154.      * This number should ALWAYS be updated atomically via methods like bulk UPDATE queries.
  155.      *
  156.      * @var int
  157.      *
  158.      * @ORM\Column(
  159.      *     type = "integer",
  160.      *     nullable = false,
  161.      *     options = {
  162.      *         "default" = 0,
  163.      *     },
  164.      * )
  165.      */
  166.     protected int $semaphore 0;
  167.     /**
  168.      * @var array
  169.      *
  170.      * @ORM\Column(
  171.      *     type = "json",
  172.      *     nullable = true,
  173.      * )
  174.      */
  175.     protected array $error = [];
  176.     /**
  177.      * @var Collection|AbstractOneRosterEntity[]
  178.      *
  179.      * @ORM\OneToMany(
  180.      *     targetEntity = AbstractOneRosterEntity::class,
  181.      *     mappedBy = "job",
  182.      * )
  183.      */
  184.     protected Collection $oneRosterEntities;
  185.     /**
  186.      *
  187.      */
  188.     public function __construct()
  189.     {
  190.         $this->logs = new ArrayCollection();
  191.     }
  192.     /**
  193.      * @return OneRosterSync
  194.      */
  195.     public function getSync(): OneRosterSync
  196.     {
  197.         return $this->sync;
  198.     }
  199.     /**
  200.      * @param OneRosterSync $sync
  201.      * @return $this
  202.      */
  203.     public function setSync(OneRosterSync $sync): self
  204.     {
  205.         $this->sync $sync;
  206.         return $this;
  207.     }
  208.     /**
  209.      * @return int
  210.      */
  211.     public function getStartPhase(): int
  212.     {
  213.         return $this->startPhase;
  214.     }
  215.     /**
  216.      * @return string|null
  217.      */
  218.     public function getStartPhaseName(): ?string
  219.     {
  220.         if ( ! $this->startPhase) {
  221.             return null;
  222.         }
  223.         return array_search($this->startPhaseself::PHASES);
  224.     }
  225.     /**
  226.      * @param int $startPhase
  227.      * @return $this
  228.      */
  229.     public function setStartPhase(int $startPhase): self
  230.     {
  231.         $this->startPhase $startPhase;
  232.         return $this;
  233.     }
  234.     /**
  235.      * @return int
  236.      */
  237.     public function getPhasesReady(): int
  238.     {
  239.         return $this->phasesReady;
  240.     }
  241.     /**
  242.      * @return int
  243.      */
  244.     public function getPhasesRunning(): int
  245.     {
  246.         return $this->phasesRunning;
  247.     }
  248.     /**
  249.      * @return int
  250.      */
  251.     public function getPhasesComplete(): int
  252.     {
  253.         return $this->phasesComplete;
  254.     }
  255.     /**
  256.      * @return int|null
  257.      */
  258.     public function getCurrentPhase(): ?int
  259.     {
  260.         foreach (array_reverse(self::PHASES) as $phase) {
  261.             if (($this->getPhasesRunning() & $phase) === $phase) {
  262.                 return $phase;
  263.             }
  264.         }
  265.         return null;
  266.     }
  267.     /**
  268.      * @return DateTime|null
  269.      */
  270.     public function getFirstActivityAt(): ?DateTime
  271.     {
  272.         return $this->firstActivityAt;
  273.     }
  274.     /**
  275.      * @return DateTime|null
  276.      */
  277.     public function getLastActivityAt(): ?DateTime
  278.     {
  279.         return $this->lastActivityAt;
  280.     }
  281.     /**
  282.      * @return int
  283.      */
  284.     public function getSemaphore(): int
  285.     {
  286.         return $this->semaphore;
  287.     }
  288.     /**
  289.      * @return string
  290.      */
  291.     public function getIdentifier(): string
  292.     {
  293.         return implode('::', [
  294.             $this->getSync()->getId(),
  295.             $this->getId()
  296.         ]);
  297.     }
  298.     /**
  299.      * @param Criteria|null $criteria
  300.      * @return Collection|OneRosterLog[]
  301.      */
  302.     public function getLogs(?Criteria $criteria null): Collection
  303.     {
  304.         if ( ! $this->logs instanceof Collection) {
  305.             $this->logs = new ArrayCollection();
  306.         }
  307.         if ( ! empty($criteria)) {
  308.             return $this->logs->matching($criteria);
  309.         }
  310.         return $this->logs;
  311.     }
  312.     /**
  313.      * @param Criteria|null $criteria
  314.      * @return int
  315.      */
  316.     public function countLogs(?Criteria $criteria null): int
  317.     {
  318.         return $this->getLogs($criteria)->count();
  319.     }
  320.     /**
  321.      * @return int
  322.      */
  323.     public function getStatus(): int
  324.     {
  325.         return $this->status;
  326.     }
  327.     /**
  328.      * @param int $status
  329.      * @return bool
  330.      */
  331.     public function isStatus(int $status): bool
  332.     {
  333.         if ($status === 0) {
  334.             return ($this->getStatus() === $status);
  335.         } else if ($status 0) {
  336.             if ($this->getStatus() < 0) {
  337.                 return false;
  338.             }
  339.             return (($this->getStatus() & $status) === $status);
  340.         } else {
  341.             if ($this->getStatus() > 0) {
  342.                 return false;
  343.             }
  344.             return ((~$this->getStatus() & ~$status) === ~$status);
  345.         }
  346.     }
  347.     /**
  348.      * @param int $status
  349.      * @return $this
  350.      */
  351.     public function setStatus(int $status): self
  352.     {
  353.         $this->status $status;
  354.         return $this;
  355.     }
  356.     /**
  357.      * @return bool
  358.      */
  359.     public function isReady(): bool
  360.     {
  361.         return $this->isStatus(self::STATUSES__READY);
  362.     }
  363.     /**
  364.      * @return bool
  365.      */
  366.     public function isRunning(): bool
  367.     {
  368.         return $this->isStatus(self::STATUSES__RUNNING);
  369.     }
  370.     /**
  371.      * @return bool
  372.      */
  373.     public function isComplete(): bool
  374.     {
  375.         return $this->isStatus(self::STATUSES__COMPLETE);
  376.     }
  377.     /**
  378.      * @return bool
  379.      */
  380.     public function isError(): bool
  381.     {
  382.         return ($this->getStatus() < 0);
  383.     }
  384.     /**
  385.      * @return array
  386.      */
  387.     public function getError(): array
  388.     {
  389.         return $this->error;
  390.     }
  391.     /**
  392.      * @param array $error
  393.      * @return $this
  394.      */
  395.     public function setError(array $error): self
  396.     {
  397.         $this->error $error;
  398.         return $this;
  399.     }
  400.     /**
  401.      * @return Collection
  402.      */
  403.     public function getOneRosterEntities(): Collection
  404.     {
  405.         return $this->oneRosterEntities;
  406.     }
  407.     /**
  408.      * @param Collection $oneRosterEntities
  409.      * @return self
  410.      */
  411.     public function setOneRosterEntities(Collection $oneRosterEntities): self
  412.     {
  413.         $this->oneRosterEntities $oneRosterEntities;
  414.         return $this;
  415.     }
  416. }