-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Sprocketbox\Toolkit\Operations; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
/** | ||
* Class ModelOperation | ||
* | ||
* @package Sprocketbox\Toolkit\Operations | ||
*/ | ||
abstract class ModelOperation extends Operation | ||
{ | ||
/** | ||
* @return \Illuminate\Database\Eloquent\Model | ||
*/ | ||
abstract protected function getModel(): Model; | ||
|
||
/** | ||
* @return array | ||
*/ | ||
abstract protected function getAttributes(): array; | ||
|
||
/** | ||
* @param array $attributes | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
*/ | ||
abstract protected function validate(array $attributes, Model $model): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Sprocketbox\Toolkit\Operations; | ||
|
||
use Closure; | ||
use Illuminate\Database\DatabaseManager; | ||
use InvalidArgumentException; | ||
|
||
/** | ||
* Class TransactionalOperation | ||
* | ||
* @method self callback(Closure $transaction) | ||
* @method self connection(string $connection) | ||
* @method self attempts(int $attempts) | ||
* | ||
* @package Sprocketbox\Toolkit\Operations | ||
*/ | ||
final class TransactionalOperation extends Operation | ||
{ | ||
protected ?Closure $callback = null; | ||
|
||
protected ?string $connection = null; | ||
|
||
protected int $attempts = 1; | ||
|
||
/** | ||
* @var \Illuminate\Database\DatabaseManager | ||
*/ | ||
protected DatabaseManager $databaseManager; | ||
|
||
public function __construct(DatabaseManager $databaseManager) | ||
{ | ||
$this->databaseManager = $databaseManager; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
* @throws \Throwable | ||
*/ | ||
public function perform() | ||
{ | ||
if ($this->callback === null) { | ||
throw new InvalidArgumentException('No transaction body provided'); | ||
} | ||
|
||
return $this->databaseManager | ||
->connection($this->connection) | ||
->transaction($this->callback, $this->attempts); | ||
} | ||
} |