Passed
Push — master ( ee13c0...fd5ab9 )
by Koldo
02:31
created

CreateClassFile::__invoke()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 3
dl 0
loc 13
ccs 0
cts 11
cp 0
crap 20
rs 10
c 0
b 0
f 0
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
    public function __invoke(string $classDir, string $className, string $content): string
18
    {
19
        if (!is_dir($classDir) && $this->createDir($classDir)) {
20
            throw new RuntimeException(sprintf('Directory "%s" was not created', $classDir));
21
        }
22
23
        $realFilePath = $classDir . DIRECTORY_SEPARATOR . $className . '.php';
24
        if (file_exists($realFilePath)) {
25
            throw new RuntimeException(sprintf('File %s already exist.', $realFilePath));
26
        }
27
        file_put_contents($realFilePath, $content);
28
29
        return $realFilePath;
30
    }
31
32
    private function createDir(string $classDir): bool
33
    {
34
        return !mkdir($classDir, 0755, true) && !is_dir($classDir);
35
    }
36
}
37