Skip to content
Snippets Groups Projects
Select Git revision
  • b11152c9e53633e708cb71d76699c47faba63518
  • main default protected
  • step-01354-5.5
  • biest-504
  • issue-3215
  • step-03209-bald-rapunzel
  • tic-3123
  • tic-2720-remove-less-compilation-in-plugins
  • step-1800
  • tic-2532
  • biest-561
  • tic-3225
  • 5.3
  • 5.4
  • step-2472
  • step-2660
  • step-1559
  • tic-3094
  • biest-3206
  • biest-3207
  • 5.0
  • v5.3.1
  • v5.2.3
  • v5.1.4
  • v5.0.6
  • v5.3
  • v5.2.2
  • v5.1.3
  • v5.0.5
  • v5.2.1
  • v5.1.2
  • v5.0.4
  • v5.2
  • v5.1.1
  • v5.0.3
  • v5.1
  • v5.0.2
  • v5.0.1
  • v5.0
39 results

PublicLinksUpdate.php

Blame
  • Forked from Stud.IP / Stud.IP
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    PublicLinksUpdate.php 2.34 KiB
    <?php
    
    namespace JsonApi\Routes\Courseware;
    
    use Courseware\PublicLink;
    use JsonApi\Errors\AuthorizationFailedException;
    use JsonApi\Errors\RecordNotFoundException;
    use JsonApi\Errors\UnprocessableEntityException;
    use JsonApi\JsonApiController;
    use JsonApi\Routes\TimestampTrait;
    use JsonApi\Routes\ValidationTrait;
    use Psr\Http\Message\ResponseInterface as Response;
    use Psr\Http\Message\ServerRequestInterface as Request;
    
    /**
     * Update one PublicLink.
     */
    class PublicLinksUpdate extends JsonApiController
    {
        use TimestampTrait;
        use ValidationTrait;
            /**
         * @SuppressWarnings(PHPMD.UnusedFormalParameter)
         */
        public function __invoke(Request $request, Response $response, $args)
        {
            $resource = PublicLink::find($args['id']);
            if (!$resource) {
                throw new RecordNotFoundException();
            }
            $json = $this->validate($request, $resource);
            if (!Authority::canUpdatePublicLink($user = $this->getUser($request), $resource)) {
                throw new AuthorizationFailedException();
            }
            $resource = $this->updatePublicLink($resource, $json);
    
            return $this->getContentResponse($resource);
        }
    
            /**
         * @SuppressWarnings(PHPMD.UnusedFormalParameters)
         */
        protected function validateResourceDocument($json, $data)
        {
            if (!self::arrayHas($json, 'data')) {
                return 'Missing `data` member at document´s top level.';
            }
    
            if (!self::arrayHas($json, 'data.id')) {
                return 'Document must have an `id`.';
            }
    
            if (self::arrayHas($json, 'data.attributes.expire-date')) {
                $expire_date = self::arrayGet($json, 'data.attributes.expire-date');
                if (!self::isValidTimestamp($expire_date)) {
                    return '`expire-date` is not an ISO 8601 timestamp.';
                }
            }
        }
    
        private function updatePublicLink(PublicLink $resource, array $json): PublicLink
        {
            $get = function ($key, $default = '') use ($json) {
                return self::arrayGet($json, $key, $default);
            };
    
            $resource->password = $get('data.attributes.password');
    
            $expire_date = $get('data.attributes.expire-date');
            $expireDate = self::fromISO8601($expire_date);
            $resource->expire_date =  $expireDate->getTimestamp();
    
            $resource->store();
    
            return $resource;
        }
    
    }