Completed
Push — master ( 1ed64c...63fdb5 )
by Julián
02:29
created

CLIApplicationBuilder::getDocumentManager()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 4
nop 1
1
<?php
2
/**
3
 * Slim3 Doctrine integration (https://github.com/juliangut/slim-doctrine)
4
 *
5
 * @link https://github.com/juliangut/slim-doctrine for the canonical source repository
6
 *
7
 * @license https://raw.githubusercontent.com/juliangut/slim-doctrine/master/LICENSE
8
 */
9
10
namespace Jgut\Slim\Doctrine;
11
12
use Doctrine\ODM\MongoDB\DocumentManager;
13
use Doctrine\ODM\MongoDB\Tools\Console\Helper\DocumentManagerHelper;
14
use Doctrine\ORM\EntityManager;
15
use Doctrine\ORM\Tools\Console\ConsoleRunner;
16
17
/**
18
 * Doctrine CLI application builder.
19
 */
20
class CLIApplicationBuilder
21
{
22
    /**
23
     * Create a Doctrine CLI application.
24
     *
25
     * @param array|\Doctrine\ORM\EntityManager           $entityManager
26
     * @param array|\Doctrine\ODM\MongoDB\DocumentManager $documentManager
0 ignored issues
show
Documentation introduced by
Should the type for parameter $documentManager not be array|DocumentManager|null? Also, consider making the array more specific, something like array<String>, or String[].

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. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

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

Loading history...
27
     *
28
     * @throws \Doctrine\DBAL\DBALException
29
     * @throws \Doctrine\ORM\ORMException
30
     * @throws \InvalidArgumentException
31
     * @throws \LogicException
32
     * @throws \RuntimeException
33
     *
34
     * @return \Symfony\Component\Console\Application
35
     */
36
    public static function build($entityManager, $documentManager = null)
37
    {
38
        $entityManager = self::getEntityManager($entityManager);
39
40
        $helperSet = ConsoleRunner::createHelperSet($entityManager);
41
        $application = ConsoleRunner::createApplication($helperSet);
42
43
        if ($documentManager !== null) {
44
            $documentManager = self::getDocumentManager($documentManager);
45
46
            $helperSet->set(new DocumentManagerHelper($documentManager), 'dm');
47
48
            $application->addCommands(
49
                [
50
                    new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateDocumentsCommand(),
51
                    new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateHydratorsCommand(),
52
                    new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateProxiesCommand(),
53
                    new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateRepositoriesCommand(),
54
                    new \Doctrine\ODM\MongoDB\Tools\Console\Command\QueryCommand(),
55
                    new \Doctrine\ODM\MongoDB\Tools\Console\Command\ClearCache\MetadataCommand(),
56
                    new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\CreateCommand(),
57
                    new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\DropCommand(),
58
                    new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\UpdateCommand(),
59
                ]
60
            );
61
        }
62
63
        return $application;
64
    }
65
66
    /**
67
     * Retrieve entity manager.
68
     *
69
     * @param array|\Doctrine\ORM\EntityManager $entityManager
70
     *
71
     * @throws \InvalidArgumentException
72
     *
73
     * @return \Doctrine\ORM\EntityManager
74
     */
75
    protected static function getEntityManager($entityManager)
76
    {
77
        if (is_array($entityManager)) {
78
            $entityManager = EntityManagerBuilder::build($entityManager);
79
        }
80
81
        if (!$entityManager instanceof EntityManager) {
82
            throw new \InvalidArgumentException('Invalid Entity Manager provided');
83
        }
84
85
        return $entityManager;
86
    }
87
88
    /**
89
     * Retrieve document manager.
90
     *
91
     * @param array|\Doctrine\ODM\MongoDB\DocumentManager $documentManager
92
     *
93
     * @throws \InvalidArgumentException
94
     *
95
     * @return \Doctrine\ODM\MongoDB\DocumentManager
96
     */
97
    protected static function getDocumentManager($documentManager)
98
    {
99
        if (is_array($documentManager)) {
100
            $documentManager = DocumentManagerBuilder::build($documentManager);
101
        }
102
103
        if (!$documentManager instanceof DocumentManager) {
104
            throw new \InvalidArgumentException('Invalid Document Manager provided');
105
        }
106
107
        return $documentManager;
108
    }
109
}
110