Passed
Pull Request — master (#3)
by Mariano
01:32
created

DirectoryPath::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Mcustiel\Phiremock\Codeception\Util;
4
5
use Codeception\Configuration;
6
use Codeception\Exception\ConfigurationException;
7
use RuntimeException;
8
9
class DirectoryPath
10
{
11
    /** @var string */
12
    private $path;
13
14
    /** @throws ConfigurationException */
15
    public function __construct(string $path)
16
    {
17
        $this->ensureDirectoryExists($path);
18
        $this->path = $path;
19
    }
20
21
    public function asString(): string
22
    {
23
        return $this->path;
24
    }
25
26
    /** @throws ConfigurationException */
27
    public static function createAndGetInstance(string $path): self
28
    {
29
        if (!is_dir($path)) {
30
            $created = mkdir($path);
31
            if (!$created) {
32
                throw new RuntimeException('Could not create directory ' . $path);
33
            }
34
        }
35
        return new self($path);
36
    }
37
38
    /** @throws ConfigurationException */
39
    public static function createAbsoluteOrRelativeToCodeceptionDir(string $path): self
40
    {
41
        if (substr($path, 0, 1) === '/') {
42
            return new self($path);
43
        }
44
        return new self(Configuration::projectDir() . $path);
45
    }
46
47
    /** @throws ConfigurationException */
48
    private function ensureDirectoryExists(string $path): void
49
    {
50
        if (!is_dir($path)) {
51
            throw new ConfigurationException('Could not find the configured expectations path: ' . $path);
52
        }
53
    }
54
}
55