Completed
Pull Request — master (#601)
by
unknown
14:30
created

GenerateService   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 53
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A generate() 0 20 2
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 Doctrine\ORM\Mapping\ClassMetadataInfo;
15
use ONGR\ElasticsearchBundle\Generator\DocumentGenerator;
16
use Symfony\Component\Filesystem\Filesystem;
17
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
18
19
/**
20
 * Generate Service
21
 */
22
class GenerateService
23
{
24
    /**
25
     * @var DocumentGenerator
26
     */
27
    private $generator;
28
29
    /**
30
     * @var Filesystem
31
     */
32
    private $filesystem;
33
34
    /**
35
     * Constructor
36
     *
37
     * @param DocumentGenerator $generator
38
     * @param Filesystem        $filesystem
39
     */
40
    public function __construct(DocumentGenerator $generator, Filesystem $filesystem)
41
    {
42
        $this->generator = $generator;
43
        $this->filesystem = $filesystem;
44
    }
45
46
    /**
47
     * Generates document class
48
     *
49
     * @param BundleInterface $bundle
50
     * @param string          $document
51
     * @param string          $type
52
     * @param array           $properties
53
     */
54
    public function generate(
55
        BundleInterface $bundle,
56
        $document,
57
        $type,
58
        array $properties
59
    ) {
60
        $documentPath = $bundle->getPath() . '/Document/' . str_replace('\\', '/', $document) . '.php';
61
        $class = new ClassMetadataInfo($bundle->getNamespace() . '\\Document\\' . $document);
62
63
        foreach ($properties as $property) {
64
            $class->mapField($property);
65
        }
66
67
        $documentCode = $this->generator
68
            ->setDocumentType($type)
69
            ->generateEntityClass($class);
70
71
        $this->filesystem->mkdir(dirname($documentPath));
72
        file_put_contents($documentPath, $documentCode);
73
    }
74
}
75