Completed
Push — master ( 8295bf...54c8d8 )
by Tomáš
04:39 queued 02:22
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 11
    public function __construct(SplFileInfo $fileInfo, string $relativeSource)
42
    {
43 11
        $this->relativeSource = $relativeSource;
44 11
        $this->fileInfo = $fileInfo;
45 11
        $this->content = file_get_contents($fileInfo->getRealPath());
46 11
    }
47
48 4
    public function setOutputPath(string $outputPath)
49
    {
50 4
        $this->outputPath = $outputPath;
51 4
    }
52
53
    public function getRelativeSource() : string
54
    {
55
        return $this->relativeSource;
56
    }
57
58 5
    public function getBaseName() : string
59
    {
60 5
        return $this->fileInfo->getBasename('.'.$this->fileInfo->getExtension());
61
    }
62
63 3
    public function getPrimaryExtension() : string
64
    {
65 3
        $fileParts = explode('.', $this->fileInfo->getBasename());
66 3
        if (count($fileParts) > 2) {
67
            return $fileParts[count($fileParts) - 2];
68
        }
69
70 3
        return $fileParts[count($fileParts) - 1];
71
    }
72
73 2
    public function getExtension() : string
74
    {
75 2
        return $this->fileInfo->getExtension();
76
    }
77
78 3
    public function getRelativeUrl() : string
79
    {
80 3
        if ($position = strpos($this->outputPath, '/index.html')) {
81 2
            return substr($this->outputPath, 0, $position);
82
        }
83
84 1
        return $this->outputPath;
85
    }
86
87 7
    public function getContent() : string
88
    {
89 7
        return $this->content;
90
    }
91
92 4
    public function changeContent(string $newContent)
93
    {
94 4
        $this->content = $newContent;
95 4
    }
96
97 2
    public function setConfiguration(array $configuration)
98
    {
99 2
        $this->configuration = $configuration;
100 2
    }
101
102 4
    public function getConfiguration() : array
103
    {
104 4
        return $this->configuration;
105
    }
106
107 1
    public function getOutputPath() : string
108
    {
109 1
        return $this->outputPath;
110
    }
111
112 2
    public function getLayout() : string
113
    {
114 2
        return $this->configuration['layout'] ?? 'default';
115
    }
116
}
117