Get Delivery execution from resource

@param $deliveryExecutionId
@return DeliveryExecutionInterface
@throws \common_exception_NotFound
962256
public function getDeliveryExecutionById($deliveryExecutionId)
    {
        $deliveryExecution = $this->getDeliveryExecutionService()->getDeliveryExecution($deliveryExecutionId);
        try {
            $deliveryExecution->getDelivery();
        } catch (\common_exception_NotFound $e) {
            throw new \common_exception_NotFound('Provided parameters don\'t match with any delivery execution.');
        }
        return $deliveryExecution;
    }
1214342
public function getDeliveryTimer($deliveryExecution)
    {
        if (is_string($deliveryExecution)) {
            $deliveryExecution = $this->getDeliveryExecutionById($deliveryExecution);
        }

        /** @var TestSessionService $testSessionService */
        $testSessionService = $this->getServiceLocator()->get(TestSessionService::SERVICE_ID);

        $testSession = $testSessionService->getTestSession($deliveryExecution);
        if ($testSession instanceof TestSession) {
            $timer = $testSession->getTimer();
        } else {
            $qtiTimerFactory = $this->getServiceLocator()->get(QtiTimerFactory::SERVICE_ID);
            $timer = $qtiTimerFactory->getTimer($deliveryExecution->getIdentifier(), $deliveryExecution->getUserIdentifier());
        }

        return $timer;
    }
1038939
public function get($serviceId)
    {
        if (!$this->has($serviceId)) {
            throw NotFoundException::constructWithThingNotFound($serviceId);
        }
        return $this->symfonyServiceContainer->get($serviceId);
    }
121647
public function getById(OrderId $orderId) : Order
    {
        if (!array_key_exists((string)$orderId, $this->orders)) {
            throw OrderNotFoundException::byId($orderId);
        }

        return $this->orders[(string)$orderId];
    }
715677
public function verifyResumeAuthorization(DeliveryExecutionInterface $deliveryExecution, User $user)
    {
        $stateId = $deliveryExecution->getState()->getUri();
        if (!in_array($stateId, $this->getResumableStates())) {
            \common_Logger::w('Unexpected state "'.$stateId);
            throw new \common_exception_Unauthorized();
        }
    }
608724
public function getDeliveryIdFromSession($deliveryExecutionId)
    {
        if (\PHPSession::singleton()->hasAttribute(static::getDeliveryIdSessionKey($deliveryExecutionId))){
            return \PHPSession::singleton()->getAttribute(static::getDeliveryIdSessionKey($deliveryExecutionId));
        }

        return false;
    }
1345113
protected function getResultStorage($delivery)
    {
        /** @var ResultServerService $resultServerService */
        $resultServerService = $this->getServiceManager()->get(ResultServerService::SERVICE_ID);
        $resultStorage = $resultServerService->getResultStorage($delivery->getUri());
        if ($resultStorage instanceof NoResultStorage){
           throw NoResultStorageException::create();
        }

        if (!$resultStorage instanceof \taoResultServer_models_classes_ReadableResultStorage){
           throw new \common_exception_Error('The results storage it is not readable');
        }
        $this->getResultsService()->setImplementation($resultStorage);
        return $resultStorage;
    }
950186
private static function createErrorMessage($deliveryExecution, $action)
    {
        if ($deliveryExecution->getState()->getUri() === DeliveryExecution::STATE_FINISHED) {
            $errorMsg = __('%s could not be %s because it is finished. Please refresh your data.', $deliveryExecution->getLabel(), $action);
        } else if ($deliveryExecution->getState()->getUri() === DeliveryExecution::STATE_TERMINATED) {
            $errorMsg = __('%s could not be %s because it is terminated. Please refresh your data.', $deliveryExecution->getLabel(), $action);
        } else {
            $errorMsg = __('%s could not be %s.', $deliveryExecution->getLabel(), $action);
        }

        return $errorMsg;
    }
279793
public function getDelivery() {
        if (!isset($this->delivery)) {
            $this->delivery = $this->getData(OntologyDeliveryExecution::PROPERTY_DELIVERY);
        }
        return $this->delivery;
    }
701485
public function getDeliveryStatus()
    {
        $status = $this->entity->getDeliveryStatus();
        if ($status !== null) {
            return new DeliveryStatus($status, $this->locale);
        }

        return null;
    }
201559
protected function _initDeliveryExecution() {
        $compiledDelivery  = new \core_kernel_classes_Resource(\tao_helpers_Uri::decode($this->getRequestParameter('uri')));
        $user              = common_session_SessionManager::getSession()->getUser();

        $assignmentService = $this->getServiceLocator()->get(AssignmentService::SERVICE_ID);

        $this->verifyDeliveryStartAuthorized($compiledDelivery->getUri());

        //check if the assignment allows the user to start the delivery and the authorization provider
        if (!$assignmentService->isDeliveryExecutionAllowed($compiledDelivery->getUri(), $user) ) {
            throw new common_exception_Unauthorized();
        }
        $stateService = $this->getServiceLocator()->get(StateServiceInterface::SERVICE_ID);
        /** @var DeliveryExecution $deliveryExecution */
        $deliveryExecution = $stateService->createDeliveryExecution($compiledDelivery->getUri(), $user, $compiledDelivery->getLabel());


        return $deliveryExecution;
    }
1210308
public function pauseActiveExecutions()
    {
        if (!$this->isRequestPost()) {
            throw new \common_exception_BadRequest('Invalid request. Only POST method allowed.');
        }

        $reason = $this->hasRequestParameter('reason') ? $this->getRequestParameter('reason') : [
            'reasons' => ['category' => 'Technical', 'subCategory' => 'ACT'],
            'comment' => __('Pause due to server maintenance'),
        ];
        /** @var DeliveryMonitoringService $monitoringService */
        $monitoringService = $this->getServiceLocator()->get(DeliveryMonitoringService::SERVICE_ID);
        $deliveryExecutions = $monitoringService->find(
            [DeliveryMonitoringService::STATUS => DeliveryExecution::STATE_ACTIVE],
            ['asArray' => true]
        );
        $ids = array_map(function ($deliveryExecution) {
            return $deliveryExecution['delivery_execution_id'];
        }, $deliveryExecutions);
        $stats = DeliveryHelper::pauseExecutions($ids, $reason);
        $paused = $stats['processed'];
        $notPaused = $stats['unprocessed'];

        $this->returnJson([
            'success' => true,
            'data' => [
                'message' => count($paused) . ' ' . __('sessions paused'),
                'processed' => $paused,
                'unprocessed' => $notPaused
            ]
        ]);
    }
1069205
public function getDeliveryThemeIdFromCache($deliveryId)
    {
        $cache    = $this->getCache();
        $cacheKey = $this->getCacheKey($deliveryId);
        if ($cache->has($cacheKey)) {
            return $cache->get($cacheKey);
        }

        return false;
    }
1144917
public function log($deliveryExecutionId, $eventId, $data, $user = null)
    {
        $data = $this->encodeData($data);

        if ($user === null) {
            $user = \common_session_SessionManager::getSession()->getUser()->getIdentifier();
        }

        if (empty($user) && PHP_SAPI == 'cli') {
            $user = 'cli';
        }

        $result = $this->getPersistence()->insert(
            self::TABLE_NAME,
            array(
                self::DELIVERY_EXECUTION_ID => $deliveryExecutionId,
                self::EVENT_ID => $eventId,
                self::DATA => $data,
                self::CREATED_AT => microtime(true),
                self::CREATED_BY => $user,
            )
        );

        return $result === 1;
    }
725757
public function getLinkedDeliveryExecutions(\core_kernel_classes_Resource $delivery, \core_kernel_classes_Resource $link, $userId)
    {
        $class = new \core_kernel_classes_Class(OntologyLTIDeliveryExecutionLink::CLASS_LTI_DELIVERYEXECUTION_LINK);
        $links = $class->searchInstances([
            OntologyLTIDeliveryExecutionLink::PROPERTY_LTI_DEL_EXEC_LINK_USER => $userId,
            OntologyLTIDeliveryExecutionLink::PROPERTY_LTI_DEL_EXEC_LINK_LINK => $link,
        ], [
            'like' => false
        ]);
        $result = [];
        foreach ($links as $link) {
            $execId = $link->getUniquePropertyValue(new \core_kernel_classes_Property(OntologyLTIDeliveryExecutionLink::PROPERTY_LTI_DEL_EXEC_LINK_EXEC_ID));
            $deliveryExecution = ServiceProxy::singleton()->getDeliveryExecution($execId);
            if ($delivery->equals($deliveryExecution->getDelivery())) {
                $result[] = $deliveryExecution;
            }
        }
        return $result;
    }
64103
public function initDeliveryExecution() {
        try {
            $deliveryExecution = $this->_initDeliveryExecution();
            //if authorized we can move to this URL.
            $this->redirect(_url('runDeliveryExecution', null, null, array('deliveryExecution' => $deliveryExecution->getIdentifier())));
        } catch (UnAuthorizedException $e) {
            return $this->redirect($e->getErrorPage());
        } catch (common_exception_Unauthorized $e) {
            return $this->returnJson([
                'success' => false,
                'message' => __('You are no longer allowed to take this test')
            ], 403);
        }
    }
1228979
private function getDelivery()
    {
        $returnValue = null;
        //passed as aprameter
        if ($this->hasRequestParameter('delivery')) {
            $returnValue = new core_kernel_classes_Resource($this->getRequestParameter('delivery'));
        } else {
            
            $launchData = LtiService::singleton()->getLtiSession()->getLaunchData();

            /** @var LtiLaunchDataService $launchDataService */
            $launchDataService = $this->getServiceLocator()->get(LtiLaunchDataService::SERVICE_ID);
            $returnValue = $launchDataService->findDeliveryFromLaunchData($launchData);
        }
        return $returnValue;
    }