Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- namespace App\GraphQL\Mutation;
- use App\Entity\Tenant\Business;
- use App\Entity\Tenant\Customer;
- use App\Entity\Tenant\Task;
- use App\Entity\Tenant\User;
- use Doctrine\Common\Collections\ArrayCollection;
- use Doctrine\Common\Persistence\ManagerRegistry;
- use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface;
- use Overblog\GraphQLBundle\Definition\Resolver\MutationInterface;
- use Overblog\GraphQLBundle\Definition\Argument;
- use Overblog\GraphQLBundle\Error\UserError;
- /**
- * Class TaskMutation
- * @package App\GraphQL\Mutation
- * @author Robinson Palacios
- */
- class TaskMutation implements MutationInterface, AliasedInterface
- {
- /**
- * @var ObjectManager
- */
- private $manager;
- /**
- * TaskMutation constructor.
- * @param ManagerRegistry $registry
- */
- public function __construct(ManagerRegistry $registry)
- {
- $this->manager = $registry->getManager('tenant');
- }
- /**
- * @return array|string[]
- */
- public static function getAliases(): array
- {
- return [
- 'createTask' => 'create_task',
- 'updateTask' => 'update_task',
- 'deleteTask' => 'delete_task'
- ];
- }
- /**
- * @param User $user
- * @param Argument $args
- * @return Task
- */
- public function createTask(User $user, Argument $args): Task
- {
- $customer = $this->manager->getRepository(Customer::Class)->find($args['input']['customer_id']);
- if(!$customer){
- throw new UserError('Customer not found.');
- }
- $business = $this->manager->getRepository(Business::Class)->find($args['input']['business_id']);
- if(!$business){
- throw new UserError('Business not found.');
- }
- $task = new Task();
- $task->setTitle($args['input']['title']);
- $task->setDescription($args['input']['description']);
- $task->setCustomer($customer);
- $task->setBusiness($business);
- if(isset($args['input']['users_ids']) && $args['input']['users_ids']){
- //If the user is not admin, only can to asign task to himself
- if($user->getUserType() != 0 && (count($args['input']['users_ids']) > 1 || $args['input']['users_ids'][0] != $user->getId())){
- throw new UserError('User asigned no valid.');
- }
- $users = $this->manager->getRepository(User::Class)->findBy(array('id' => $args['input']['users_ids']));
- $task->setUsers(new ArrayCollection($users));
- }
- $task->setDueDate($args['input']['due_date']);
- $task->setPriority($args['input']['priority']);
- $task->setEstimatedTime($args['input']['estimated_time']);
- $task->setCompleteness($args['input']['completeness']);
- $this->manager->getRepository(Task::Class)->save($task);
- return $task;
- }
- /**
- * @param User $user
- * @param Argument $args
- * @return Task
- */
- public function updateTask(User $user, Argument $args): Task
- {
- $task = $this->manager->getRepository(Task::Class)->find($args['input']['id']);
- if (!$task) {
- throw new UserError(
- 'No task found for Id '.$args['input']['id']
- );
- }
- $customer = $this->manager->getRepository(Customer::Class)->find($args['input']['customer_id']);
- if(!$customer){
- throw new UserError('Customer not found.');
- }
- $business = $this->manager->getRepository(Business::Class)->find($args['input']['business_id']);
- if(!$business){
- throw new UserError('Business not found.');
- }
- $task->setTitle($args['input']['title']);
- $task->setDescription($args['input']['description']);
- $task->setCustomer($customer);
- $task->setBusiness($business);
- if(isset($args['input']['users_ids']) && $args['input']['users_ids']){
- //If the user is not admin, only can to asign task to himself
- if($user->getUserType() != 0 && (count($args['input']['users_ids']) > 1 || $args['input']['users_ids'][0] != $user->getId())){
- throw new UserError('User asigned no valid.');
- }
- $users = $this->manager->getRepository(User::Class)->findBy(array('id' => $args['input']['users_ids']));
- $task->setUsers(new ArrayCollection($users));
- }
- $task->setDueDate($args['input']['due_date']);
- $task->setPriority($args['input']['priority']);
- $task->setEstimatedTime($args['input']['estimated_time']);
- $task->setCompleteness($args['input']['completeness']);
- $this->manager->getRepository(Task::Class)->save($task);
- return $task;
- }
- public function deleteTask(User $user, Argument $args): Task{
- $task = $this->manager->getRepository(Task::Class)->find($args['id']);
- if(!$task){
- throw new UserError('Task not exist.');
- }
- if($user->getUserType() == 0 || $user->getId() == $task->getCreatedBy()->getId()) {
- $task->setDeletedBy($user);
- $this->manager->getRepository(Task::Class)->save($task);
- $this->manager->remove($task);
- $this->manager->flush();
- }else{
- throw new UserError("You can't delete this task.");
- }
- return $task;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement