Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
othillo committed Apr 28, 2020
1 parent 3792218 commit 56facca
Show file tree
Hide file tree
Showing 13 changed files with 484 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .docheader
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* This file is part of the broadway/broadway-saga package.
*
* (c) 2020 Broadway project
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor/
composer.lock
.php_cs.cache
.phpunit.result.cache
13 changes: 13 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$config = require 'vendor/broadway/coding-standard/.php_cs.dist';

$config->setFinder(
\PhpCsFixer\Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/test',
])
);

return $config;
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: php

php:
- 7.2
- 7.3
- 7.4

before_install:
# Disable XDebug speed up test execution.
- phpenv config-rm xdebug.ini || return 0

install:
- make dependencies

script:
- make test
- make qa
- make license
- make changelog
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2014 Qandidate.com - http://qandidate.com/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
36 changes: 36 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.DEFAULT_GOAL:=help

.PHONY: dependencies
dependencies:
composer install --no-interaction --no-suggest --no-scripts --ansi

.PHONY: test
test:
vendor/bin/phpunit --testdox --exclude-group=none --colors=always

.PHONY: qa
qa: php-cs-fixer-ci phpstan

.PHONY: php-cs-fixer
php-cs-fixer:
vendor/bin/php-cs-fixer fix --no-interaction --allow-risky=yes --diff --verbose

.PHONY: php-cs-fixer-ci
php-cs-fixer-ci:
vendor/bin/php-cs-fixer fix --dry-run --no-interaction --allow-risky=yes --diff --verbose

PHONY: phpstan
phpstan:
vendor/bin/phpstan analyse --level=max src/

.PHONY: changelog
changelog:
git log $$(git describe --abbrev=0 --tags)...HEAD --no-merges --pretty=format:"* [%h](http://github.com/${TRAVIS_REPO_SLUG}/commit/%H) %s (%cN)"

.PHONY: license
license:
vendor/bin/docheader check --no-interaction --ansi -vvv {src,test,examples}

# Based on https://suva.sh/posts/well-documented-makefiles/
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n\nTargets:\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST)
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
# saga-state-dbal
Saga state persistence for broadway/broadway-saga using doctrine/dbal
# broadway/saga-state-dbal

Saga state persistence for [broadway/broadway-saga](https://github.com/broadway/broadway-saga) using [doctrine/dbal](https://github.com/doctrine/dbal).

## Installation

```
$ composer require broadway/saga-state-dbal
```

## Testing

To run the tests:

```
./vendor/bin/phpunit
```
38 changes: 38 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "broadway/saga-state-dbal",
"description": "Saga state persistence for broadway/broadway-saga using doctrine/dbal",
"license": "MIT",
"require": {
"php": ">=7.2",
"broadway/broadway-saga": "^0.10.1",
"doctrine/dbal": "^2.6",
"beberlei/assert": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0",
"ramsey/uuid": "^4.0",
"broadway/coding-standard": "^1.0",
"phpstan/phpstan": "@stable"
},
"authors": [
{
"name": "othillo",
"email": "othillo@othillo.nl"
}
],
"autoload": {
"psr-4": {
"Broadway\\Saga\\State\\Dbal\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Broadway\\Saga\\State\\Dbal\\": "test/"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
}
}
7 changes: 7 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
ignoreErrors:
-
message: "#^Method Broadway\\\\Saga\\\\State\\\\Dbal\\\\DbalRepository\\:\\:createAndExecuteQuery\\(\\) should return Doctrine\\\\DBAL\\\\Driver\\\\ResultStatement but returns Doctrine\\\\DBAL\\\\Driver\\\\ResultStatement\\|int\\.$#"
count: 1
path: src/DbalRepository.php

5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
includes:
- phpstan-baseline.neon

parameters:
checkMissingIterableValueType: false
23 changes: 23 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Broadway Saga Test Suite">
<directory>./test/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</phpunit>
114 changes: 114 additions & 0 deletions src/DbalRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

/*
* This file is part of the broadway/broadway-saga package.
*
* (c) 2020 Broadway project
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Broadway\Saga\State\Dbal;

use Broadway\Saga\State;
use Broadway\Saga\State\Criteria;
use Broadway\Saga\State\RepositoryException;
use Broadway\Saga\State\RepositoryInterface;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\DBAL\Query\QueryBuilder;

class DbalRepository implements RepositoryInterface
{
/** @var Connection */
private $connection;

/** @var string */
private $tableName;

public function __construct(Connection $connection, string $tableName)
{
$this->connection = $connection;
$this->tableName = $tableName;
}

/**
* {@inheritdoc}
*/
public function findOneBy(Criteria $criteria, string $sagaId): ?State
{
$results = $this->createAndExecuteQuery($criteria, $sagaId)
->fetchAll();

if (1 === count($results)) {
$result = current($results);

$values = $result;
unset($values['id']);
unset($values['done']);
unset($values['sagaId']);

return State::deserialize([
'id' => $result['id'],
'done' => (bool) $result['done'],
'values' => $values,
]);
}

if (count($results) > 1) {
throw new RepositoryException('Multiple saga state instances found.');
}

return null;
}

/**
* {@inheritdoc}
*/
public function save(State $state, string $sagaId): void
{
$this->connection->beginTransaction();

try {
$this->connection->insert($this->tableName, array_merge([
'id' => $state->getId(),
'done' => $state->isDone(),
'sagaId' => $sagaId,
], $state->getValues()));

$this->connection->commit();
} catch (UniqueConstraintViolationException $e) {
$this->connection->update($this->tableName, array_merge([
'done' => $state->isDone(),
'sagaId' => $sagaId,
], $state->getValues()), [
'id' => $state->getId(),
]);
} catch (\Throwable $e) {
$this->connection->rollBack();

throw $e;
}
}

protected function createAndExecuteQuery(Criteria $criteria, string $sagaId): ResultStatement
{
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->connection->createQueryBuilder()
->select('*')
->from($this->tableName)
->where('done = :done')
->setParameter(':done', false);

foreach ($criteria->getComparisons() as $key => $value) {
$queryBuilder->andWhere(sprintf('%s = :%s', $key, $key));
$queryBuilder->setParameter(sprintf(':%s', $key), $value);
}

return $queryBuilder->execute();
}
}
Loading

0 comments on commit 56facca

Please sign in to comment.