DatetimeNamingStrategy::getFileFormat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace FileNamingResolver\NamingStrategy;
4
5
use FileNamingResolver\FileInfo;
6
7
/**
8
 * @author Victor Bocharsky <[email protected]>
9
 */
10
class DatetimeNamingStrategy extends AbstractNamingStrategy
11
{
12
    const FORMAT_DIR_YEAR_MONTH_DAY = 'Y/m/d';
13
    const FORMAT_DIR_YEAR_MONTH = 'Y/m';
14
    const FORMAT_DIR_YEAR = 'Y';
15
    const FORMAT_FILE_DAY_TIME_MICROSECONDS = 'd-H-i-s-u';
16
    const FORMAT_FILE_DAY_TIME_SECONDS = 'd-H-i-s';
17
    const FORMAT_FILE_TIME_MICROSECONDS = 'H-i-s-u';
18
    const FORMAT_FILE_TIME_SECONDS = 'H-i-s';
19
    const FORMAT_FILE_TIMESTAMP_MICROSECONDS = 'U-u';
20
    const FORMAT_FILE_TIMESTAMP_SECONDS = 'U';
21
22
    /**
23
     * @var string Datetime directory format
24
     */
25
    protected $dirFormat;
26
27
    /**
28
     * @var string Datetime file format
29
     */
30
    protected $fileFormat;
31
32
    /**
33
     * @param string $dirFormat
34
     * @param string $fileFormat
35
     */
36 4
    public function __construct($dirFormat = self::FORMAT_DIR_YEAR_MONTH, $fileFormat = self::FORMAT_FILE_TIME_MICROSECONDS)
37
    {
38 4
        $this->dirFormat = (string)$dirFormat;
39 4
        $this->fileFormat = (string)$fileFormat;
40 4
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public function provideName(FileInfo $srcFileInfo)
46
    {
47 1
        $datetime = \DateTime::createFromFormat('U.u', microtime(true));
48
49 1
        $pathSuffix = FileInfo::purifyPath($datetime->format($this->dirFormat));
50
        $dstFileInfo = $srcFileInfo
51 1
            ->changePath($srcFileInfo->getPath().FileInfo::SEPARATOR_DIRECTORY.$pathSuffix)
52 1
            ->changeBasename($datetime->format($this->fileFormat))
53 1
        ;
54
55 1
        return $dstFileInfo;
56
    }
57
58
    /**
59
     * @return string
60
     */
61 1
    public function getDirFormat()
62
    {
63 1
        return $this->dirFormat;
64
    }
65
66
    /**
67
     * @return string
68
     */
69 1
    public function getFileFormat()
70
    {
71 1
        return $this->fileFormat;
72
    }
73
}
74