aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Files/SubfoldersCreate.php
blob: f476f15c4fb02a872cc497a03c67fe5ac39fc176 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

namespace JsonApi\Routes\Files;

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

class SubfoldersCreate extends RangeFoldersCreate
{
    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameters)
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        if (!$parent = \Folder::find($args['id'])) {
            throw new RecordNotFoundException();
        }

        $folder = $this->validateAndCreate($request, $parent);

        return $this->getCreatedResponse($folder);
    }

    protected function validateAndCreate(Request $request, \Folder $parent)
    {
        $rangeType = $parent->range_type;
        $range = $parent->$rangeType;

        if (!Authority::canShowFileArea($user = $this->getUser($request), $range)) {
            throw new AuthorizationFailedException();
        }

        $json = $this->validate($request);

        return $this->validateAndCreateSubfolder($range, $user, $json, $parent);
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameters)
     */
    protected function validateResourceDocument($json, $data)
    {
        if ($err = $this->validateFolderResourceObject($json, null, false)) {
            return $err;
        }
    }
}