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

Script::getWorkingDirectory()   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\Listing;
4
5
use function getmypid;
6
use function mb_strpos;
7
use function pathinfo;
8
9
/**
10
 * ValueObject containing all information for a single script to be executed
11
 */
12
class Script
13
{
14
    /**
15
     * @var string
16
     */
17
    private $directory;
18
19
    /**
20
     * @var string
21
     */
22
    private $scriptName;
23
24
    /**
25
     * @var string
26
     */
27
    private $environment;
28
29
    /**
30
     * @var string
31
     */
32
    public $description;
33
34
    /**
35
     * @var bool
36
     */
37
    private $isHiddenPath;
38
39
    /**
40
     * @var string
41
     */
42
    private $workingDirectory;
43
44
    public function __construct(
45
        string $directory,
46
        string $scriptName,
47
        bool $isHiddenPath,
48
        string $workingDirectory,
49
        ?string $environment = null,
50
        string $description = ''
51
    ) {
52
        $this->directory = $directory;
53
        $this->scriptName = $scriptName;
54
        $this->environment = $environment;
55
        $this->description = $description;
56
        $this->isHiddenPath = $isHiddenPath;
57
        $this->workingDirectory = $workingDirectory;
58
    }
59
60
    public function getTmpPath(): string
61
    {
62
        return sprintf('%s/.tmp_%s_%s', $this->directory, getmypid(), $this->scriptName);
63
    }
64
65
    public function getPath(): string
66
    {
67
        return $this->directory . '/' . $this->scriptName;
68
    }
69
70
    public function getDirectory(): string
71
    {
72
        return $this->directory;
73
    }
74
75
    public function getName(): string
76
    {
77
        $name = pathinfo($this->scriptName, PATHINFO_FILENAME);
78
79
        if (!$this->environment) {
80
            return $name;
81
        }
82
83
        return $this->environment . ':' . $name;
84
    }
85
86
    /**
87
     * @return string|null
88
     */
89
    public function getEnvironment()
90
    {
91
        return $this->environment;
92
    }
93
94
    public function getDescription(): string
95
    {
96
        return $this->description;
97
    }
98
99
    public function isHidden(): bool
100
    {
101
        return $this->isHiddenPath || mb_strpos($this->scriptName, '.') === 0;
102
    }
103
104
    public function getWorkingDirectory(): string
105
    {
106
        return $this->workingDirectory;
107
    }
108
}
109