DBALMysqlResourceHelper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 40
c 0
b 0
f 0
ccs 18
cts 20
cp 0.9
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getLastIncrement() 0 21 4
1
<?php
2
3
namespace Maketok\DataMigration\Storage\Db;
4
5
class DBALMysqlResourceHelper implements ResourceHelperInterface
6
{
7
    /**
8
     * @var DBALMysqlResource
9
     */
10
    private $resource;
11
12
    /**
13
     * @param DBALMysqlResource $resource
14
     */
15 7
    public function __construct(DBALMysqlResource $resource)
16
    {
17 7
        $this->resource = $resource;
18 7
    }
19
20
    /**
21
     * {@inheritdoc
22
     */
23 3
    public function getLastIncrement($table)
24
    {
25 3
        $con = $this->resource->getConnection();
26 3
        $builder = $con->createQueryBuilder();
27 3
        $builder->select('AUTO_INCREMENT')
28 3
            ->from('INFORMATION_SCHEMA.TABLES')
29 3
            ->where('TABLE_SCHEMA=?')
30 3
            ->andWhere('TABLE_NAME=?');
31 3
        $stmt = $con->prepare($builder->getSQL());
32 3
        $stmt->bindValue(1, $con->getDatabase());
33 3
        $stmt->bindValue(2, $table);
34 3
        $stmt->execute();
35 3
        $result = $stmt->fetch(\PDO::FETCH_NUM);
36 3
        if ($result !== false && is_array($result)) {
37 3
            if (is_null($result[0])) {
38 3
                return 1;
39
            }
40
            return $result[0];
41
        }
42
        throw new \Exception("Could not fetch last increment id.");
43
    }
44
}
45