Completed
Push — feature/schema-export ( 4a3675 )
by
unknown
13:50
created

SchemaExportCommand::getMongoDBVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 9
loc 9
ccs 0
cts 5
cp 0
rs 9.6666
c 1
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * generate MongoDB Fulltext-Search Indexes
4
 */
5
6
namespace Graviton\GeneratorBundle\Command;
7
8
use Doctrine\ODM\MongoDB\DocumentManager;
9
use Symfony\Bundle\FrameworkBundle\Console\Application;
10
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
11
use Symfony\Component\Filesystem\Filesystem;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
16
17
/**
18
 * Here, we generate all MongoDB Fulltext-Search Indexes
19
 *
20
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
21
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
22
 * @link     http://swisscom.ch
23
 */
24
class SchemaExportCommand extends Command
25
{
26
27
    /**
28
     * @var documentManager
29
     */
30
    private $documentManager;
31
32
    /**
33
     * @var Filesystem
34
     */
35
    private $fileSystem;
36
37
    /**
38
     * GenerateBuildIndexesCommand constructor.
39
     *
40
     * @param DocumentManager $documentManager The Doctrine Document Manager
41
     * @param Filesystem      $fileSystem      Sf file manager
42
     * @param String          $name            The Name of this Command
0 ignored issues
show
Documentation introduced by
Should the type for parameter $name not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
43
     */
44 4
    public function __construct(
45
        DocumentManager $documentManager,
46
        Filesystem $fileSystem,
47
        $name = null
48
    ) {
49 4
        parent::__construct($name);
50
51 4
        $this->documentManager = $documentManager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $documentManager of type object<Doctrine\ODM\MongoDB\DocumentManager> is incompatible with the declared type object<Graviton\Generato...ommand\documentManager> of property $documentManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52 4
        $this->fileSystem = $fileSystem;
53 4
    }
54
55
    /**
56
     * {@inheritDoc}
57
     *
58
     * @return void
59
     */
60 4
    protected function configure()
61
    {
62 4
        parent::configure();
63
64 2
        $this
65 4
            ->setName('graviton:generate:schema-export')
66 4
            ->setDescription(
67 2
                'Generates Mongo DB schema output to root folder, schema.json'
68 2
            );
69 4
    }
70
71
    /**
72
     * {@inheritDoc}
73
     *
74
     * @param InputInterface  $input  input
75
     * @param OutputInterface $output output
76
     *
77
     * @return void
78
     */
79
    protected function execute(InputInterface $input, OutputInterface $output)
80
    {
81
        // Check mongo db version
82
        $mongoVersion = $this->getMongoDBVersion('Graviton\\CoreBundle\\Document\\App');
83
        if ((float) $mongoVersion < 2.6) {
84
            $output->writeln("MongoDB Version < 2.6 installed: " . $mongoVersion);
85
            exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method execute() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
86
        }
87
88
        /** @var Application $application */
89
        $application = $this->getApplication();
90
        $container = $application->getKernel()->getContainer();
91
        $rootDir = $container->getParameter('kernel.root_dir');
92
        $baseDir = $rootDir . ((strpos($rootDir, 'vendor')) ? '/../../../../' : '/../');
93
94
        $schema = [];
95
        /** @var ClassMetadata $metadata */
96
        foreach ($this->documentManager->getMetadataFactory()->getAllMetadata() as $metadata) {
97
            $name = $metadata->getCollection();
98
            if (array_key_exists($name, $schema)) {
99
                throw new InvalidConfigurationException();
100
            }
101
            $schema[$name] = [
102
                'class'  => $metadata->getName(),
103
                'fields' => $metadata->fieldMappings,
104
                'relations' => $metadata->associationMappings
105
                ];
106
        }
107
108
        $this->fileSystem->dumpFile(
109
            $baseDir . '/schema.json',
110
            json_encode($schema, JSON_PRETTY_PRINT)
111
        );
112
    }
113
114
    /**
115
     * Gets the installed MongoDB Version
116
     * @param String $className The Classname of the collection, needed to fetch the right DB connection
117
     * @return String getMongoDBVersion The version of the MongoDB as a string
118
     */
119 View Code Duplication
    private function getMongoDBVersion($className)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        $buildInfo = $this->documentManager->getDocumentDatabase($className)->command(['buildinfo' => 1]);
122
        if (isset($buildInfo['version'])) {
123
            return $buildInfo['version'];
124
        } else {
125
            return 'unkown';
126
        }
127
    }
128
}
129