Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- * User: radims
- * Date: 08.03.16
- * Time: 11:53
- */
- namespace App\Services\ActivationValidator;
- use App\Model\DB\Activation\Activation;
- use App\Model\DB\Activation\ActivationMapper;
- use App\Model\DB\Activation\ActivationSchema;
- use App\Model\DB\Order\OrderSchema;
- use App\Services\IValidator;
- use Nette\Object;
- use Nette\Security\User;
- use Qest\DbLayer\Mapper;
- class ActivationValidator extends Object implements IValidator{
- /**
- * Validuje nový insert update nějakého updatu
- *
- * @param ActivationMapper $activationMapper
- * @param array $values
- * @param $entityOrId
- * @param User|null $user
- *
- * @return bool|void
- * @throws ActivationValidatorException
- */
- public function validate(Mapper $activationMapper,array $values,$entityOrId , User $user = null) {
- //společná validační pravidla
- $this->validateCommon($activationMapper, $values, $entityOrId, $user);
- //pokud ukládám stávající entitu
- if ($entityOrId)
- return $this->validateUpdateEntity($activationMapper, $values, $entityOrId, $user);
- // vytvářím novou entitu
- else
- return $this->validateInsertEntity($activationMapper, $values, $entityOrId, $user);
- }
- /**
- * Kontroluje se zda se provádí update se všemi hodnotami
- *
- * @param array $values
- *
- * @throws ActivationValidatorException
- */
- public function hasAllValues(array $values) {
- if(!isset($values["order_id"]))
- throw new ActivationValidatorException("Order id is not set");
- if(!isset($values["state"]))
- throw new ActivationValidatorException("State is not set");
- if(!isset($values["at_id"]))
- throw new ActivationValidatorException("Activation technician is not set");
- if(!isset($values["realization_start"]))
- throw new ActivationValidatorException("Realization start is not set");
- if(!isset($values["realization_end"]))
- throw new ActivationValidatorException("Realization end is not set");
- return true;
- }
- /**
- * Validuje aktivaci pro insert i update
- *
- * @param ActivationMapper $activationMapper
- * @param array $values
- * @param $entityOrId
- * @param User|null $user
- *
- * @return bool
- * @throws ActivationValidatorException
- */
- private function validateCommon(ActivationMapper $activationMapper,array $values,$entityOrId , User $user = null) {
- if (!$this->hasAllValues($values)) {
- throw new ActivationValidatorException("All values fail");
- }
- if ($this->isActivationOfOrderAlreadyExist($activationMapper, $values,$entityOrId)) {
- throw new ActivationValidatorException("There is some other activation in state 'scheduled'");
- }
- if ($this->hasUserMoreActivation($activationMapper, $values,$entityOrId)) {
- throw new ActivationValidatorException("User has some activation in this time");
- }
- return true;
- }
- /**
- * Validuje update entity
- *
- * @param $activationMapper
- * @param $values
- * @param $entityOrId
- * @param $user
- *
- * @return bool
- * @throws ActivationValidatorException
- */
- private function validateUpdateEntity($activationMapper, $values, $entityOrId, $user = null) {
- if (!$this->hasSameValueOrder($activationMapper, $values, $entityOrId)) {
- throw new ActivationValidatorException("Order id canot be changed");
- }
- if (!$this->inStateScheduled($activationMapper,$values,$entityOrId)) {
- throw new ActivationValidatorException("Activation can be changed only in scheduled state");
- }
- return true;
- }
- /**
- * Validuje vložení nové entity
- *
- * @param ActivationMapper $activationMapper
- * @param array $values
- * @param $entityOrId
- * @param User $user
- *
- * @return bool
- * @throws ActivationValidatorException
- */
- private function validateInsertEntity(ActivationMapper $activationMapper,array $values, $entityOrId, User $user = null) {
- if (!$this->isOrderStateScheduled($activationMapper, $values)) {
- throw new ActivationValidatorException("This activation can not be planned.");
- }
- if ($this->thereIsRealizedActivation($activationMapper,$values)) {
- throw new ActivationValidatorException("There is realized activaiton");
- }
- return true;
- }
- /**
- * Kontroluje se existuje pro danou objendávku jiná aktivace v daném stavu
- *
- * @param ActivationMapper $activationMapper
- * @param array $values
- *
- * @return bool
- * @throws ActivationValidatorException
- */
- private function isActivationOfOrderAlreadyExist(ActivationMapper $activationMapper,array $values, $entityOrId) {
- $order_id = $values["order_id"];
- //kontroluje se stav pouze v případě, že je stav naplánován
- $state = (isset($values["state"]) && $values["state"] == ActivationSchema::STATE_SCHEDULED) ? $values["state"] : null;
- $activations = $activationMapper->findAllByOrderId($order_id, ActivationSchema::STATE_SCHEDULED);
- //pokud je udpdate a je tam jedna aktivace a je to právě ta aktivace nad kterou dělám update je to ok
- if ($entityOrId && $activations->size() == 1 && $activations->getByIndex(0)->getId() == $entityOrId) {
- return false;
- }
- return $activations->size() > 0 ? true : false;
- }
- /**
- * Validuje zda má uživatel v daném rozmezí nějakou jinou aktivaci
- *
- * @param ActivationMapper $activationMapper
- * @param array $values
- *
- * @return bool
- */
- private function hasUserMoreActivation(ActivationMapper $activationMapper,array $values,$entityOrId) {
- $user_id = $values["at_id"];
- $start = $values["realization_start"];
- $end = $values["realization_end"];
- $activations = $activationMapper->findAllScheduledByUserAndTime($user_id, $start, $end);
- //pokud je udpdate a je tam jedna aktivace a je to právě ta aktivace nad kterou dělám update je to ok
- if ($entityOrId && $activations->size() == 1 && $activations->getByIndex(0)->getId() == $entityOrId) {
- return false;
- }
- return $activations->size() ? true : false;
- }
- /**
- * Je možné přiřadit aktivaci pouze objednávce, který je potvrzená
- *
- * @param ActivationMapper $activationMapper
- * @param array $values
- *
- * @return bool
- */
- private function isOrderStateScheduled(ActivationMapper $activationMapper, array $values) {
- $id = $values["order_id"];
- $state = $activationMapper->getActivationOrderStateFluent($id);
- return $state == OrderSchema::STATE_CONFIRMED;
- }
- /**
- * @param ActivationMapper $activationMapper
- * @param array $values
- * @param $entityOrId
- */
- private function hasSameValueOrder(ActivationMapper $activationMapper, array $values, $entityOrId) {
- $activation = $activationMapper->findByID($entityOrId);
- return ($activation->getOrderId() === $values["order_id"])? true: false;
- }
- /**
- * Při nastavení stavu rejected nebo confirmed už nemůže být aktivace měněna
- *
- * @param $activationMapper
- * @param $values
- * @param $entityOrId
- */
- private function inStateScheduled(ActivationMapper $activationMapper, $values, $entityOrId) {
- $activation = $activationMapper->findByID($entityOrId);
- return ($activation->getState() === ActivationSchema::STATE_SCHEDULED);
- }
- /**
- * Kontroluje zda není u dané objednávky nějaká jiná aktivace už dokončená
- *
- * @param $activationMapper
- * @param $values
- */
- private function thereIsRealizedActivation(ActivationMapper $activationMapper,array $values) {
- $order_id = $values["order_id"];
- $activations = $activationMapper->findAllByOrderId($order_id, ActivationSchema::STATE_REALIZED);
- if ($activations->size() > 0) {
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement