ActionDeleteAbstract::action()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 0
crap 6
1
<?php
2
namespace Staticus\Middlewares;
3
4
use League\Flysystem\FilesystemInterface;
5
use Staticus\Resources\Commands\DeleteSafetyResourceCommand;
6
use Staticus\Resources\Commands\DestroyResourceCommand;
7
use Staticus\Resources\Middlewares\PrepareResourceMiddlewareAbstract;
8
use Staticus\Resources\ResourceDOInterface;
9
use Zend\Diactoros\Response\EmptyResponse;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Staticus\Resources\File\ResourceDO;
13
14
abstract class ActionDeleteAbstract extends MiddlewareAbstract
15
{
16
    /**
17
     * @var ResourceDOInterface|ResourceDO
18
     */
19
    protected $resourceDO;
20
    /**
21
     * @var
22
     */
23
    protected $filesystem;
24
25
    public function __construct(ResourceDOInterface $resourceDO, FilesystemInterface $filesystem)
26
    {
27
        $this->resourceDO = $resourceDO;
28
        $this->filesystem = $filesystem;
29
    }
30
    /**
31
     * @param ServerRequestInterface $request
32
     * @param ResponseInterface $response
33
     * @param callable|null $next
34
     * @return EmptyResponse
35
     * @throws \Exception
36
     */
37
    public function __invoke(
38
        ServerRequestInterface $request,
39
        ResponseInterface $response,
40
        callable $next = null
41
    )
42
    {
43
        parent::__invoke($request, $response, $next);
44
        $this->response = $this->action();
45
46
        return $this->next();
47
    }
48
49
    protected function action()
50
    {
51
        $headers = [
52
            'Content-Type' => $this->resourceDO->getMimeType(),
53
        ];
54
        $destroy = PrepareResourceMiddlewareAbstract::getParamFromRequest('destroy', $this->request);
55
        if ($destroy) {
56
            $command = new DestroyResourceCommand($this->resourceDO, $this->filesystem);
57
            $command();
58
        } else {
59
            $command = new DeleteSafetyResourceCommand($this->resourceDO, $this->filesystem);
60
            $command();
61
        }
62
63
        /** @see \Zend\Diactoros\Response::$phrases */
64
        return new EmptyResponse(204, $headers);
65
    }
66
}