PdoFactory::optimizer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\RfcLinc\DataGateway\Pdo;
6
7
use PDO;
8
use PhpCfdi\RfcLinc\DataGateway\CatalogGatewayInterface;
9
use PhpCfdi\RfcLinc\DataGateway\FactoryInterface;
10
use PhpCfdi\RfcLinc\DataGateway\ListedRfcGatewayInterface;
11
use PhpCfdi\RfcLinc\DataGateway\NullOptimizer;
12
use PhpCfdi\RfcLinc\DataGateway\OptimizerInterface;
13
use PhpCfdi\RfcLinc\DataGateway\RfcLogGatewayInterface;
14
15
class PdoFactory implements FactoryInterface
16
{
17
    /** @var PDO */
18
    private $pdo;
19
20
    /** @var OptimizerInterface */
21
    private $optimizer;
22
23
    /** @var CatalogGatewayInterface|null  */
24
    private $catalog = null;
25
26
    /** @var ListedRfcGatewayInterface|null  */
27
    private $listedRfc = null;
28
29
    /** @var RfcLogGatewayInterface */
30
    private $rfcLog = null;
31
32 39
    public function __construct(PDO $pdo, OptimizerInterface $optimizer = null)
33
    {
34 39
        $this->pdo = $pdo;
35 39
        if (null === $optimizer) {
36 39
            $optimizer = $this->createOptimizerByDriver($pdo);
37
        }
38 39
        $this->optimizer = $optimizer;
39 39
    }
40
41 39
    public function createOptimizerByDriver(PDO $pdo): OptimizerInterface
42
    {
43 39
        $driver = (string) $pdo->getAttribute($pdo::ATTR_DRIVER_NAME);
44 39
        if (in_array($driver, ['sqlite', 'pgsql', 'mysql'], true)) {
45 39
            return new TransactionOptimizer($pdo);
46
        }
47
        return new NullOptimizer();
48
    }
49
50 1
    public function pdo(): PDO
51
    {
52 1
        return $this->pdo;
53
    }
54
55 11
    public function catalog(): CatalogGatewayInterface
56
    {
57 11
        if (null === $this->catalog) {
58 11
            $this->catalog = new CatalogGateway($this->pdo);
59
        }
60 11
        return $this->catalog;
61
    }
62
63 13
    public function listedRfc(): ListedRfcGatewayInterface
64
    {
65 13
        if (null === $this->listedRfc) {
66 13
            $this->listedRfc = new ListedRfcGateway($this->pdo);
67
        }
68 13
        return $this->listedRfc;
69
    }
70
71 6
    public function rfclog(): RfcLogGatewayInterface
72
    {
73 6
        if (null === $this->rfcLog) {
74 6
            $this->rfcLog = new RfcLogGateway($this->pdo);
75
        }
76 6
        return $this->rfcLog;
77
    }
78
79 3
    public function optimizer(): OptimizerInterface
80
    {
81 3
        return $this->optimizer;
82
    }
83
}
84