1 | <?php |
||
15 | class RdsActionInspector extends AbstractActionInspector |
||
16 | { |
||
17 | const TABLE_NAME = 'scheduler_action_inspector'; |
||
18 | const COLUMN_ID = 'id'; |
||
19 | const COLUMN_REPORT = 'report'; |
||
20 | const COLUMN_STATE = 'state'; |
||
21 | const COLUMN_CREATED_AT = 'created_at'; |
||
22 | |||
23 | /** @var \Doctrine\DBAL\Connection */ |
||
24 | private $connection; |
||
25 | |||
26 | /** |
||
27 | * RdsActionInspector constructor. |
||
28 | * @param \Doctrine\DBAL\Connection $connection |
||
29 | */ |
||
30 | 1 | public function __construct(\Doctrine\DBAL\Connection $connection) |
|
34 | |||
35 | /** |
||
36 | * @param ActionInterface $action |
||
37 | * @return boolean returns `false` if action is already exists in this state in the log |
||
38 | * @throws SchedulerException |
||
39 | */ |
||
40 | 1 | public function update(ActionInterface $action) |
|
41 | { |
||
42 | 1 | $actionId = $action->getId(); |
|
43 | 1 | $actionState = $action->getState(); |
|
44 | 1 | $result = false; |
|
45 | 1 | $previousState = null; |
|
46 | try { |
||
47 | 1 | $selectQb = $this->getSelectQuery($actionId); |
|
48 | |||
49 | 1 | if ($dbResult = $selectQb->execute()->fetch(\PDO::FETCH_ASSOC)) { |
|
50 | 1 | $previousState = $dbResult[self::COLUMN_STATE]; |
|
51 | 1 | $qb = $this->getUpdateQuery($actionState, $actionId); |
|
52 | 1 | } else { |
|
53 | 1 | $qb = $this->getInsertQuery($actionState, $actionId); |
|
54 | } |
||
55 | |||
56 | 1 | if ($actionState === ActionInterface::STATE_FINISHED) { |
|
57 | 1 | $qb->set(self::COLUMN_REPORT, ':report') |
|
58 | 1 | ->setParameter(self::COLUMN_REPORT, $action->getReport()); |
|
59 | 1 | } |
|
60 | |||
61 | 1 | if ($this->isStateAllowed($action, $previousState)) { |
|
62 | 1 | $result = $qb->execute() === 1; |
|
63 | 1 | } |
|
64 | 1 | } catch (DBALException $e) { |
|
65 | 1 | $result = false; |
|
66 | } |
||
67 | |||
68 | 1 | return $result; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * @return \Doctrine\DBAL\Query\QueryBuilder |
||
73 | */ |
||
74 | 1 | protected function getQueryBuilder() |
|
78 | |||
79 | /** |
||
80 | * @param $actionId |
||
81 | * @return \Doctrine\DBAL\Query\QueryBuilder |
||
82 | */ |
||
83 | 1 | protected function getSelectQuery($actionId) |
|
91 | |||
92 | /** |
||
93 | * @param $actionState |
||
94 | * @param $actionId |
||
95 | * @return \Doctrine\DBAL\Query\QueryBuilder |
||
96 | */ |
||
97 | 1 | protected function getUpdateQuery($actionState, $actionId) |
|
98 | { |
||
99 | 1 | return $this->getQueryBuilder() |
|
100 | 1 | ->update(self::TABLE_NAME) |
|
101 | 1 | ->set(self::COLUMN_STATE, ':state') |
|
102 | 1 | ->set(self::COLUMN_CREATED_AT, ':created_at') |
|
103 | 1 | ->where(self::COLUMN_ID . ' = :id') |
|
104 | 1 | ->setParameters([ |
|
105 | 1 | self::COLUMN_STATE => $actionState, |
|
106 | 1 | self::COLUMN_ID => $actionId, |
|
107 | 1 | self::COLUMN_CREATED_AT => (new \DateTime)->format('Y-m-d H:i:s') |
|
108 | 1 | ]); |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param $actionState |
||
113 | * @param $actionId |
||
114 | * @return \Doctrine\DBAL\Query\QueryBuilder |
||
115 | */ |
||
116 | 1 | protected function getInsertQuery($actionState, $actionId) |
|
117 | { |
||
118 | 1 | return $this->getQueryBuilder() |
|
119 | 1 | ->insert(self::TABLE_NAME) |
|
120 | 1 | ->values([ |
|
121 | 1 | self::COLUMN_ID => ':id', |
|
122 | 1 | self::COLUMN_STATE => ':state', |
|
123 | 1 | self::COLUMN_CREATED_AT => ':created_at', |
|
124 | 1 | ]) |
|
125 | 1 | ->setParameters([ |
|
126 | 1 | self::COLUMN_ID => $actionId, |
|
127 | 1 | self::COLUMN_STATE => $actionState, |
|
128 | 1 | self::COLUMN_CREATED_AT => (new \DateTime)->format('Y-m-d H:i:s'), |
|
129 | 1 | ]); |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * @return \Doctrine\DBAL\Connection |
||
134 | */ |
||
135 | 1 | private function getConnection() |
|
139 | |||
140 | /** |
||
141 | * @param \Doctrine\DBAL\Connection $connection |
||
142 | * @throws \Doctrine\DBAL\DBALException |
||
143 | */ |
||
144 | 3 | public static function initDb(\Doctrine\DBAL\Connection $connection) |
|
161 | |||
162 | 3 | public static function dropDb(\Doctrine\DBAL\Connection $connection) |
|
173 | } |