Completed
Push — master ( 06a7b2...aedaa6 )
by Valentin
03:01
created

TouchMaterializer::convertName()   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 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Cycle\ORM\Promise\Materizalizer;
5
6
use Cycle\ORM\Promise\Declaration\Declaration;
7
use Cycle\ORM\Promise\MaterializerInterface;
8
9
class TouchMaterializer implements MaterializerInterface
10
{
11
    /** @var string */
12
    private $directory;
13
14
    public function __construct(string $directory)
15
    {
16
        $this->directory = $directory;
17
    }
18
19
    /**
20
     * @todo add file changes check
21
     * {@inheritdoc}
22
     */
23
    public function materialize(string $code, Declaration $declaration): void
24
    {
25
        if (class_exists($declaration->class->getNamespacesName())) {
26
            throw new \RuntimeException("Class `{$declaration->class->getNamespacesName()}` already exists.");
27
        }
28
29
        $filename = $this->directory . DIRECTORY_SEPARATOR . $this->convertName($declaration);
30
        if (file_exists($filename)) {
31
            throw new \RuntimeException("Filename `$filename` already exists.");
32
        }
33
34
        file_put_contents($filename, $code);
35
    }
36
37
    private function convertName(Declaration $declaration): string
38
    {
39
        return str_replace('\\', '', $declaration->class->getNamespacesName());
40
    }
41
}