Completed
Push — master ( d42705...573ee7 )
by Tomáš
05:03 queued 02:39
created

PostFile::ensurePathStartsWithDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\PHP7_Sculpin\Renderable\File;
11
12
use DateTimeInterface;
13
use Exception;
14
use SplFileInfo;
15
use Symplify\PHP7_Sculpin\Utils\PathAnalyzer;
16
17
final class PostFile extends File
18
{
19
    /**
20
     * @var DateTimeInterface
21
     */
22
    private $date;
23
24
    /**
25
     * @var string
26
     */
27
    private $filenameWithoutDate;
28
29 2
    public function __construct(SplFileInfo $fileInfo, string $relativeSource)
30
    {
31 2
        parent::__construct($fileInfo, $relativeSource);
32 2
        $this->ensurePathStartsWithDate($fileInfo);
33 1
        $this->date = PathAnalyzer::detectDate($fileInfo);
34 1
        $this->filenameWithoutDate = PathAnalyzer::detectFilenameWithoutDate($fileInfo);
35 1
    }
36
37 1
    public function getDate() : DateTimeInterface
38
    {
39 1
        return $this->date;
40
    }
41
42 1
    public function getDateInFormat(string $format) : string
43
    {
44 1
        return $this->date->format($format);
45
    }
46
47 1
    public function getFilenameWithoutDate() : string
48
    {
49 1
        return $this->filenameWithoutDate;
50
    }
51
52 2
    private function ensurePathStartsWithDate(SplFileInfo $fileInfo)
53
    {
54 2
        if (!PathAnalyzer::startsWithDate($fileInfo)) {
55 1
            throw new Exception(
56 1
                'Post name has to start with a date in "Y-m-d" format. E.g. "2016-01-01-name.md".'
57
            );
58
        }
59 1
    }
60
}
61