MySQL::checkTable()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Sinergi\Emails\Driver;
4
5
use PDO;
6
use Exception;
7
8
class MySQL
9
{
10
    private $connection;
11
12
    public function __construct(PDO $connection)
13
    {
14
        $this->connection = $connection;
15
    }
16
17
    public function insert($table, $data): int
18
    {
19
        $query = "INSERT INTO `{$table}` (";
20
        foreach ($data as $key => $value) {
21
            $query .= "`{$key}`, ";
22
        }
23
        $query = substr($query, 0, -2) . ') VALUES (';
24
        foreach ($data as $key => $value) {
25
            $query .= ":{$key}, ";
26
        }
27
        $query = substr($query, 0, -2) . ')';
28
29
        $sth = $this->connection->prepare($query);
30
31
        $finalParams = [];
32
        foreach ($data as $key => $value) {
33
            if (is_bool($value)) {
34
                $value = $value ? 1 : 0;
35
            }
36
            $finalParams[':' . $key] = $value;
37
        }
38
39
        $result = $sth->execute($finalParams);
40
        if (!$result) {
41
            throw new Exception('Could not insert row in ' . $table);
42
        }
43
        return $this->connection->lastInsertId();
44
    }
45
46
    public function checkTable($name): bool
47
    {
48
        try {
49
            $result = $this->connection->query("SELECT 1 FROM $name LIMIT 1");
50
        } catch (Exception $e) {
51
            return false;
52
        }
53
        return $result !== false;
54
    }
55
56
    public function createEmailsTable()
57
    {
58
        $this->createTable('emails', <<<SQL
59
CREATE TABLE `emails` (
60
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
61
  `from` text,
62
  `subject` text,
63
  `text_body` text,
64
  `html_body` text,
65
  `to` text,
66
  `cc` text,
67
  `bcc` text,
68
  `reply_to` text,
69
  `is_sent` tinyint(1) DEFAULT NULL,
70
  `has_error` tinyint(1) DEFAULT NULL,
71
  `errors` text,
72
  `sent_date_time` datetime DEFAULT NULL,
73
  `creation_date_time` datetime DEFAULT NULL,
74
  PRIMARY KEY (`id`)
75
)
76
SQL
77
        );
78
    }
79
80
    public function createAttachmentsTable()
81
    {
82
        $this->createTable('emails_attachments', <<<SQL
83
CREATE TABLE `emails_attachments` (
84
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
85
  `email_id` int(11) unsigned NOT NULL,
86
  `name` varchar(255) DEFAULT NULL,
87
  `mime_type` varchar(64) DEFAULT NULL,
88
  `content` longblob,
89
  PRIMARY KEY (`id`),
90
  KEY `email_id` (`email_id`),
91
  CONSTRAINT `email_relation` FOREIGN KEY (`email_id`) REFERENCES `emails` (`id`)
92
)
93
SQL
94
        );
95
    }
96
97
    private function createTable($name, $query)
98
    {
99
        try {
100
            $result = $this->connection->query($query);
101
        } catch (Exception $e) {
102
            throw new Exception('Could not create ' . $name . ' table');
103
        }
104
        if ($result === false) {
105
            throw new Exception('Could not create ' . $name . ' table');
106
        }
107
    }
108
}
109