|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author stev leibelt <[email protected]> |
|
4
|
|
|
* @since 2014-05-26 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Net\Bazzline\Component\Locator\FileExistsStrategy; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class SuffixWithCurrentTimestampStrategy |
|
11
|
|
|
* @package Net\Bazzline\Component\Locator\FileExistsStrategy |
|
12
|
|
|
*/ |
|
13
|
|
|
class SuffixWithCurrentTimestampStrategy extends AbstractStrategy |
|
14
|
|
|
{ |
|
15
|
|
|
public function __construct() |
|
16
|
|
|
{ |
|
17
|
|
|
$this->currentTimeStamp = time(); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var int |
|
22
|
|
|
*/ |
|
23
|
|
|
private $currentTimeStamp; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param int $currentTimeStamp |
|
27
|
|
|
*/ |
|
28
|
|
|
public function setCurrentTimeStamp($currentTimeStamp) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->currentTimeStamp = (int) $currentTimeStamp; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return int |
|
35
|
|
|
* @throws RuntimeException |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getCurrentTimeStamp() |
|
38
|
|
|
{ |
|
39
|
|
|
if (is_null($this->currentTimeStamp)) { |
|
40
|
|
|
throw new RuntimeException( |
|
41
|
|
|
'current timestamp is mandatory' |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this->currentTimeStamp; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @throws RuntimeException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function execute() |
|
52
|
|
|
{ |
|
53
|
|
|
$name = $this->getFileName(); |
|
54
|
|
|
$path = $this->getFilePath(); |
|
55
|
|
|
|
|
56
|
|
|
$newName = $path . DIRECTORY_SEPARATOR . $name . '.' . $this->getCurrentTimeStamp(); |
|
57
|
|
|
$oldName = $path . DIRECTORY_SEPARATOR . $name; |
|
58
|
|
|
|
|
59
|
|
|
$fileCouldNotBeMoved = ((rename($oldName, $newName)) === false); |
|
60
|
|
|
|
|
61
|
|
|
if ($fileCouldNotBeMoved) { |
|
62
|
|
|
throw new RuntimeException( |
|
63
|
|
|
'could not move file from "' . $oldName . '" to "' . $newName . '"' |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |