Skip to content

Commit

Permalink
Merge branch 'release/2.1.0-beta'
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieread committed Apr 10, 2020
2 parents 1190bb6 + f975ef1 commit c422d85
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [2.1.0-beta] - 2020-04-10
## Added
- Added abstract operation for dealing with create & update events on models
- Added transactional operation for simplicity

## [2.0.1-beta] - 2020-03-05
### Fixed
- Fixed operation self class reference for the `Operation::start()` method.

Expand Down
29 changes: 29 additions & 0 deletions src/Operations/ModelOperation.php
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;
}
50 changes: 50 additions & 0 deletions src/Operations/TransactionalOperation.php
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);
}
}

0 comments on commit c422d85

Please sign in to comment.