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

GenerateService::generate()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 25
rs 8.8571
cc 2
eloc 17
nc 2
nop 5
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          $annotation
51
     * @param string          $type
52
     * @param array           $properties
53
     */
54
    public function generate(
55
        BundleInterface $bundle,
56
        $document,
57
        $annotation,
58
        $type,
59
        array $properties
60
    ) {
61
        $documentPath = $bundle->getPath() . '/Document/' . str_replace('\\', '/', $document) . '.php';
62
        $class = [
63
            'name' => $bundle->getNamespace() . '\\Document\\' . $document,
64
            'annotation' => $annotation,
65
            'type' => $type
66
        ];
67
68
        $class['properties'] = [];
69
70
        foreach ($properties as $property) {
71
            $class['properties'][] = $property;
72
        }
73
74
        $documentCode = $this->generator->generateDocumentClass($class);
75
76
        $this->filesystem->mkdir(dirname($documentPath));
77
        file_put_contents($documentPath, $documentCode);
78
    }
79
}
80