modules/CDev/PINCodes/src/Model/PinCode.php line 94

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace CDev\PINCodes\Model;
  7. use ApiPlatform\Core\Annotation as ApiPlatform;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use CDev\PINCodes\API\Endpoint\ProductPINCode\DTO\ProductPINCodeInput as Input;
  10. use CDev\PINCodes\API\Endpoint\ProductPINCode\DTO\ProductPINCodeOutput as Output;
  11. /**
  12.  * PIN Code
  13.  *
  14.  * @ORM\Entity
  15.  * @ORM\Table  (
  16.  *      name="pin_codes",
  17.  *      uniqueConstraints={
  18.  *          @ORM\UniqueConstraint (name="productCode", columns={"code", "productId"})
  19.  *      }
  20.  * )
  21.  * @ApiPlatform\ApiResource(
  22.  *     shortName="Product PIN Code",
  23.  *     input=Input::class,
  24.  *     output=Output::class,
  25.  *     itemOperations={
  26.  *          "get"={
  27.  *              "method"="GET",
  28.  *              "path"="/products/{product_id}/pin_codes/{id}.{_format}",
  29.  *              "identifiers"={"product_id", "id"},
  30.  *              "requirements"={"product_id"="\d+", "id"="\d+"},
  31.  *              "openapi_context"={
  32.  *                  "parameters"={
  33.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  34.  *                      {"name"="id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  35.  *                  }
  36.  *              }
  37.  *          },
  38.  *          "put"={
  39.  *              "method"="PUT",
  40.  *              "path"="/products/{product_id}/pin_codes/{id}.{_format}",
  41.  *              "identifiers"={"product_id", "id"},
  42.  *              "requirements"={"product_id"="\d+", "id"="\d+"},
  43.  *              "openapi_context"={
  44.  *                  "parameters"={
  45.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  46.  *                      {"name"="id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  47.  *                  }
  48.  *              }
  49.  *          },
  50.  *          "delete"={
  51.  *              "method"="DELETE",
  52.  *              "path"="/products/{product_id}/pin_codes/{id}.{_format}",
  53.  *              "identifiers"={"product_id", "id"},
  54.  *              "requirements"={"product_id"="\d+", "id"="\d+"},
  55.  *              "openapi_context"={
  56.  *                  "parameters"={
  57.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  58.  *                      {"name"="id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  59.  *                  }
  60.  *              }
  61.  *          }
  62.  *     },
  63.  *     collectionOperations={
  64.  *          "get"={
  65.  *              "method"="GET",
  66.  *              "path"="/products/{product_id}/pin_codes.{_format}",
  67.  *              "identifiers"={"product_id"},
  68.  *              "requirements"={"product_id"="\d+"},
  69.  *              "openapi_context"={
  70.  *                  "parameters"={
  71.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  72.  *                  },
  73.  *              }
  74.  *          },
  75.  *          "post"={
  76.  *              "method"="POST",
  77.  *              "path"="/products/{product_id}/pin_codes.{_format}",
  78.  *              "controller"="xcart.api.xc.pin_codes.product_pin_code.controller",
  79.  *              "identifiers"={"product_id"},
  80.  *              "requirements"={"product_id"="\d+"},
  81.  *              "openapi_context"={
  82.  *                  "parameters"={
  83.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  84.  *                  }
  85.  *              }
  86.  *          }
  87.  *     }
  88.  * )
  89.  */
  90. class PinCode extends \XLite\Model\AEntity
  91. {
  92.     /**
  93.      * PIN Code unique id
  94.      *
  95.      * @var integer
  96.      *
  97.      * @ORM\Id
  98.      * @ORM\GeneratedValue (strategy="AUTO")
  99.      * @ORM\Column         (type="integer")
  100.      */
  101.     protected $id;
  102.     /**
  103.      * Code
  104.      *
  105.      * @var string
  106.      *
  107.      * @ORM\Column (type="string", length=64)
  108.      */
  109.     protected $code '';
  110.     /**
  111.      * Create date
  112.      *
  113.      * @var integer
  114.      *
  115.      * @ORM\Column (type="integer")
  116.      */
  117.     protected $createDate;
  118.     /**
  119.      * Is sold
  120.      *
  121.      * @var boolean
  122.      *
  123.      * @ORM\Column (type="boolean")
  124.      */
  125.     protected $isSold false;
  126.     /**
  127.      * Is blocked
  128.      *
  129.      * @var boolean
  130.      *
  131.      * @ORM\Column (type="boolean")
  132.      */
  133.     protected $isBlocked false;
  134.     /**
  135.      * Product (relation)
  136.      *
  137.      * @var \XLite\Model\Product
  138.      *
  139.      * @ORM\ManyToOne  (targetEntity="XLite\Model\Product", inversedBy="pinCodes")
  140.      * @ORM\JoinColumn (name="productId", referencedColumnName="product_id", onDelete="SET NULL")
  141.      */
  142.     protected $product;
  143.     /**
  144.      * OrderItem (relation)
  145.      *
  146.      * @var \XLite\Model\OrderItem
  147.      *
  148.      * @ORM\ManyToOne  (targetEntity="XLite\Model\OrderItem", inversedBy="pinCodes")
  149.      * @ORM\JoinColumn (name="orderItemId", referencedColumnName="item_id", onDelete="SET NULL")
  150.      */
  151.     protected $orderItem;
  152.     /**
  153.      * Generate pin code
  154.      *
  155.      * @return string
  156.      *
  157.      * @throws \Exception on attempt to autogenerate without $product defined
  158.      */
  159.     public function generateCode()
  160.     {
  161.         if (!$this->getProduct()) {
  162.             throw new \Exception('Can not ensure pin uniqueness without a product assigned to this pin code');
  163.         }
  164.         $newValue null;
  165.         $repo \XLite\Core\Database::getRepo('CDev\PINCodes\Model\PinCode');
  166.         while (!$newValue || $repo->findOneBy(['code' => $newValue'product' => $this->getProduct()->getId()])) {
  167.             $newValue $this->getRandomCode();
  168.         }
  169.         $this->code $newValue;
  170.         return $this->code;
  171.     }
  172.     /**
  173.      * Generates random pin code
  174.      *
  175.      * @return string
  176.      */
  177.     protected function getRandomCode()
  178.     {
  179.         return sprintf('%04d%04d%04d%04d'mt_rand(09999), mt_rand(09999), mt_rand(09999), mt_rand(09999));
  180.     }
  181.     /**
  182.      * Get id
  183.      *
  184.      * @return integer
  185.      */
  186.     public function getId()
  187.     {
  188.         return $this->id;
  189.     }
  190.     /**
  191.      * Set code
  192.      *
  193.      * @param string $code
  194.      * @return PinCode
  195.      */
  196.     public function setCode($code)
  197.     {
  198.         $this->code $code;
  199.         return $this;
  200.     }
  201.     /**
  202.      * Get code
  203.      *
  204.      * @return string
  205.      */
  206.     public function getCode()
  207.     {
  208.         return $this->code;
  209.     }
  210.     /**
  211.      * Set createDate
  212.      *
  213.      * @param integer $createDate
  214.      * @return PinCode
  215.      */
  216.     public function setCreateDate($createDate)
  217.     {
  218.         $this->createDate $createDate;
  219.         return $this;
  220.     }
  221.     /**
  222.      * Get createDate
  223.      *
  224.      * @return integer
  225.      */
  226.     public function getCreateDate()
  227.     {
  228.         return $this->createDate;
  229.     }
  230.     /**
  231.      * Set isSold
  232.      *
  233.      * @param boolean $isSold
  234.      * @return PinCode
  235.      */
  236.     public function setIsSold($isSold)
  237.     {
  238.         $this->isSold $isSold;
  239.         return $this;
  240.     }
  241.     /**
  242.      * Get isSold
  243.      *
  244.      * @return boolean
  245.      */
  246.     public function getIsSold()
  247.     {
  248.         return $this->isSold;
  249.     }
  250.     /**
  251.      * Set isBlocked
  252.      *
  253.      * @param boolean $isBlocked
  254.      * @return PinCode
  255.      */
  256.     public function setIsBlocked($isBlocked)
  257.     {
  258.         $this->isBlocked $isBlocked;
  259.         return $this;
  260.     }
  261.     /**
  262.      * Get isBlocked
  263.      *
  264.      * @return boolean
  265.      */
  266.     public function getIsBlocked()
  267.     {
  268.         return $this->isBlocked;
  269.     }
  270.     /**
  271.      * Set product
  272.      *
  273.      * @param \XLite\Model\Product $product
  274.      * @return PinCode
  275.      */
  276.     public function setProduct(\XLite\Model\Product $product null)
  277.     {
  278.         $this->product $product;
  279.         return $this;
  280.     }
  281.     /**
  282.      * Get product
  283.      *
  284.      * @return \XLite\Model\Product
  285.      */
  286.     public function getProduct()
  287.     {
  288.         return $this->product;
  289.     }
  290.     /**
  291.      * Set orderItem
  292.      *
  293.      * @param \XLite\Model\OrderItem $orderItem
  294.      * @return PinCode
  295.      */
  296.     public function setOrderItem(\XLite\Model\OrderItem $orderItem null)
  297.     {
  298.         $this->orderItem $orderItem;
  299.         return $this;
  300.     }
  301.     /**
  302.      * Get orderItem
  303.      *
  304.      * @return \XLite\Model\OrderItem
  305.      */
  306.     public function getOrderItem()
  307.     {
  308.         return $this->orderItem;
  309.     }
  310. }