queryBuilder = $queryBuilder; $this->entityManager = $entityManager; $this->service = $service; $this->user = $user; } /** * @throws BadRequest * @throws Forbidden */ public function process(Params $params, Data $data): Result { $folderId = $data->get('folderId'); if (!is_string($folderId)) { throw new BadRequest("No folder ID."); } if ($folderId !== self::FOLDER_INBOX && strpos($folderId, 'group:') !== 0) { $folder = $this->entityManager ->getRDBRepositoryByClass(EmailFolder::class) ->where([ 'assignedUserId' => $this->user->getId(), 'id' => $folderId, ]) ->findOne(); if (!$folder) { throw new Forbidden("Folder not found."); } } if ($folderId && strpos($folderId, 'group:') === 0) { $folder = $this->entityManager ->getRDBRepositoryByClass(GroupEmailFolder::class) ->where(['id' => substr($folderId, 6)]) ->findOne(); if (!$folder) { throw new Forbidden("Group folder not found."); } } $query = $this->queryBuilder->build($params); $collection = $this->entityManager ->getRDBRepositoryByClass(Email::class) ->clone($query) ->sth() ->select(['id']) ->find(); $count = 0; foreach ($collection as $email) { try { $this->service->moveToFolder($email->getId(), $folderId, $this->user->getId()); } catch (Exception $e) { continue; } $count++; } return new Result($count); } }