getCurrentTimeStamp()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
}