| Total Complexity | 7 |
| Total Lines | 58 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 12 | abstract class Migration |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var AdapterInterface |
||
| 16 | */ |
||
| 17 | private $dbInstance; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Migration constructor. |
||
| 21 | */ |
||
| 22 | public function __construct() |
||
| 23 | { |
||
| 24 | $this->dbInstance = Di::getDefault()->getShared('db'); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param string $action |
||
| 29 | * @return bool |
||
| 30 | */ |
||
| 31 | public function safeRun(string $action): bool |
||
| 32 | { |
||
| 33 | $this->dbInstance->begin(); |
||
| 34 | try { |
||
| 35 | switch ($action) { |
||
| 36 | default: |
||
| 37 | case 'up': |
||
| 38 | $result = $this->up(); |
||
| 39 | break; |
||
| 40 | case 'down': |
||
| 41 | $result = $this->down(); |
||
| 42 | break; |
||
| 43 | } |
||
| 44 | $result ? $this->dbInstance->commit() : $this->dbInstance->rollback(); |
||
| 45 | return $result; |
||
| 46 | } catch (\Exception $e) { |
||
| 47 | echo $e->getMessage() . PHP_EOL; |
||
| 48 | $this->dbInstance->rollback(); |
||
| 49 | return false; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return AdapterInterface |
||
| 55 | */ |
||
| 56 | public function getDbConnection(): AdapterInterface |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return bool |
||
| 63 | */ |
||
| 64 | abstract public function up(): bool; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return bool |
||
| 68 | */ |
||
| 69 | abstract public function down(): bool; |
||
| 70 | } |