Completed
Branch 2.0-dev (d250b8)
by Jan-Petter
03:02
created

SQLTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A pdoInitialize() 0 5 1
A createTable() 0 12 3
A tableExists() 0 9 2
1
<?php
2
namespace vipnytt\RobotsTxtParser\Client\SQL;
3
4
use PDO;
5
use PDOException;
6
7
/**
8
 * Class SQLTrait
9
 *
10
 * @package vipnytt\RobotsTxtParser\Client\SQL
11
 */
12
trait SQLTrait
13
{
14
    /**
15
     * Initialize PDO connection
16
     *
17
     * @param PDO $pdo
18
     * @return PDO
19
     */
20
    private function pdoInitialize(PDO $pdo)
21
    {
22
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
23
        return $pdo;
24
    }
25
26
    /**
27
     * Create table
28
     *
29
     * @param PDO $pdo
30
     * @param string $table
31
     * @param string $query
32
     * @return bool
33
     */
34
    private function createTable(PDO $pdo, $table, $query)
35
    {
36
        if ($this->tableExists($pdo, $table)) {
37
            return true;
38
        }
39
        try {
40
            $pdo->query($query);
41
        } catch (PDOException $Exception) {
42
            return FALSE;
43
        }
44
        return $this->tableExists($pdo, $table);
45
    }
46
47
    /**
48
     * Check if the table exists
49
     *
50
     * @param PDO $pdo
51
     * @param string $table
52
     * @return bool
53
     */
54
    private function tableExists(PDO $pdo, $table)
55
    {
56
        try {
57
            $result = $pdo->query("SELECT 1 FROM $table LIMIT 1;");
58
        } catch (PDOException $Exception) {
59
            return FALSE;
60
        }
61
        return $result !== FALSE;
62
    }
63
}
64