Completed
Push — ezp-31088-refactor-content-mod... ( ab3ba3 )
by
unknown
13:24 queued 33s
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
use Traversable;
13
14
/**
15
 * Builds Shared Gateway object based on the database connection.
16
 *
17
 * @internal For internal use by Legacy Storage Gateways.
18
 */
19
final class GatewayFactory
20
{
21
    /** @var \eZ\Publish\Core\Persistence\Legacy\SharedGateway\Gateway */
22
    private $fallbackGateway;
23
24
    /** @var \Traversable|\eZ\Publish\Core\Persistence\Legacy\SharedGateway\Gateway[] */
25
    private $gateways;
26
27
    public function __construct(Gateway $fallbackGateway, Traversable $gateways)
28
    {
29
        $this->fallbackGateway = $fallbackGateway;
30
        $this->gateways = $gateways;
31
    }
32
33
    /**
34
     * @throws \Doctrine\DBAL\DBALException
35
     */
36
    public function buildSharedGateway(Connection $connection): Gateway
37
    {
38
        return $this->getGatewayForDatabasePlatform($connection->getDatabasePlatform()->getName());
39
    }
40
41
    private function getGatewayForDatabasePlatform(string $currentDatabasePlatformName)
42
    {
43
        foreach ($this->gateways as $databasePlatformName => $gateway) {
44
            if ($currentDatabasePlatformName === $databasePlatformName) {
45
                return $gateway;
46
            }
47
        }
48
49
        return $this->fallbackGateway;
50
    }
51
}
52