1
|
|
|
<?php |
2
|
|
|
/******************************************************************* |
3
|
|
|
* Created by: Marko Kungla @ OkramLabs on aug 6, 2012 - 9:11:06 |
4
|
|
|
* Contact: [email protected] - https://okramlabs.com |
5
|
|
|
* @copyright 2015 OkramLabs - https://okramlabs.com |
6
|
|
|
* @license MIT |
7
|
|
|
* |
8
|
|
|
* Package name:libhowi-filesystem |
9
|
|
|
* @category HOWI3 |
10
|
|
|
* @package libhowi |
11
|
|
|
* @subpackage filesystem |
12
|
|
|
* |
13
|
|
|
* Lang: PHP |
14
|
|
|
* Encoding: UTF-8 |
15
|
|
|
* File: TmpTrait.inc |
16
|
|
|
* @link https:// |
17
|
|
|
******************************************************************** |
18
|
|
|
* Contributors: |
19
|
|
|
* @author Marko Kungla <[email protected]> |
20
|
|
|
* Github: https://github.com/mkungla |
21
|
|
|
******************************************************************** |
22
|
|
|
* Comments: |
23
|
|
|
*/ |
24
|
|
|
namespace HOWI3\libhowi\Filesystem\php7\Objects; |
25
|
|
|
|
26
|
|
|
use HOWI3\libhowi\Filesystem\Commons\ObjectInterfaces\TmpInterface; |
27
|
|
|
use \HOWI3\libhowi\Filesystem\Commons\TraitForResponse; |
28
|
|
|
use \HOWI3\libhowi\Filesystem\php7\TraitForSharedMethods; |
29
|
|
|
|
30
|
|
View Code Duplication |
class TmpObject implements TmpInterface |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
use TraitForResponse; |
34
|
|
|
use TraitForSharedMethods; |
35
|
|
|
|
36
|
|
|
private $tmp_path; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
241 |
|
public function setTmp($path = false) |
|
|
|
|
44
|
|
|
{ |
45
|
241 |
|
if (! empty($path)) { |
46
|
3 |
|
$tmp = $this->makeAbsolute($path); |
47
|
3 |
|
$isWritable = $this->isWritable($tmp); |
|
|
|
|
48
|
3 |
|
$this->tmp_path = $isWritable ? $tmp : $this->tmp_path; |
49
|
|
|
|
50
|
3 |
|
! empty($isWritable) ? $this->debug(803) : $this->warning(501, $tmp); |
|
|
|
|
51
|
3 |
|
$this->response()->setStatus($isWritable); |
52
|
|
|
} else { |
53
|
241 |
|
$this->tmp_path = sys_get_temp_dir(); |
54
|
|
|
|
55
|
241 |
|
$this->debug(804); |
56
|
241 |
|
$this->response()->setStatus(true); |
57
|
241 |
|
$isWritable = true; |
58
|
|
|
} |
59
|
241 |
|
return $isWritable; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
* |
66
|
|
|
*/ |
67
|
2 |
|
public function getTmp() |
68
|
|
|
{ |
69
|
2 |
|
if (empty($this->tmp_path)) |
70
|
|
|
$this->tmp_path = $this->setTmp(); |
71
|
|
|
|
72
|
2 |
|
$this->debug(805, $this->tmp_path); |
73
|
2 |
|
$this->response()->setStatus(true); |
74
|
2 |
|
return $this->tmp_path; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
* |
81
|
|
|
*/ |
82
|
1 |
|
public function tempnam($dir = false, $prefix = 'howi-fs-tmp-') |
83
|
|
|
{ |
84
|
1 |
|
$dir = empty($dir) ? $this->getTmp() : $this->makeAbsolute($dir); |
85
|
|
|
|
86
|
1 |
|
$tmpname = tempnam($dir, $prefix); |
87
|
|
|
|
88
|
1 |
|
$this->debug(806, $tmpname); |
89
|
1 |
|
$this->response()->setStatus(true); |
90
|
|
|
|
91
|
1 |
|
return $tmpname; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* |
96
|
|
|
* {@inheritDoc} |
97
|
|
|
* |
98
|
|
|
*/ |
99
|
1 |
|
public function tmpfile() |
100
|
|
|
{ |
101
|
1 |
|
$this->debug(807); |
102
|
1 |
|
$tmpfie = tmpfile(); |
103
|
1 |
|
$this->response()->setStatus(! empty($tmpfie) ? true : false); |
104
|
1 |
|
return $tmpfie; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.