src/AtechNet/CoreBundle/Entity/OrderComment.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\AtechNet\CoreBundle\Entity;
  3. use App\AtechNet\CoreBundle\Enum\OrderErrorComment\OrderErrorCategories;
  4. use App\AtechNet\CoreBundle\Enum\OrderErrorComment\OrderErrorTypes;
  5. use Doctrine\ORM\Mapping\Column;
  6. use Doctrine\ORM\Mapping\Entity;
  7. use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
  8. use Doctrine\ORM\Mapping\JoinColumn;
  9. use Doctrine\ORM\Mapping\ManyToOne;
  10. use Doctrine\ORM\Mapping\Table;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. use function PHPUnit\Framework\isNan;
  13. /**
  14.  * OrderComment
  15.  *
  16.  * @Table(name="order_comment")
  17.  * @Entity
  18.  * @HasLifecycleCallbacks
  19.  */
  20. class OrderComment extends AbstractSignedEntity
  21. {
  22.     public const int TYPE_FOR_PREPARATION = 1;
  23.     public const int TYPE_FOR_PACKING = 2;
  24.     public const int TYPE_FOR_INSTALLATION = 3;
  25.     public const int TYPE_FOR_LABELS = 4;
  26.     public const int TYPE_ERROR = 5;
  27.     /**
  28.      * @return array
  29.      */
  30.     public static function getTypeChoices(): array
  31.     {
  32.         return [
  33.             self::TYPE_FOR_PREPARATION  => 'for-preparation',
  34.             self::TYPE_FOR_PACKING      => 'for-packing',
  35.             self::TYPE_FOR_INSTALLATION => 'for-installation',
  36.             self::TYPE_FOR_LABELS       => 'for-labels',
  37.             self::TYPE_ERROR            => 'error',
  38.         ];
  39.     }
  40.     /**
  41.      * @var Order
  42.      *
  43.      * @Assert\NotNull
  44.      *
  45.      * @ManyToOne(targetEntity="Order", inversedBy="comments")
  46.      * @JoinColumn(name="order_id", referencedColumnName="id")
  47.      */
  48.     private $order;
  49.     /**
  50.      * @var int
  51.      *
  52.      * @Column(name="type", type="integer", nullable=true)
  53.      */
  54.     private $type;
  55.     /**
  56.      * @var string
  57.      *
  58.      * @Column(name="text", type="text", nullable=true)
  59.      */
  60.     private $text;
  61.     /**
  62.      * @var User
  63.      *
  64.      * @ManyToOne(targetEntity="User")
  65.      * @JoinColumn(name="done_by", referencedColumnName="id", nullable=true)
  66.      */
  67.     private $doneBy;
  68.     /**
  69.      * @var \DateTime
  70.      *
  71.      * @Column(name="done_time", type="datetime", nullable=true)
  72.      */
  73.     private $doneTime;
  74.     /**
  75.      * @param User $user
  76.      *
  77.      * @return bool
  78.      */
  79.     public function isDeletableByUser(User $user)
  80.     {
  81.         return !is_null($this->getCreatedBy()) && $this->getCreatedBy()->getId() == $user->getId();
  82.     }
  83.     public function isDone()
  84.     {
  85.         return $this->doneTime != null;
  86.     }
  87.     /**
  88.      * @return Order
  89.      */
  90.     public function getOrder()
  91.     {
  92.         return $this->order;
  93.     }
  94.     /**
  95.      * @param Order $order
  96.      */
  97.     public function setOrder($order)
  98.     {
  99.         $this->order = $order;
  100.     }
  101.     /**
  102.      * @return string
  103.      */
  104.     public function getText()
  105.     {
  106.         return $this->text;
  107.     }
  108.     /**
  109.      * @param string $text
  110.      */
  111.     public function setText($text)
  112.     {
  113.         $this->text = $text;
  114.     }
  115.     /**
  116.      * @return User
  117.      */
  118.     public function getDoneBy()
  119.     {
  120.         return $this->doneBy;
  121.     }
  122.     /**
  123.      * @param User $doneBy
  124.      */
  125.     public function setDoneBy($doneBy)
  126.     {
  127.         $this->doneBy = $doneBy;
  128.     }
  129.     /**
  130.      * @return \DateTime
  131.      */
  132.     public function getDoneTime()
  133.     {
  134.         return $this->doneTime;
  135.     }
  136.     /**
  137.      * @param \DateTime $doneTime
  138.      */
  139.     public function setDoneTime($doneTime)
  140.     {
  141.         $this->doneTime = $doneTime;
  142.     }
  143.     /**
  144.      * @return int|null
  145.      */
  146.     public function getType(): ?int
  147.     {
  148.         return $this->type;
  149.     }
  150.     /**
  151.      * @param int|null $type
  152.      */
  153.     public function setType(?int $type)
  154.     {
  155.         if (!isset(self::getTypeChoices()[$type])) {
  156.             throw new \InvalidArgumentException("Invalid comment type value " . $type);
  157.         }
  158.         $this->type = $type;
  159.     }
  160.     /**
  161.      * @param string $rawText
  162.      *
  163.      * @return string
  164.      */
  165.     public function convertAndSetErrorTypeCommentToIntegers(string $rawText): string
  166.     {
  167.         preg_match('/\{([^}]+)\}/', $rawText, $categoryMatch);
  168.         $category = $categoryMatch[1] ?? null;
  169.         preg_match_all('/\[([^\]]+)\]/', $rawText, $itemMatches);
  170.         $items = $itemMatches[1] ?? [];
  171.         preg_match_all('/\|([^|]+)\|/', $rawText, $otherComments);
  172.         $otherComment = $otherComments[1][0] ?? null;
  173.         $dbString = '';
  174.         if (!is_null($category)) {
  175.             $dbString = "{" . OrderErrorCategories::fromDisplayName($category) . "}";
  176.         }
  177.         foreach ($items as $item) {
  178.             if (!is_null($otherComment) && $item == 'Other') {
  179.                 $dbString .= "[" . OrderErrorTypes::fromDisplayName($item) . "] | " . trim($otherComment) . " |";
  180.             }
  181.             else {
  182.                 $dbString .= "[" . OrderErrorTypes::fromDisplayName($item) . "]";
  183.             }
  184.         }
  185.         return $dbString == '' ? $rawText : $dbString;
  186.     }
  187.     /**
  188.      * @return string
  189.      */
  190.     public function convertErrorTypeOrderToStrings(): string
  191.     {
  192.         $rawText = $this->getText();
  193.         preg_match('/\{([^}]+)\}/', $rawText, $categoryMatch);
  194.         $category = $categoryMatch[1] ?? null;
  195.         preg_match_all('/\[([^\]]+)\]/', $rawText, $itemMatches);
  196.         $items = $itemMatches[1] ?? [];
  197.         preg_match_all('/\|([^|]+)\|/', $rawText, $otherComments);
  198.         $otherComment = $otherComments[1][0] ?? null;
  199.         $uiString = '';
  200.         if (!is_null($category)) {
  201.             $uiString = "Category: {" . OrderErrorCategories::fromValueToDisplayName($category) . "}<br>";
  202.         }
  203.         foreach ($items as $item) {
  204.             if (!is_null($otherComment) && in_array($item, OrderErrorTypes::getAllOtherCategories())) {
  205.                 $uiString .= "[" . OrderErrorTypes::fromValueToDisplayName($item) . "] | " . $otherComment . " |";
  206.             }
  207.             else {
  208.                 $uiString .= "[" . OrderErrorTypes::fromValueToDisplayName($item) . "]";
  209.             }
  210.         }
  211.         return $uiString == '' ? $rawText : $uiString;
  212.     }
  213. }