| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\MigrationLog; |
| 4 |
|
| 5 |
use Give\Framework\Database\Exceptions\DatabaseQueryException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class MigrationLogModel |
| 9 |
* @package Give\MigrationLog |
| 10 |
* |
| 11 |
* @since 2.10.0 |
| 12 |
*/ |
| 13 |
class MigrationLogModel { |
| 14 |
/** |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
private $id; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
private $status; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var string|null |
| 26 |
*/ |
| 27 |
private $last_run; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var mixed|null |
| 31 |
*/ |
| 32 |
private $error; |
| 33 |
|
| 34 |
/** |
| 35 |
* MigrationModel constructor. |
| 36 |
* |
| 37 |
* @param string $id |
| 38 |
* @param string $status |
| 39 |
* @param mixed|null $error |
| 40 |
* @param string|null $lastRun |
| 41 |
*/ |
| 42 |
public function __construct( $id, $status = '', $error = null, $lastRun = null ) { |
| 43 |
$this->id = $id; |
| 44 |
$this->last_run = $lastRun; |
| 45 |
$this->setError( $error ); |
| 46 |
$this->setStatus( $status ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Set migration status |
| 51 |
* |
| 52 |
* @see MigrationLogStatus::STATUS_NAME |
| 53 |
* |
| 54 |
* @param string $status |
| 55 |
* |
| 56 |
* @return MigrationLogModel |
| 57 |
* @uses MigrationLogStatus |
| 58 |
*/ |
| 59 |
public function setStatus( $status ) { |
| 60 |
$this->status = array_key_exists( $status, MigrationLogStatus::getAll() ) |
| 61 |
? $status |
| 62 |
: MigrationLogStatus::getDefault(); |
| 63 |
|
| 64 |
return $this; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Add migration error notice |
| 69 |
* |
| 70 |
* @param mixed $error |
| 71 |
* |
| 72 |
* @return MigrationLogModel |
| 73 |
*/ |
| 74 |
public function setError( $error ) { |
| 75 |
if ( is_array( $error ) || is_object( $error ) ) { |
| 76 |
$error = print_r( $error, true ); |
| 77 |
} |
| 78 |
|
| 79 |
$this->error = $error; |
| 80 |
|
| 81 |
return $this; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @return int |
| 86 |
*/ |
| 87 |
public function getId() { |
| 88 |
return $this->id; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @return string |
| 93 |
*/ |
| 94 |
public function getStatus() { |
| 95 |
return $this->status; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @return string |
| 100 |
*/ |
| 101 |
public function getLastRunDate() { |
| 102 |
return $this->last_run; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @return string|null |
| 107 |
*/ |
| 108 |
public function getError() { |
| 109 |
return $this->error; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Save migration |
| 114 |
*/ |
| 115 |
public function save() { |
| 116 |
give( MigrationLogRepository::class )->save( $this ); |
| 117 |
} |
| 118 |
} |
| 119 |
|