Completed
Push — master ( 0ec757...511d0a )
by
unknown
89:40 queued 75:27
created

GatewayFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Persistence\Legacy\SharedGateway;
10
11
use Doctrine\DBAL\Connection;
12
13
/**
14
 * Builds Shared Gateway object based on the database connection.
15
 *
16
 * @internal For internal use by Legacy Storage Gateways.
17
 */
18
final class GatewayFactory
19
{
20
    /** @var \eZ\Publish\Core\Persistence\Legacy\SharedGateway\Gateway */
21
    private $fallbackGateway;
22
23
    /** @var \iterable|\eZ\Publish\Core\Persistence\Legacy\SharedGateway\Gateway[] */
24
    private $gateways;
25
26
    public function __construct(Gateway $fallbackGateway, iterable $gateways)
27
    {
28
        $this->fallbackGateway = $fallbackGateway;
29
        $this->gateways = $gateways;
30
    }
31
32
    /**
33
     * @throws \Doctrine\DBAL\DBALException
34
     */
35
    public function buildSharedGateway(Connection $connection): Gateway
36
    {
37
        return $this->getGatewayForDatabasePlatform($connection->getDatabasePlatform()->getName());
38
    }
39
40
    private function getGatewayForDatabasePlatform(string $currentDatabasePlatformName): Gateway
41
    {
42
        foreach ($this->gateways as $databasePlatformName => $gateway) {
43
            if ($currentDatabasePlatformName === $databasePlatformName) {
44
                return $gateway;
45
            }
46
        }
47
48
        return $this->fallbackGateway;
49
    }
50
}
51