Advertisement
Guest User

Untitled

a guest
May 31st, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.52 KB | None | 0 0
  1. <?php
  2. /**
  3.  *
  4.  * User: radims
  5.  * Date: 08.03.16
  6.  * Time: 11:53
  7.  */
  8.  
  9. namespace App\Services\ActivationValidator;
  10.  
  11.  
  12.  
  13. use App\Model\DB\Activation\Activation;
  14. use App\Model\DB\Activation\ActivationMapper;
  15. use App\Model\DB\Activation\ActivationSchema;
  16. use App\Model\DB\Order\OrderSchema;
  17. use App\Services\IValidator;
  18. use Nette\Object;
  19. use Nette\Security\User;
  20. use Qest\DbLayer\Mapper;
  21.  
  22.  
  23.  
  24. class ActivationValidator extends Object implements IValidator{
  25.  
  26.  
  27.  
  28.     /**
  29.      * Validuje nový insert update nějakého updatu
  30.      *
  31.      * @param ActivationMapper $activationMapper
  32.      * @param array $values
  33.      * @param $entityOrId
  34.      * @param User|null $user
  35.      *
  36.      * @return bool|void
  37.      * @throws ActivationValidatorException
  38.      */
  39.     public function validate(Mapper $activationMapper,array $values,$entityOrId , User $user = null) {
  40.  
  41.         //společná validační pravidla
  42.         $this->validateCommon($activationMapper, $values, $entityOrId, $user);
  43.  
  44.         //pokud ukládám stávající entitu
  45.         if ($entityOrId)
  46.             return $this->validateUpdateEntity($activationMapper, $values, $entityOrId, $user);
  47.         // vytvářím novou entitu
  48.         else
  49.             return $this->validateInsertEntity($activationMapper, $values, $entityOrId, $user);
  50.     }
  51.  
  52.     /**
  53.      * Kontroluje se zda se provádí update se všemi hodnotami
  54.      *
  55.      * @param array $values
  56.      *
  57.      * @throws ActivationValidatorException
  58.      */
  59.     public function hasAllValues(array $values) {
  60.         if(!isset($values["order_id"]))
  61.             throw new ActivationValidatorException("Order id is not set");
  62.  
  63.         if(!isset($values["state"]))
  64.             throw new ActivationValidatorException("State is not set");
  65.  
  66.         if(!isset($values["at_id"]))
  67.             throw new ActivationValidatorException("Activation technician is not set");
  68.  
  69.         if(!isset($values["realization_start"]))
  70.             throw new ActivationValidatorException("Realization start is not set");
  71.  
  72.         if(!isset($values["realization_end"]))
  73.             throw new ActivationValidatorException("Realization end is not set");
  74.  
  75.  
  76.  
  77.  
  78.         return true;
  79.     }
  80.  
  81.     /**
  82.      * Validuje aktivaci pro insert i update
  83.      *
  84.      * @param ActivationMapper $activationMapper
  85.      * @param array $values
  86.      * @param $entityOrId
  87.      * @param User|null $user
  88.      *
  89.      * @return bool
  90.      * @throws ActivationValidatorException
  91.      */
  92.     private function validateCommon(ActivationMapper $activationMapper,array $values,$entityOrId , User $user = null) {
  93.         if (!$this->hasAllValues($values)) {
  94.             throw new ActivationValidatorException("All values fail");
  95.         }
  96.         if ($this->isActivationOfOrderAlreadyExist($activationMapper, $values,$entityOrId)) {
  97.             throw new ActivationValidatorException("There is some other activation in state 'scheduled'");
  98.         }
  99.  
  100.         if ($this->hasUserMoreActivation($activationMapper, $values,$entityOrId)) {
  101.             throw new ActivationValidatorException("User has some activation in this time");
  102.         }
  103.  
  104.         return true;
  105.     }
  106.  
  107.  
  108.  
  109.     /**
  110.      * Validuje update entity
  111.      *
  112.      * @param $activationMapper
  113.      * @param $values
  114.      * @param $entityOrId
  115.      * @param $user
  116.      *
  117.      * @return bool
  118.      * @throws ActivationValidatorException
  119.      */
  120.     private function validateUpdateEntity($activationMapper, $values, $entityOrId, $user = null) {
  121.         if (!$this->hasSameValueOrder($activationMapper, $values, $entityOrId)) {
  122.             throw new ActivationValidatorException("Order id canot be changed");
  123.         }
  124.  
  125.         if (!$this->inStateScheduled($activationMapper,$values,$entityOrId)) {
  126.             throw new ActivationValidatorException("Activation can be changed only in scheduled state");
  127.         }
  128.  
  129.         return true;
  130.     }
  131.  
  132.     /**
  133.      * Validuje vložení nové entity
  134.      *
  135.      * @param ActivationMapper $activationMapper
  136.      * @param array $values
  137.      * @param $entityOrId
  138.      * @param User $user
  139.      *
  140.      * @return bool
  141.      * @throws ActivationValidatorException
  142.      */
  143.     private function validateInsertEntity(ActivationMapper $activationMapper,array $values, $entityOrId, User $user = null) {
  144.         if (!$this->isOrderStateScheduled($activationMapper, $values)) {
  145.             throw new ActivationValidatorException("This activation can not be planned.");
  146.         }
  147.  
  148.         if ($this->thereIsRealizedActivation($activationMapper,$values)) {
  149.             throw new ActivationValidatorException("There is realized activaiton");
  150.         }
  151.  
  152.         return true;
  153.     }
  154.  
  155.     /**
  156.      * Kontroluje se existuje pro danou objendávku jiná aktivace v daném stavu
  157.      *
  158.      * @param ActivationMapper $activationMapper
  159.      * @param array $values
  160.      *
  161.      * @return bool
  162.      * @throws ActivationValidatorException
  163.      */
  164.     private function isActivationOfOrderAlreadyExist(ActivationMapper $activationMapper,array $values, $entityOrId) {
  165.         $order_id = $values["order_id"];
  166.         //kontroluje se stav pouze v případě, že je stav naplánován
  167.         $state = (isset($values["state"]) && $values["state"] == ActivationSchema::STATE_SCHEDULED) ? $values["state"] : null;
  168.         $activations = $activationMapper->findAllByOrderId($order_id, ActivationSchema::STATE_SCHEDULED);
  169.         //pokud je udpdate a je tam jedna aktivace a je to právě ta aktivace nad kterou dělám update je to ok
  170.  
  171.         if ($entityOrId && $activations->size() == 1 && $activations->getByIndex(0)->getId() == $entityOrId) {
  172.  
  173.             return false;
  174.         }
  175.  
  176.  
  177.         return $activations->size() > 0 ? true : false;
  178.     }
  179.  
  180.     /**
  181.      * Validuje zda má uživatel v daném rozmezí nějakou jinou aktivaci
  182.      *
  183.      * @param ActivationMapper $activationMapper
  184.      * @param array $values
  185.      *
  186.      * @return bool
  187.      */
  188.     private function hasUserMoreActivation(ActivationMapper $activationMapper,array $values,$entityOrId) {
  189.         $user_id = $values["at_id"];
  190.         $start = $values["realization_start"];
  191.         $end = $values["realization_end"];
  192.  
  193.         $activations = $activationMapper->findAllScheduledByUserAndTime($user_id, $start, $end);
  194.         //pokud je udpdate a je tam jedna aktivace a je to právě ta aktivace nad kterou dělám update je to ok
  195.         if ($entityOrId && $activations->size() == 1 && $activations->getByIndex(0)->getId() == $entityOrId) {
  196.             return false;
  197.         }
  198.  
  199.  
  200.         return $activations->size() ? true : false;
  201.     }
  202.  
  203.     /**
  204.      * Je možné přiřadit aktivaci pouze objednávce, který je potvrzená
  205.      *
  206.      * @param ActivationMapper $activationMapper
  207.      * @param array $values
  208.      *
  209.      * @return bool
  210.      */
  211.     private function isOrderStateScheduled(ActivationMapper $activationMapper, array $values) {
  212.         $id = $values["order_id"];
  213.         $state = $activationMapper->getActivationOrderStateFluent($id);
  214.         return $state == OrderSchema::STATE_CONFIRMED;
  215.     }
  216.  
  217.     /**
  218.      * @param ActivationMapper $activationMapper
  219.      * @param array $values
  220.      * @param $entityOrId
  221.      */
  222.     private function hasSameValueOrder(ActivationMapper $activationMapper, array $values, $entityOrId) {
  223.         $activation = $activationMapper->findByID($entityOrId);
  224.         return ($activation->getOrderId() === $values["order_id"])? true: false;
  225.  
  226.     }
  227.  
  228.     /**
  229.      * Při nastavení stavu rejected nebo confirmed už nemůže být aktivace měněna
  230.      *
  231.      * @param $activationMapper
  232.      * @param $values
  233.      * @param $entityOrId
  234.      */
  235.     private function inStateScheduled(ActivationMapper $activationMapper, $values, $entityOrId) {
  236.         $activation = $activationMapper->findByID($entityOrId);
  237.         return ($activation->getState() === ActivationSchema::STATE_SCHEDULED);
  238.  
  239.     }
  240.  
  241.     /**
  242.      * Kontroluje zda není u dané objednávky nějaká jiná aktivace už dokončená
  243.      *
  244.      * @param $activationMapper
  245.      * @param $values
  246.      */
  247.     private function thereIsRealizedActivation(ActivationMapper $activationMapper,array $values) {
  248.         $order_id = $values["order_id"];
  249.         $activations = $activationMapper->findAllByOrderId($order_id, ActivationSchema::STATE_REALIZED);
  250.         if ($activations->size() > 0) {
  251.             return true;
  252.         }
  253.         return false;
  254.  
  255.     }
  256.  
  257.  
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement