Completed
Push — master ( 8e0587...afc775 )
by recca
01:27
created

Api::createChunkFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Recca0120\Upload;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\JsonResponse;
7
use Recca0120\Upload\Contracts\Api as ApiContract;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
10
abstract class Api implements ApiContract
11
{
12
    /**
13
     * $request.
14
     *
15
     * @var \Illuminate\Http\Request
16
     */
17
    protected $request;
18
19
    /**
20
     * $files.
21
     *
22
     * @var \Recca0120\Upload\Filesystem
23
     */
24
    protected $files;
25
26
    /**
27
     * $chunkFile.
28
     *
29
     * @var \Recca0120\Upload\ChunkFileFactory
30
     */
31
    protected $ChunkFileFactory;
32
33
    /**
34
     * $config.
35
     *
36
     * @var array
37
     */
38
    protected $config;
39
40
    /**
41
     * __construct.
42
     *
43
     * @param array $config
44
     * @param \Illuminate\Http\Request $request
45
     * @param \Recca0120\Upload\Filesystem $files
46
     * @param \Recca0120\Upload\ChunkFile $chunkFile
0 ignored issues
show
Documentation introduced by
There is no parameter named $chunkFile. Did you maybe mean $chunkFileFactory?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
47
     */
48 24
    public function __construct($config = [], Request $request = null, Filesystem $files = null, ChunkFileFactory $chunkFileFactory = null)
49
    {
50 24
        $this->request = $request ?: Request::capture();
51 24
        $this->files = $files ?: new Filesystem();
52 24
        $this->chunkFileFactory = $chunkFileFactory ?: new ChunkFileFactory($this->files);
0 ignored issues
show
Bug introduced by
The property chunkFileFactory does not seem to exist. Did you mean ChunkFileFactory?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
53 24
        $this->config = array_merge([
54 24
            'chunks' => sys_get_temp_dir().'/chunks',
55 24
            'storage' => 'storage/temp',
56 24
            'domain' => $this->request->root(),
57 24
            'path' => 'storage/temp',
58 24
        ], $config);
59 24
    }
60
61
    /**
62
     * domain.
63
     *
64
     * @return string
65
     */
66 1
    public function domain()
67
    {
68 1
        return rtrim($this->config['domain'], '/').'/';
69
    }
70
71
    /**
72
     * path.
73
     *
74
     * @return string
75
     */
76 1
    public function path()
77
    {
78 1
        return rtrim($this->config['path'], '/').'/';
79
    }
80
81
    /**
82
     * makeDirectory.
83
     *
84
     * @return $this
85
     */
86 11
    public function makeDirectory($path)
87
    {
88 11
        if ($this->files->isDirectory($path) === false) {
89 1
            $this->files->makeDirectory($path, 0777, true, true);
90 1
        }
91
92 11
        return $this;
93
    }
94
95
    /**
96
     * cleanDirectory.
97
     *
98
     * @param string $path
99
     * @return $this
100
     */
101 2
    public function cleanDirectory($path)
102
    {
103 2
        $time = time();
104 2
        $maxFileAge = 3600;
105 2
        $files = (array) $this->files->files($path);
106 2
        foreach ($files as $file) {
107 1
            if ($this->files->isFile($file) === true &&
108 1
                $this->files->lastModified($file) < ($time - $maxFileAge)
109 1
            ) {
110 1
                $this->files->delete($file);
111 1
            }
112 2
        }
113
114 2
        return $this;
115
    }
116
117
    /**
118
     * receive.
119
     *
120
     * @param string $inputName
121
     * @return \Symfony\Component\HttpFoundation\File\UploadedFile
122
     *
123
     * @throws \Recca0120\Upload\Exceptions\ChunkedResponseException
124
     */
125
    abstract public function receive($inputName);
126
127
    /**
128
     * deleteUploadedFile.
129
     *
130
     * @param \Symfony\Component\HttpFoundation\File\UploadedFile
131
     * @return $this
132
     */
133 1
    public function deleteUploadedFile(UploadedFile $uploadedFile)
134
    {
135 1
        $file = $uploadedFile->getPathname();
136 1
        if ($this->files->isFile($file) === true) {
137 1
            $this->files->delete($file);
138 1
        }
139 1
        $this->cleanDirectory($this->chunkPath());
140
141 1
        return $this;
142
    }
143
144
    /**
145
     * completedResponse.
146
     *
147
     * @param \Illuminate\Http\JsonResponse $response
148
     * @return \Illuminate\Http\JsonResponse
149
     */
150 1
    public function completedResponse(JsonResponse $response)
151
    {
152 1
        return $response;
153
    }
154
155
    /**
156
     * chunkPath.
157
     *
158
     * @return string
159
     */
160 10
    protected function chunkPath()
161
    {
162 10
        $this->makeDirectory($this->config['chunks']);
163
164 10
        return rtrim($this->config['chunks'], '/').'/';
165
    }
166
167
    /**
168
     * storagePath.
169
     *
170
     * @return string
171
     */
172 9
    protected function storagePath()
173
    {
174 9
        $this->makeDirectory($this->config['storage']);
175
176 9
        return rtrim($this->config['storage'], '/').'/';
177
    }
178
179
    protected function createChunkFile($name, $uuid = null)
180
    {
181
        return $this->chunkFileFactory->create(
0 ignored issues
show
Bug introduced by
The property chunkFileFactory does not seem to exist. Did you mean ChunkFileFactory?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
182
            $name, $this->chunkPath(), $this->storagePath(), $uuid
183
        );
184
    }
185
}
186