TemporaryFactory::makeDirectory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 8
rs 9.4286
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of peridot-temporary-plugin.
5
 *
6
 * (c) Noritaka Horio <[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 holyshared\peridot\temporary;
13
14
use Ramsey\Uuid\Uuid;
15
16
/**
17
 * TemporaryFactory
18
 *
19
 * @package holyshared\peridot\temporary
20
 */
21
final class TemporaryFactory
22
{
23
24
    /**
25
     * Create a new temporary directory
26
     *
27
     * @param int $mode permission
28
     * @return \holyshared\peridot\temporary\TemporaryDirectory
29
     */
30
    public function makeDirectory($mode = FileSystemPermission::NORMAL)
31
    {
32
        $directoryId = $this->generateId();
33
        $path = sys_get_temp_dir() . '/' . $directoryId;
34
        $directory = new TemporaryDirectory($path, $mode);
35
36
        return $directory;
37
    }
38
39
    /**
40
     * Create a new temporary file
41
     *
42
     * @param int $mode permission
43
     * @return \holyshared\peridot\temporary\TemporaryFile
44
     */
45
    public function makeFile($mode = FileSystemPermission::NORMAL)
46
    {
47
        $fileId = $this->generateId();
48
        $filePath = sys_get_temp_dir() . '/' . $fileId;
49
        $file = new TemporaryFile($filePath, $mode);
50
51
        return $file;
52
    }
53
54
    private function generateId()
55
    {
56
        $uuid4 = Uuid::uuid4();
57
        return $uuid4->toString();
58
    }
59
60
}
61