OrmSchemaProvider::createSchema()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Provider;
6
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\Migrations\Provider\Exception\NoMappingFound;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Doctrine\ORM\Mapping\ClassMetadata;
11
use Doctrine\ORM\Tools\SchemaTool;
12
use function count;
13
use function usort;
14
15
/**
16
 * The OrmSchemaProvider class is responsible for creating a Doctrine\DBAL\Schema\Schema instance from the mapping
17
 * information provided by the Doctrine ORM. This is then used to diff against your current database schema to produce
18
 * a migration to bring your database in sync with the ORM mapping information.
19
 *
20
 * @internal
21
 */
22
final class OrmSchemaProvider implements SchemaProvider
23
{
24
    /** @var EntityManagerInterface */
25
    private $entityManager;
26
27 2
    public function __construct(EntityManagerInterface $em)
28
    {
29 2
        $this->entityManager = $em;
30 2
    }
31
32
    /**
33
     * @throws NoMappingFound
34
     */
35 2
    public function createSchema() : Schema
36
    {
37 2
        $metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();
38
39 2
        if (count($metadata) === 0) {
40 1
            throw NoMappingFound::new();
41
        }
42
43
        usort($metadata, static function (ClassMetadata $a, ClassMetadata $b) : int {
44 1
            return $a->getTableName() <=> $b->getTableName();
45 1
        });
46
47 1
        $tool = new SchemaTool($this->entityManager);
48
49 1
        return $tool->getSchemaFromMetadata($metadata);
50
    }
51
}
52