CommandExecutionContext::getWorkingDirectory()   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 declare(strict_types=1);
2
3
namespace Bigwhoop\Trumpet\Commands;
4
5
use Bigwhoop\Trumpet\Exceptions\InvalidArgumentException;
6
use Bigwhoop\Trumpet\Presentation\Theming\Theme;
7
8
final class CommandExecutionContext
9
{
10
    /** @var Theme */
11
    private $theme;
12
13
    /** @var string */
14
    private $workingDir = '';
15
    
16 1
    public function __construct(Theme $theme, string $workingDirectory)
17
    {
18 1
        $this->theme = $theme;
19 1
        $this->workingDir = $workingDirectory;
20 1
    }
21
    
22
    public function getTheme(): Theme
23
    {
24
        return $this->theme;
25
    }
26
27 7
    public function getWorkingDirectory(): string
28
    {
29 7
        return $this->workingDir;
30
    }
31
32 21
    public function hasFileInWorkingDirectory(string $file): bool
33
    {
34 21
        return file_exists($this->workingDir.'/'.$file);
35
    }
36
37 8
    public function getPathOfFileInWorkingDirectory(string $file): string
38
    {
39 8
        return $this->workingDir.'/'.$file;
40
    }
41
42 14
    public function getContentsOfFileInWorkingDirectory(string $file): string
43
    {
44 14
        $path = $this->workingDir.'/'.$file;
45 14
        if (!is_readable($path)) {
46
            throw new InvalidArgumentException("File '$file' must be readable from path '{$this->workingDir}'.");
47
        }
48
49 14
        return file_get_contents($path);
50
    }
51
52 5
    public function ensureTempDirectory(): string
53
    {
54 5
        $tmpDir = $this->getWorkingDirectory().'/.tmp';
55 5
        if (!is_dir($tmpDir)) {
56 1
            mkdir($tmpDir, 0755, true);
57
        }
58
59 5
        return $tmpDir;
60
    }
61
}
62