Completed
Pull Request — master (#601)
by
unknown
02:39
created

GenerateService::generate()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 2
eloc 16
nc 2
nop 4
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchBundle\Service;
13
14
use ONGR\ElasticsearchBundle\Generator\DocumentGenerator;
15
use Symfony\Component\Filesystem\Filesystem;
16
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
17
18
/**
19
 * Generate Service
20
 */
21
class GenerateService
22
{
23
    /**
24
     * @var DocumentGenerator
25
     */
26
    private $generator;
27
28
    /**
29
     * @var Filesystem
30
     */
31
    private $filesystem;
32
33
    /**
34
     * Constructor
35
     *
36
     * @param DocumentGenerator $generator
37
     * @param Filesystem        $filesystem
38
     */
39
    public function __construct(DocumentGenerator $generator, Filesystem $filesystem)
40
    {
41
        $this->generator = $generator;
42
        $this->filesystem = $filesystem;
43
    }
44
45
    /**
46
     * Generates document class
47
     *
48
     * @param BundleInterface $bundle
49
     * @param string          $document
50
     * @param string          $type
51
     * @param array           $properties
52
     */
53
    public function generate(
54
        BundleInterface $bundle,
55
        $document,
56
        $type,
57
        array $properties
58
    ) {
59
        $documentPath = $bundle->getPath() . '/Document/' . str_replace('\\', '/', $document) . '.php';
60
        $class = [
61
            'name' => $bundle->getNamespace() . '\\Document\\' . $document,
62
            'annotation' => 'Document',
63
            'type' => $type
64
        ];
65
66
        $class['properties'] = [];
67
68
        foreach ($properties as $property) {
69
            $class['properties'][] = $property;
70
        }
71
72
        $documentCode = $this->generator->generateDocumentClass($class);
73
74
        $this->filesystem->mkdir(dirname($documentPath));
75
        file_put_contents($documentPath, $documentCode);
76
    }
77
}
78