DBALMysqlResourceHelper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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