1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scheduler\ActionInspector; |
4
|
|
|
|
5
|
|
|
use Scheduler\Action\ActionInterface; |
6
|
|
|
use Scheduler\Exception\SchedulerException; |
7
|
|
|
use Doctrine\DBAL\DBALException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class RdsActionInspector |
11
|
|
|
* @package Scheduler\ActionInspector |
12
|
|
|
* @author Aleh Hutnikau, <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class RdsActionInspector extends AbstractActionInspector |
15
|
|
|
{ |
16
|
|
|
const TABLE_NAME = 'scheduler_action_inspector'; |
17
|
|
|
const COLUMN_ID = 'id'; |
18
|
|
|
const COLUMN_REPORT = 'report'; |
19
|
|
|
const COLUMN_STATE = 'state'; |
20
|
|
|
|
21
|
|
|
/** @var \Doctrine\DBAL\Connection */ |
22
|
|
|
private $connection; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* RdsActionInspector constructor. |
26
|
|
|
* @param \Doctrine\DBAL\Connection $connection |
27
|
|
|
*/ |
28
|
1 |
|
public function __construct(\Doctrine\DBAL\Connection $connection) |
29
|
|
|
{ |
30
|
1 |
|
$this->connection = $connection; |
31
|
1 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ActionInterface $action |
35
|
|
|
* @return boolean returns `false` if action is already exists in this state in the log |
36
|
|
|
* @throws SchedulerException |
37
|
|
|
*/ |
38
|
1 |
|
public function update(ActionInterface $action) |
39
|
|
|
{ |
40
|
1 |
|
$actionId = $action->getId(); |
41
|
1 |
|
$actionState = $action->getState(); |
42
|
1 |
|
$result = false; |
43
|
1 |
|
$previousState = null; |
44
|
|
|
try { |
45
|
1 |
|
$selectQb = $this->getSelectQuery($actionId); |
46
|
|
|
|
47
|
1 |
|
if ($dbResult = $selectQb->execute()->fetch(\PDO::FETCH_ASSOC)) { |
48
|
1 |
|
$previousState = $dbResult[self::COLUMN_STATE]; |
49
|
1 |
|
$qb = $this->getUpdateQuery($actionState, $actionId); |
50
|
|
|
} else { |
51
|
1 |
|
$qb = $this->getInsertQuery($actionState, $actionId); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
if ($actionState === ActionInterface::STATE_FINISHED) { |
55
|
1 |
|
$qb->set(self::COLUMN_REPORT, ':report') |
56
|
1 |
|
->setParameter(self::COLUMN_REPORT, $action->getReport()); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
if ($this->isStateAllowed($action, $previousState)) { |
60
|
1 |
|
$result = $qb->execute() === 1; |
61
|
|
|
} |
62
|
1 |
|
} catch (DBALException $e) { |
63
|
1 |
|
$result = false; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return $result; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $actionId |
71
|
|
|
* @return \Doctrine\DBAL\Query\QueryBuilder |
72
|
|
|
*/ |
73
|
1 |
|
private function getSelectQuery($actionId) |
74
|
|
|
{ |
75
|
1 |
|
return $this->getConnection()->createQueryBuilder() |
76
|
1 |
|
->select(implode(',', [self::COLUMN_REPORT, self::COLUMN_STATE, self::COLUMN_ID])) |
77
|
1 |
|
->from(self::TABLE_NAME) |
78
|
1 |
|
->andWhere(self::COLUMN_ID . ' = ?') |
79
|
1 |
|
->setParameter(0, $actionId); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $actionState |
84
|
|
|
* @param $actionId |
85
|
|
|
* @return \Doctrine\DBAL\Query\QueryBuilder |
86
|
|
|
*/ |
87
|
1 |
View Code Duplication |
private function getUpdateQuery($actionState, $actionId) |
|
|
|
|
88
|
|
|
{ |
89
|
1 |
|
return $this->getConnection() |
90
|
1 |
|
->createQueryBuilder() |
91
|
1 |
|
->update(self::TABLE_NAME) |
92
|
1 |
|
->set(self::COLUMN_STATE, ':state') |
93
|
1 |
|
->where(self::COLUMN_ID . ' = :id') |
94
|
1 |
|
->setParameters(['state' => $actionState, 'id' => $actionId]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $actionState |
99
|
|
|
* @param $actionId |
100
|
|
|
* @return \Doctrine\DBAL\Query\QueryBuilder |
101
|
|
|
*/ |
102
|
1 |
View Code Duplication |
private function getInsertQuery($actionState, $actionId) |
|
|
|
|
103
|
|
|
{ |
104
|
1 |
|
return $this->getConnection() |
105
|
1 |
|
->createQueryBuilder() |
106
|
1 |
|
->insert(self::TABLE_NAME) |
107
|
1 |
|
->values([ |
108
|
1 |
|
self::COLUMN_ID => ':id', |
109
|
1 |
|
self::COLUMN_STATE => ':state' |
110
|
|
|
]) |
111
|
1 |
|
->setParameters(['id' => $actionId, 'state' => $actionState]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return \Doctrine\DBAL\Connection |
116
|
|
|
*/ |
117
|
1 |
|
private function getConnection() |
118
|
|
|
{ |
119
|
1 |
|
return $this->connection; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param \Doctrine\DBAL\Connection $connection |
124
|
|
|
* @throws \Doctrine\DBAL\DBALException |
125
|
|
|
*/ |
126
|
3 |
|
public static function initDb(\Doctrine\DBAL\Connection $connection) |
127
|
|
|
{ |
128
|
3 |
|
$schemaManager = $connection->getSchemaManager(); |
129
|
3 |
|
$schema = $schemaManager->createSchema(); |
130
|
3 |
|
$fromSchema = clone $schema; |
131
|
3 |
|
$table = $schema->createTable(self::TABLE_NAME); |
132
|
2 |
|
$table->addColumn(static::COLUMN_ID, 'string', ['length' => 255, 'notnull' => true]); |
133
|
2 |
|
$table->addColumn(static::COLUMN_STATE, 'string', ['length' => 255, 'notnull' => true]); |
134
|
2 |
|
$table->addColumn(static::COLUMN_REPORT, 'text', ['notnull' => false]); |
135
|
2 |
|
$table->setPrimaryKey([static::COLUMN_ID]); |
136
|
2 |
|
$table->addIndex([static::COLUMN_ID], 'IDX_' . static::TABLE_NAME . '_' . static::COLUMN_ID); |
137
|
2 |
|
$queries = $fromSchema->getMigrateToSql($schema, $connection->getDatabasePlatform()); |
138
|
2 |
|
foreach ($queries as $query) { |
139
|
2 |
|
$connection->exec($query); |
140
|
|
|
} |
141
|
2 |
|
} |
142
|
|
|
|
143
|
2 |
|
public static function dropDb(\Doctrine\DBAL\Connection $connection) |
144
|
|
|
{ |
145
|
2 |
|
$schemaManager = $connection->getSchemaManager(); |
146
|
2 |
|
$schema = $schemaManager->createSchema(); |
147
|
2 |
|
$fromSchema = clone $schema; |
148
|
2 |
|
$schema->dropTable(self::TABLE_NAME); |
149
|
2 |
|
$queries = $fromSchema->getMigrateToSql($schema, $connection->getDatabasePlatform()); |
150
|
2 |
|
foreach ($queries as $query) { |
151
|
2 |
|
$connection->exec($query); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
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.