Completed
Pull Request — master (#111)
by Jan Philipp
02:02
created

Template::getWorkingDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Psh\Config;
4
5
use Shopware\Psh\ScriptRuntime\Execution\TemplateNotValid;
6
use function file_exists;
7
use function file_get_contents;
8
use function file_put_contents;
9
10
/**
11
 * Access template content and write the content to the target destination
12
 */
13
class Template
14
{
15
    /**
16
     * @var string
17
     */
18
    private $source;
19
20
    /**
21
     * @var string
22
     */
23
    private $destination;
24
25
    /**
26
     * @var string
27
     */
28
    private $workingDir;
29
30
    public function __construct(string $source, string $destination, string $workingDir)
31
    {
32
        $this->source = $source;
33
        $this->destination = $destination;
34
        $this->workingDir = $workingDir;
35
    }
36
37
    public function getDestination(): string
38
    {
39
        return $this->destination;
40
    }
41
42
    public function setDestination(string $destination): void
43
    {
44
        $this->destination = $destination;
45
    }
46
47
    public function getWorkingDir(): string
48
    {
49
        return $this->workingDir;
50
    }
51
52
    /**
53
     * @throws TemplateNotValid
54
     */
55
    public function getContent(): string
56
    {
57
        if (!file_exists($this->source)) {
58
            throw new TemplateNotValid('File source not found in "' . $this->source . '"');
59
        }
60
61
        return file_get_contents($this->source);
62
    }
63
64
    public function setContents(string $contents): void
65
    {
66
        $success = @file_put_contents($this->destination, $contents);
67
68
        if ($success === false) {
69
            throw new TemplateWriteNotSuccessful($this->destination);
70
        }
71
    }
72
}
73