aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Courseware/StructuralElementsImageDelete.php
blob: b8d2a0c132d7488852a817a23295cea58da9e1b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

namespace JsonApi\Routes\Courseware;

use Courseware\StructuralElement;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\NonJsonApiController;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class StructuralElementsImageDelete extends NonJsonApiController
{
    public function invoke(Request $request, Response $response, array $args): Response
    {
        if (!($structuralElement = StructuralElement::find($args['id']))) {
            throw new RecordNotFoundException();
        }

        if (!Authority::canDeleteStructuralElementsImage($this->getUser($request), $structuralElement)) {
            throw new AuthorizationFailedException();
        }

        if ($structuralElement->image) {
            $structuralElement->image->getFileType()->delete();
        }

        $structuralElement->image_id = null;
        $structuralElement->store();

        return $response->withStatus(204);
    }
}