Completed
Push — master ( ab7ebf...740609 )
by Grégoire
02:23
created

ConfigurationFile   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
c 1
b 0
f 1
dl 0
loc 33
ccs 12
cts 12
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getEntityManager() 0 17 4
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Configuration\EntityManager;
6
7
use Doctrine\Migrations\Configuration\EntityManager\Exception\FileNotFound;
8
use Doctrine\Migrations\Configuration\EntityManager\Exception\InvalidConfiguration;
9
use Doctrine\ORM\EntityManagerInterface;
10
use function file_exists;
11
12
/**
13
 * This class will return an EntityManager instance, loaded from a configuration file provided as argument.
14
 */
15
final class ConfigurationFile implements EntityManagerLoader
16
{
17
    /** @var string */
18
    private $filename;
19
20 4
    public function __construct(string $filename)
21
    {
22 4
        $this->filename = $filename;
23 4
    }
24
25
    /**
26
     * Read the input and return a Configuration, returns null if the config
27
     * is not supported.
28
     *
29
     * @throws InvalidConfiguration
30
     */
31 4
    public function getEntityManager() : EntityManagerInterface
32
    {
33 4
        if (! file_exists($this->filename)) {
34 1
            throw FileNotFound::new($this->filename);
35
        }
36
37 3
        $params = include $this->filename;
38
39 3
        if ($params instanceof EntityManagerInterface) {
40 1
            return $params;
41
        }
42
43 2
        if ($params instanceof EntityManagerLoader) {
44 1
            return $params->getEntityManager();
45
        }
46
47 1
        throw InvalidConfiguration::invalidArrayConfiguration();
48
    }
49
}
50