1
|
|
|
<?php |
2
|
|
|
namespace Kir\Services\Cmd\Dispatcher\AttributeRepositories; |
3
|
|
|
|
4
|
|
|
use PDO; |
5
|
|
|
use Exception; |
6
|
|
|
use PDOStatement; |
7
|
|
|
use Kir\Services\Cmd\Dispatcher\AttributeRepository; |
8
|
|
|
use Kir\Services\Cmd\Dispatcher\Dispatchers\DefaultDispatcher\Service; |
9
|
|
|
|
10
|
|
|
class MySQLAttributeRepository implements AttributeRepository { |
11
|
|
|
/** @var PDO */ |
12
|
|
|
private $pdo = null; |
13
|
|
|
/** @var PDOStatement */ |
14
|
|
|
private $selectServices = null; |
15
|
|
|
/** @var PDOStatement */ |
16
|
|
|
private $hasService = null; |
17
|
|
|
/** @var PDOStatement */ |
18
|
|
|
private $insertService = null; |
19
|
|
|
/** @var PDOStatement */ |
20
|
|
|
private $updateService = null; |
21
|
|
|
/** @var PDOStatement */ |
22
|
|
|
private $updateTryDate = null; |
23
|
|
|
/** @var PDOStatement */ |
24
|
|
|
private $updateRunDate = null; |
25
|
|
|
/** @var array */ |
26
|
|
|
private $services = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param PDO $pdo |
30
|
|
|
* @param string $tableName |
31
|
|
|
*/ |
32
|
|
|
public function __construct(PDO $pdo, $tableName = 'services') { |
33
|
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
34
|
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS `{$tableName}` (`service_key` VARCHAR(255) NOT NULL DEFAULT '', `service_last_try` DATETIME NULL DEFAULT '2000-01-01 00:00:00', `service_last_run` DATETIME NULL DEFAULT '2000-01-01 00:00:00', `service_timeout` INT UNSIGNED NULL DEFAULT '0', PRIMARY KEY (`service_key`));"); |
35
|
|
|
|
36
|
|
|
$this->selectServices = $pdo->prepare("SELECT service_key FROM `{$tableName}` WHERE DATE_ADD(service_last_run, INTERVAL service_timeout SECOND) ORDER BY GREATEST(service_last_try, service_last_run) ASC;"); |
37
|
|
|
$this->hasService = $pdo->prepare("SELECT COUNT(*) FROM `{$tableName}` WHERE service_key=:key;"); |
38
|
|
|
$this->insertService = $pdo->prepare("INSERT INTO `{$tableName}` (service_key, service_last_try, service_last_run, service_timeout) VALUES (:key, :try, :run, :timeout);"); |
39
|
|
|
$this->updateService = $pdo->prepare("UPDATE `{$tableName}` SET service_timeout=:timeout WHERE service_key=:key;"); |
40
|
|
|
$this->updateTryDate = $pdo->prepare("UPDATE `{$tableName}` SET service_last_try=NOW() WHERE service_key=:key;"); |
41
|
|
|
$this->updateRunDate = $pdo->prepare("UPDATE `{$tableName}` SET service_last_run=NOW() WHERE service_key=:key;"); |
42
|
|
|
$this->pdo = $pdo; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $key |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function has($key) { |
|
|
|
|
50
|
|
|
$this->hasService->bindValue('key', $key); |
51
|
|
|
$this->hasService->execute(); |
52
|
|
|
$count = $this->hasService->fetchColumn(0); |
53
|
|
|
return $count > 0; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $key |
58
|
|
|
* @param int $timeout |
59
|
|
|
* @param array $data |
60
|
|
|
* @throws Exception |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function store($key, $timeout, array $data = array()) { |
|
|
|
|
64
|
|
|
$key = trim(strtolower($key)); |
65
|
|
|
if(!in_array($key, $this->services)) { |
66
|
|
|
$this->services[] = $key; |
67
|
|
|
} else { |
68
|
|
|
throw new Exception("Duplicate service: {$key}"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if($this->has($key)) { |
72
|
|
|
$this->updateService->bindValue('key', $key); |
73
|
|
|
$this->updateService->bindValue('timeout', $timeout); |
74
|
|
|
$this->updateService->execute(); |
75
|
|
|
} else { |
76
|
|
|
$this->insertService->bindValue('key', $key); |
77
|
|
|
$this->insertService->bindValue('timeout', $timeout); |
78
|
|
|
$this->insertService->bindValue('try', '2000-01-01 00:00:00'); |
79
|
|
|
$this->insertService->bindValue('run', '2000-01-01 00:00:00'); |
80
|
|
|
$this->insertService->execute(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $key |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function markTry($key) { |
91
|
|
|
$this->updateTryDate->bindValue('key', $key); |
92
|
|
|
$this->updateTryDate->execute(); |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $key |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function markRun($key) { |
101
|
|
|
$this->updateRunDate->bindValue('key', $key); |
102
|
|
|
$this->updateRunDate->execute(); |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return Service[] |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function fetchServices() { |
|
|
|
|
110
|
|
|
$this->selectServices->execute(); |
111
|
|
|
$services = $this->selectServices->fetchAll(PDO::FETCH_ASSOC); |
112
|
|
|
$result = array(); |
113
|
|
|
foreach($services as $service) { |
114
|
|
|
$result[] = $service['service_key']; |
115
|
|
|
} |
116
|
|
|
$this->selectServices->closeCursor(); |
117
|
|
|
return $result; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.