Passed
Push — master ( 6248da...8ce640 )
by László
02:40
created

BaseHandler::handle()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace CodingSocks\UploadHandler\Driver;
4
5
use Closure;
6
use CodingSocks\UploadHandler\Event\FileUploaded;
7
use CodingSocks\UploadHandler\Exception\InternalServerErrorHttpException;
8
use CodingSocks\UploadHandler\StorageConfig;
9
use Illuminate\Http\Request;
10
use Illuminate\Http\UploadedFile;
11
use Illuminate\Support\Facades\Storage;
12
use Symfony\Component\HttpFoundation\BinaryFileResponse;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
15
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
16
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17
18
abstract class BaseHandler
19
{
20
21
    /**
22
     * @param \Illuminate\Http\Request $request
23
     * @param \CodingSocks\UploadHandler\StorageConfig $config
24
     * @param \Closure|null $fileUploaded
25
     *
26
     * @return \Symfony\Component\HttpFoundation\Response
27
     *
28
     */
29
    abstract public function handle(Request $request, StorageConfig $config, Closure $fileUploaded = null): Response;
30
31
    /**
32
     * @param string $filename
33
     * @param \CodingSocks\UploadHandler\StorageConfig $storageConfig
34
     *
35
     * @return \Symfony\Component\HttpFoundation\Response
36
     */
37
    public function fileResponse(string $filename, StorageConfig $storageConfig): Response
38
    {
39
        /** @var \Illuminate\Filesystem\FilesystemAdapter $disk */
40
        $disk = Storage::disk($storageConfig->getDisk());
41
        $prefix = $storageConfig->getMergedDirectory() . '/';
42
43
        if (! $disk->exists($prefix . $filename)) {
44
            throw new NotFoundHttpException($filename . ' file not found on server');
45
        }
46
47
        $path = $disk->path($prefix . $filename);
48
49
        return new BinaryFileResponse($path, 200, [], true, ResponseHeaderBag::DISPOSITION_ATTACHMENT);
50
    }
51
52
    /**
53
     * Check if the request type of the given request is in the specified list.
54
     *
55
     * @param \Illuminate\Http\Request $request
56
     * @param array $methods
57
     *
58
     * @return bool
59
     */
60 135
    public function isRequestMethodIn(Request $request, array $methods): bool
61
    {
62 135
        foreach ($methods as $method) {
63 135
            if ($request->isMethod($method)) {
64 94
                return true;
65
            }
66
        }
67
68 103
        return false;
69
    }
70
71
    /**
72
     * Dispatch a {@link \CodingSocks\UploadHandler\Event\FileUploaded} event.
73
     * Also call the given {@link \Closure} if not null.
74
     *
75
     * @param $disk
76
     * @param $path
77
     * @param \Closure|null $fileUploaded
78
     */
79 20
    protected function triggerFileUploadedEvent($disk, $path, Closure $fileUploaded = null): void
80
    {
81 20
        if ($fileUploaded !== null) {
82 10
            $fileUploaded($disk, $path);
83
        }
84
85 20
        event(new FileUploaded($disk, $path));
86 20
    }
87
88
    /**
89
     * Validate an uploaded file. An exception is thrown when it is invalid.
90
     *
91
     * @param \Illuminate\Http\UploadedFile|null $file
92
     *
93
     * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException when given file is null.
94
     * @throws \CodingSocks\UploadHandler\Exception\InternalServerErrorHttpException when given file is invalid.
95
     */
96 88
    protected function validateUploadedFile(UploadedFile $file = null): void
97
    {
98 88
        if (null === $file) {
99 8
            throw new BadRequestHttpException('File not found in request body');
100
        }
101
102 80
        if (! $file->isValid()) {
103 8
            throw new InternalServerErrorHttpException($file->getErrorMessage());
104
        }
105 72
    }
106
}
107