/**
  * Partial re-order of milestone backlog relative to one element.
  *
  * <br>
  * Example:
  * <pre>
  * "order": {
  *   "ids" : [123, 789, 1001],
  *   "direction": "before",
  *   "compared_to": 456
  * },
  * "add": {
  *   [123]
  * }
  * </pre>
  *
  * <br>
  * Resulting order will be: <pre>[…, 123, 789, 1001, 456, …]</pre>
  *
  * <br>
  * Add example:
  * <pre>
  * "add": [
  *   {
  *     "id": 34
  *     "remove_from": 56
  *   },
  *   ...
  * ]
  * </pre>
  *
  * <br>
  * Will remove element id 34 from milestone 56 backlog and add it to current backlog
  *
  * @url PATCH {id}/backlog
  *
  * @param int                                                $id    Id of the milestone Item
  * @param \Tuleap\AgileDashboard\REST\v1\OrderRepresentation $order Order of the children {@from body}
  * @param array                                              $add    Ids to add/move to milestone backlog {@from body}
  *
  * @throw 400
  * @throw 403
  * @throw 404
  * @throw 409
  */
 protected function patchBacklog($id, OrderRepresentation $order = null, array $add = null)
 {
     $user = $this->getCurrentUser();
     $milestone = $this->getMilestoneById($user, $id);
     $this->checkIfUserCanChangePrioritiesInMilestone($milestone, $user);
     $to_add = array();
     try {
         if ($add) {
             $this->resources_patcher->startTransaction();
             $to_add = $this->resources_patcher->removeArtifactFromSource($user, $add);
             if (count($to_add)) {
                 $valid_to_add = $this->milestone_validator->validateArtifactIdsCanBeAddedToBacklog($to_add, $milestone, $user);
                 $this->addMissingElementsToBacklog($milestone, $user, $valid_to_add);
             }
             $this->resources_patcher->commit();
         }
     } catch (Tracker_NoChangeException $exception) {
         // nothing to do
     } catch (\Exception $exception) {
         throw new RestException(400, $exception->getMessage());
     }
     try {
         if ($order) {
             $order->checkFormat($order);
             $this->milestone_validator->validateArtifactIdsAreInUnplannedMilestone($this->filterOutAddedElements($order, $to_add), $milestone, $user);
             $this->resources_patcher->updateArtifactPriorities($order, $id, $milestone->getProject()->getId());
         }
     } catch (IdsFromBodyAreNotUniqueException $exception) {
         throw new RestException(409, $exception->getMessage());
     } catch (ArtifactIsNotInUnplannedBacklogItemsException $exception) {
         throw new RestException(409, $exception->getMessage());
     } catch (Tracker_Artifact_Exception_CannotRankWithMyself $exception) {
         throw new RestException(400, $exception->getMessage());
     } catch (\Exception $exception) {
         throw new RestException(400, $exception->getMessage());
     }
 }