Advertisement
danrmejia

Untitled

Jul 5th, 2020
1,426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.41 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\GraphQL\Mutation;
  6.  
  7. use App\Entity\Tenant\Business;
  8. use App\Entity\Tenant\Customer;
  9. use App\Entity\Tenant\Task;
  10. use App\Entity\Tenant\User;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Persistence\ManagerRegistry;
  13. use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface;
  14. use Overblog\GraphQLBundle\Definition\Resolver\MutationInterface;
  15. use Overblog\GraphQLBundle\Definition\Argument;
  16. use Overblog\GraphQLBundle\Error\UserError;
  17.  
  18. /**
  19.  * Class TaskMutation
  20.  * @package App\GraphQL\Mutation
  21.  * @author Robinson Palacios
  22.  */
  23. class TaskMutation implements MutationInterface, AliasedInterface
  24. {
  25.     /**
  26.      * @var ObjectManager
  27.      */
  28.     private $manager;
  29.  
  30.     /**
  31.      * TaskMutation constructor.
  32.      * @param ManagerRegistry $registry
  33.      */
  34.     public function __construct(ManagerRegistry $registry)
  35.     {
  36.         $this->manager = $registry->getManager('tenant');
  37.     }
  38.  
  39.     /**
  40.      * @return array|string[]
  41.      */
  42.     public static function getAliases(): array
  43.     {
  44.         return [
  45.             'createTask' => 'create_task',
  46.             'updateTask' => 'update_task',
  47.             'deleteTask' => 'delete_task'
  48.  
  49.         ];
  50.     }
  51.  
  52.     /**
  53.      * @param User $user
  54.      * @param Argument $args
  55.      * @return Task
  56.      */
  57.     public function createTask(User $user, Argument $args): Task
  58.     {
  59.         $customer = $this->manager->getRepository(Customer::Class)->find($args['input']['customer_id']);
  60.         if(!$customer){
  61.             throw new UserError('Customer not found.');
  62.         }
  63.  
  64.         $business = $this->manager->getRepository(Business::Class)->find($args['input']['business_id']);
  65.         if(!$business){
  66.             throw new UserError('Business not found.');
  67.         }
  68.  
  69.         $task = new Task();
  70.  
  71.         $task->setTitle($args['input']['title']);
  72.         $task->setDescription($args['input']['description']);
  73.         $task->setCustomer($customer);
  74.         $task->setBusiness($business);
  75.  
  76.         if(isset($args['input']['users_ids']) && $args['input']['users_ids']){
  77.             //If the user is not admin, only can to asign task to himself
  78.             if($user->getUserType() != 0 && (count($args['input']['users_ids']) > 1 || $args['input']['users_ids'][0] != $user->getId())){
  79.                 throw new UserError('User asigned no valid.');
  80.             }
  81.             $users = $this->manager->getRepository(User::Class)->findBy(array('id' => $args['input']['users_ids']));
  82.             $task->setUsers(new ArrayCollection($users));
  83.         }
  84.  
  85.         $task->setDueDate($args['input']['due_date']);
  86.         $task->setPriority($args['input']['priority']);
  87.         $task->setEstimatedTime($args['input']['estimated_time']);
  88.         $task->setCompleteness($args['input']['completeness']);
  89.  
  90.         $this->manager->getRepository(Task::Class)->save($task);
  91.  
  92.         return $task;
  93.     }
  94.  
  95.     /**
  96.      * @param User $user
  97.      * @param Argument $args
  98.      * @return Task
  99.      */
  100.     public function updateTask(User $user, Argument $args): Task
  101.     {
  102.         $task =  $this->manager->getRepository(Task::Class)->find($args['input']['id']);
  103.  
  104.         if (!$task) {
  105.             throw new UserError(
  106.                 'No task found for Id '.$args['input']['id']
  107.             );
  108.         }
  109.  
  110.         $customer = $this->manager->getRepository(Customer::Class)->find($args['input']['customer_id']);
  111.         if(!$customer){
  112.             throw new UserError('Customer not found.');
  113.         }
  114.  
  115.         $business = $this->manager->getRepository(Business::Class)->find($args['input']['business_id']);
  116.         if(!$business){
  117.             throw new UserError('Business not found.');
  118.         }
  119.  
  120.         $task->setTitle($args['input']['title']);
  121.         $task->setDescription($args['input']['description']);
  122.         $task->setCustomer($customer);
  123.         $task->setBusiness($business);
  124.  
  125.         if(isset($args['input']['users_ids']) && $args['input']['users_ids']){
  126.             //If the user is not admin, only can to asign task to himself
  127.             if($user->getUserType() != 0 && (count($args['input']['users_ids']) > 1 || $args['input']['users_ids'][0] != $user->getId())){
  128.                 throw new UserError('User asigned no valid.');
  129.             }
  130.             $users = $this->manager->getRepository(User::Class)->findBy(array('id' => $args['input']['users_ids']));
  131.             $task->setUsers(new ArrayCollection($users));
  132.         }
  133.  
  134.         $task->setDueDate($args['input']['due_date']);
  135.         $task->setPriority($args['input']['priority']);
  136.         $task->setEstimatedTime($args['input']['estimated_time']);
  137.         $task->setCompleteness($args['input']['completeness']);
  138.  
  139.         $this->manager->getRepository(Task::Class)->save($task);
  140.  
  141.         return $task;
  142.     }
  143.  
  144.     public function deleteTask(User $user, Argument $args): Task{
  145.         $task =  $this->manager->getRepository(Task::Class)->find($args['id']);
  146.         if(!$task){
  147.             throw new UserError('Task not exist.');
  148.         }
  149.         if($user->getUserType() == 0 || $user->getId() == $task->getCreatedBy()->getId()) {
  150.             $task->setDeletedBy($user);
  151.             $this->manager->getRepository(Task::Class)->save($task);
  152.             $this->manager->remove($task);
  153.             $this->manager->flush();
  154.  
  155.         }else{
  156.             throw new UserError("You can't delete this task.");
  157.         }
  158.  
  159.         return $task;
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement