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

DirectoryPath   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 43
rs 10
c 1
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createAbsoluteOrRelativeToCodeceptionDir() 0 6 2
A __construct() 0 4 1
A ensureDirectoryExists() 0 4 2
A createAndGetInstance() 0 9 3
A asString() 0 3 1
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