ActionGetAbstract::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 2
1
<?php
2
namespace Staticus\Middlewares;
3
4
use League\Flysystem\FilesystemInterface;
5
use Staticus\Exceptions\ErrorException;
6
use Staticus\Resources\ResourceDOInterface;
7
use Zend\Diactoros\Response\EmptyResponse;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Staticus\Resources\File\ResourceDO;
11
12
abstract class ActionGetAbstract extends MiddlewareAbstract
13
{
14
    /**
15
     * @var mixed
16
     */
17
    protected $generator;
18
    /**
19
     * @var ResourceDOInterface|ResourceDO
20
     */
21
    protected $resourceDO;
22
    /**
23
     * @var
24
     */
25
    protected $filesystem;
26
27
    public function __construct(ResourceDOInterface $resourceDO, FilesystemInterface $filesystem)
28
    {
29
        $this->resourceDO = $resourceDO;
30
        $this->filesystem = $filesystem;
31
    }
32
    /**
33
     * @param ServerRequestInterface $request
34
     * @param ResponseInterface $response
35
     * @param callable|null $next
36
     * @return EmptyResponse
37
     * @throws \Exception
38
     */
39
    public function __invoke(
40
        ServerRequestInterface $request,
41
        ResponseInterface $response,
42
        callable $next = null
43
    )
44
    {
45
        parent::__invoke($request, $response, $next);
46
        $this->response = $this->action();
47
48
        return $this->next();
49
    }
50
51
    /**
52
     * @param string $path
53
     * @param string $filename Filename for saving dialog on the client-side
54
     * @param bool $forceSaveDialog
55
     * @return EmptyResponse
56
     * @throws ErrorException
57
     */
58
    protected function XAccelRedirect($path, $filename = '', $forceSaveDialog = false)
59
    {
60
        $mime = $this->filesystem->getMimetype($path);
61
        if (empty($mime)) {
62
            throw new ErrorException('Mime content type can not be reached');
63
        }
64
        $headers = [
65
            'X-Accel-Redirect' => $path,
66
            'Content-Type' => $mime,
67
            // '' =>
68
        ];
69
        if ($forceSaveDialog) {
70
            if (!$filename) {
71
                $filename = basename($path);
72
            }
73
            $headers['Content-Disposition'] = 'attachment; filename=' . $filename;
74
        }
75
76
        return new EmptyResponse(200, $headers);
77
    }
78
79
    protected function action()
80
    {
81
        $headers = [
82
            'Content-Type' => $this->resourceDO->getMimeType(),
83
        ];
84
        $filePath = $this->resourceDO->getFilePath();
85
        $filename = $this->resourceDO->getName() . '.' . $this->resourceDO->getType();
86
        if ($this->filesystem->has($filePath)) {
87
88
            return $this->XAccelRedirect(realpath($filePath), $filename, false);
89
        }
90
        /** @see \Zend\Diactoros\Response::$phrases */
91
        return new EmptyResponse(404, $headers);
92
    }
93
}