Issues (9)

src/Responses/StreamResponse.php (1 issue)

Labels
Severity
1
<?php
2
3
/*
4
 * This file is part of the overtrue/http.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\Http\Responses;
13
14
use Overtrue\Http\Exceptions\InvalidArgumentException;
15
use Overtrue\Http\Support\File;
0 ignored issues
show
The type Overtrue\Http\Support\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
/**
18
 * Class StreamResponse.
19
 *
20
 * @author overtrue <[email protected]>
21
 */
22
class StreamResponse extends Response
23
{
24
    /**
25
     * @param string $directory
26
     * @param string $filename
27
     *
28
     * @throws \Overtrue\Http\Exceptions\InvalidArgumentException
29
     *
30
     * @return string
31
     */
32
    public function save(string $directory, string $filename = ''): string
33
    {
34
        $this->getBody()->rewind();
35
36
        $directory = rtrim($directory, '/');
37
38
        if (!is_dir($directory)) {
39
            mkdir($directory, 0755, true); // @codeCoverageIgnore
40
        }
41
42
        if (!is_writable($directory)) {
43
            throw new InvalidArgumentException(sprintf("'%s' is not writable.", $directory));
44
        }
45
46
        $contents = $this->getBody()->getContents();
47
48
        if (empty($filename)) {
49
            if (preg_match('/filename="(?<filename>.*?)"/', $this->getHeaderLine('Content-Disposition'), $match)) {
50
                $filename = $match['filename'];
51
            } else {
52
                $filename = md5($contents);
53
            }
54
        }
55
56
        if (empty(pathinfo($filename, PATHINFO_EXTENSION))) {
57
            $filename .= File::getStreamExt($contents);
58
        }
59
60
        file_put_contents($directory.'/'.$filename, $contents);
61
62
        return $filename;
63
    }
64
65
    /**
66
     * @param string $directory
67
     * @param string $filename
68
     *
69
     * @throws \Overtrue\Http\Exceptions\InvalidArgumentException
70
     *
71
     * @return string
72
     */
73
    public function saveAs(string $directory, string $filename): string
74
    {
75
        return $this->save($directory, $filename);
76
    }
77
}
78