ActionDeleteAbstract   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 53
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 11 1
A action() 0 17 2
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
}