FilePerClassWriter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 4
1
<?php
2
/**
3
 * Spiral Framework, IDE Helper
4
 *
5
 * @author    Dmitry Mironov <[email protected]>
6
 * @licence   MIT
7
 */
8
9
namespace Spiral\IdeHelper\Writers;
10
11
use Psr\Log\LoggerInterface;
12
use Spiral\Files\FilesInterface;
13
use Spiral\IdeHelper\Model\ClassDefinition;
14
use Spiral\IdeHelper\Renderer\RendererInterface;
15
16
/**
17
 * Class ClassesWriter
18
 *
19
 * @package Spiral\IdeHelper
20
 */
21
class FilePerClassWriter implements WriterInterface
22
{
23
    /**
24
     * @var FilesInterface
25
     */
26
    private $files;
27
28
    /**
29
     * @var \Psr\Log\LoggerInterface
30
     */
31
    private $logger;
32
33
    /**
34
     * @var string
35
     */
36
    private $outputDirectory;
37
38
    /**
39
     * @var RendererInterface
40
     */
41
    private $renderer;
42
43
    /**
44
     * ClassWriter constructor.
45
     *
46
     * @param FilesInterface    $files
47
     * @param LoggerInterface   $logger
48
     * @param RendererInterface $renderer
49
     * @param string            $outputDirectory
50
     */
51
    public function __construct(
52
        FilesInterface $files,
53
        LoggerInterface $logger,
54
        RendererInterface $renderer,
55
        string $outputDirectory
56
    ) {
57
        $this->files = $files;
58
        $this->logger = $logger;
59
        $this->renderer = $renderer;
60
        $this->outputDirectory = $outputDirectory;
61
    }
62
63
    /**
64
     * @param ClassDefinition[] $classes
65
     */
66
    public function write(array $classes)
67
    {
68
        foreach ($classes as $class) {
69
            $directory = $this->outputDirectory . $class->getNamespace();
70
            $directory = $this->files->normalizePath($directory);
71
            $filename = $directory . '/' . $class->getShortName() . '.php';
72
            $filename = $this->files->normalizePath($filename);
73
74
            $this->files->ensureDirectory($directory);
75
            $this->files->write($filename, $this->renderer->render([$class]));
76
77
            $this->logger->info("Create $filename file.");
78
        }
79
    }
80
}
81