Passed
Push — master ( 00bfea...b57ae9 )
by Koldo
02:36
created

CreateClassFile   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 13 4
A createDir() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\DevTools\Application\Service;
6
7
use RuntimeException;
8
9
use function file_exists;
10
use function file_put_contents;
11
use function is_dir;
12
use function mkdir;
13
use function sprintf;
14
15
class CreateClassFile
16
{
17 3
    public function __invoke(string $classDir, string $className, string $content): string
18
    {
19 3
        if (!is_dir($classDir) && $this->createDir($classDir)) {
20 1
            throw new RuntimeException(sprintf('Directory "%s" was not created', $classDir));
21
        }
22
23 2
        $realFilePath = $classDir . DIRECTORY_SEPARATOR . $className . '.php';
24 2
        if (file_exists($realFilePath)) {
25 1
            throw new RuntimeException(sprintf('File %s already exist.', $realFilePath));
26
        }
27 1
        file_put_contents($realFilePath, $content);
28
29 1
        return $realFilePath;
30
    }
31
32 1
    private function createDir(string $classDir): bool
33
    {
34 1
        return !mkdir($classDir, 0755, true) && !is_dir($classDir);
35
    }
36
}
37