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

TouchMaterializer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 33
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A materialize() 0 13 3
A convertName() 0 4 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
}