Completed
Push — master ( a44b62...afcee9 )
by Tomáš
02:35
created

File::getRelativeUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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 SplFileInfo;
13
14
class File
15
{
16
    /**
17
     * @var SplFileInfo
18
     */
19
    protected $fileInfo;
20
21
    /**
22
     * @var string
23
     */
24
    private $relativeSource;
25
26
    /**
27
     * @var string
28
     */
29
    private $outputPath;
30
31
    /**
32
     * @var string
33
     */
34
    private $content;
35
36
    /**
37
     * @var array
38
     */
39
    private $configuration = [];
40
41 20
    public function __construct(SplFileInfo $fileInfo, string $relativeSource)
42
    {
43 20
        $this->relativeSource = $relativeSource;
44 20
        $this->fileInfo = $fileInfo;
45 20
        $this->content = file_get_contents($fileInfo->getRealPath());
46 20
    }
47
48 10
    public function setOutputPath(string $outputPath)
49
    {
50 10
        $this->outputPath = $outputPath;
51 10
    }
52
53 1
    public function getRelativeSource() : string
54
    {
55 1
        return $this->relativeSource;
56
    }
57
58 12
    public function getBaseName() : string
59
    {
60 12
        return $this->fileInfo->getBasename('.'.$this->fileInfo->getExtension());
61
    }
62
63 5
    public function getPrimaryExtension() : string
64
    {
65 5
        $fileParts = explode('.', $this->fileInfo->getBasename());
66 5
        if (count($fileParts) > 2) {
67 1
            return $fileParts[count($fileParts) - 2];
68
        }
69
70 4
        return $fileParts[count($fileParts) - 1];
71
    }
72
73 6
    public function getExtension() : string
74
    {
75 6
        return $this->fileInfo->getExtension();
76
    }
77
78 5
    public function getRelativeUrl() : string
79
    {
80 5
        if ($position = strpos($this->outputPath, '/index.html')) {
81 3
            return substr($this->outputPath, 0, $position);
82
        }
83
84 2
        return $this->outputPath;
85
    }
86
87 12
    public function getContent() : string
88
    {
89 12
        return $this->content;
90
    }
91
92 9
    public function changeContent(string $newContent)
93
    {
94 9
        $this->content = $newContent;
95 9
    }
96
97 4
    public function setConfiguration(array $configuration)
98
    {
99 4
        $this->configuration = $configuration;
100 4
    }
101
102 9
    public function getConfiguration() : array
103
    {
104 9
        return $this->configuration;
105
    }
106
107 5
    public function getOutputPath() : string
108
    {
109 5
        return $this->outputPath;
110
    }
111
112 8
    public function getLayout() : string
113
    {
114 8
        return $this->configuration['layout'] ?? '';
115
    }
116
}
117