Skip to content

Commit

Permalink
fix: reintroduce DoctrineTransactionalSession persa durante il refact…
Browse files Browse the repository at this point in the history
…oring dei moduli
  • Loading branch information
matiux committed Nov 8, 2021
1 parent ad83541 commit 2f858a1
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 33 deletions.
1 change: 0 additions & 1 deletion composer.json
Expand Up @@ -49,7 +49,6 @@
"psr-4": {
"Tests\\Unit\\DDDStarterPack\\": "tests/Unit/DDDStarterPack/",
"Tests\\Integration\\DDDStarterPack\\": "tests/Integration/DDDStarterPack/",
"Tests\\DDDStarterPack\\": "tests/DDDStarterPack/",
"Tests\\Learning\\": "tests/Learning/",
"Tests\\Support\\": "tests/Support/",
"Tests\\Tool\\": "tests/Tool/"
Expand Down
6 changes: 3 additions & 3 deletions docker/dc.sh
Expand Up @@ -39,9 +39,9 @@ elif [[ "$1" == "log" ]]; then ${DC_BASE_COMM

#elif [[ "$1" == "composer" ]]; then shift 1; ${DC_EXEC} composer "$@"
elif [[ "$1" == "create-badge" ]]; then shift 1; ${DC_EXEC} $PROJECT_TOOL create-badge "$@"
elif [[ "$1" == "php-cs-fixer-fix" ]]; then shift 1; ${DC_EXEC} $PROJECT_TOOL coding-standard-fix "$@"
elif [[ "$1" == "php-cs-fixer-check-staged" ]]; then shift 1; ${DC_EXEC} $PROJECT_TOOL coding-standard-check-staged
elif [[ "$1" == "php-cs-fixer-fix-staged" ]]; then shift 1; ${DC_EXEC} $PROJECT_TOOL coding-standard-fix-staged
elif [[ "$1" == "coding-standard-fix" ]]; then shift 1; ${DC_EXEC} $PROJECT_TOOL coding-standard-fix "$@"
elif [[ "$1" == "coding-standard-check-staged" ]]; then shift 1; ${DC_EXEC} $PROJECT_TOOL coding-standard-check-staged
elif [[ "$1" == "coding-standard-fix-staged" ]]; then shift 1; ${DC_EXEC} $PROJECT_TOOL coding-standard-fix-staged
elif [[ "$1" == "phpunit" ]]; then shift 1; ${DC_EXEC} $PROJECT_TOOL coverage
elif [[ "$1" == "psalm" ]]; then shift 1; ${DC_EXEC} $PROJECT_TOOL psalm "$@"
elif [[ "$1" == "deptrac-all" ]]; then shift 1; ${DC_EXEC} $PROJECT_TOOL deptrac-all
Expand Down
6 changes: 3 additions & 3 deletions scripts/git-hooks/pre-commit-functions.sh
Expand Up @@ -3,7 +3,7 @@
check_code_style() {

# Formattazione del codice con PHP CS Fixer
./dc php-cs-fixer-check-staged
./dc coding-standard-check-staged

STATUS=$?

Expand All @@ -23,11 +23,11 @@ check_code_style() {
return 0
;;
[Nn]*)
echo "Run './dc php-cs-fixer-fix-staged' to fix"
echo "Run './dc coding-standard-fix-staged' to fix"
return 1
;;
[Ff]*)
./dc php-cs-fixer-fix-staged
./dc coding-standard-fix-staged
return 1
;;
*) echo "Please answer y, n or f." ;;
Expand Down
4 changes: 3 additions & 1 deletion scripts/project/bin/project
Expand Up @@ -53,11 +53,13 @@ psalm() {
}

coding_standard_fix() {

#--stop-on-violation \

./vendor/bin/php-cs-fixer \
fix \
--verbose \
--show-progress=dots \
--stop-on-violation \
--cache-file="$TOOLS_PATH"/php-cs-fixer/.php-cs-fixer.cache \
--config="$TOOLS_PATH"/php-cs-fixer/.php-cs-fixer.dist.php "$@"

Expand Down
3 changes: 3 additions & 0 deletions scripts/tools-for-external/coding-standard-fix
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

./dc coding-standard-fix "$@"
3 changes: 0 additions & 3 deletions scripts/tools-for-external/php-cs-fixer-fix

This file was deleted.

Expand Up @@ -6,16 +6,31 @@

use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\ORM\EntityManagerInterface;
use InvalidArgumentException;
use Webmozart\Assert\Assert;

abstract class DoctrineRepository
{
protected Registry $registry;
protected EntityManagerInterface $em;

/**
* @var class-string
*/
private string $entityClassName;

/**
* @psalm-assert class-string $model
*
* @param Registry $registry
* @param class-string $model
*/
public function __construct(Registry $registry, string $model)
{
if (!class_exists($model)) {
throw new InvalidArgumentException(sprintf('`%s` is not a valid class-string', (string) $model));
}

$this->registry = $registry;

$em = $registry->getManagerForClass($model);
Expand All @@ -27,6 +42,9 @@ public function __construct(Registry $registry, string $model)
$this->entityClassName = $model;
}

/**
* @return class-string
*/
public function getEntityClassName(): string
{
return $this->entityClassName;
Expand Down
Expand Up @@ -6,6 +6,6 @@

use DDDStarterPack\Exception\Domain\DomainException;

abstract class ApplicationException extends DomainException
class ApplicationException extends DomainException
{
}
Expand Up @@ -52,7 +52,7 @@ protected function purgeSqsQueue(null|string $queueUrl = null): void
/** @var list<array{MessageId: string, ReceiptHandle: string, MD5OfBody: string, Body: string }> $messages */
$messages = $this->pullFromSqsQueue(10)->get('Messages');

if (empty($message)) {
if (empty($messages)) {
break;
}

Expand Down
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace DDDStarterPack\Service\Infrastructure\Doctrine;

use DDDStarterPack\Exception\Application\TransactionFailedException;
use DDDStarterPack\Service\Application\TransactionalSession;
use Doctrine\ORM\EntityManager;
use Throwable;

/**
* @template O
*
* @implements TransactionalSession<O>
*/
class DoctrineTransactionalSession implements TransactionalSession
{
public function __construct(private EntityManager $entityManager)
{
}

/**
* @param callable $operation
*
* @throws TransactionFailedException
*
* @return O
*/
public function executeAtomically(callable $operation)
{
try {
/** @var O $reponse */
$reponse = $this->entityManager->wrapInTransaction($operation);

return $reponse;
} catch (Throwable $t) {
throw new TransactionFailedException(sprintf('Transaction failed: %s', $t->getMessage()), (int) $t->getCode(), $t);
}
}
}
Expand Up @@ -30,8 +30,6 @@ class SQSMessageConsumerTest extends TestCase

public function setUp(): void
{
parent::setUp();

$this->setQueueUrl(EnvVarUtil::get('AWS_SQS_QUEUE_NAME'));
$this->purgeSqsQueue();

Expand Down
Expand Up @@ -28,6 +28,7 @@ class SQSMessageProducerTest extends TestCase
use SnsRawClient;

private DateTimeImmutable $occurredAt;
private Configuration $SQSConfiguration;
private MessageConsumer $messageConsumer;

protected function setUp(): void
Expand All @@ -37,14 +38,12 @@ protected function setUp(): void

$this->occurredAt = new DateTimeImmutable();

$SQSconfiguration = SQSConfigurationBuilder::create()
->withRegion('eu-west-1')
$this->SQSConfiguration = SQSConfigurationBuilder::create()
->withRegion(EnvVarUtil::get('AWS_DEFAULT_REGION'))
->withQueueUrl($this->getQueueUrl())
->withAccessKey(EnvVarUtil::get('AWS_ACCESS_KEY_ID'))
->withSecretKey(EnvVarUtil::get('AWS_SECRET_ACCESS_KEY'))
->build();

$this->messageConsumer = MessageConsumerFactory::create()->obtainConsumer($SQSconfiguration);
$this->messageConsumer = MessageConsumerFactory::create()->obtainConsumer($this->SQSConfiguration);
}

protected function tearDown(): void
Expand Down Expand Up @@ -97,15 +96,10 @@ public function it_should_throw_exception_if_required_params_are_invalid(): void
* @group sqs
* @group producer
*/
public function message_provider_can_send_message_in_queue(): void
public function message_producer_can_send_message_in_queue(): void
{
$configuration = SQSConfigurationBuilder::create()
->withRegion('eu-west-1')
->withQueueUrl($this->getQueueUrl())
->build();

$factory = MessageProducerFactory::create();
$messageProducer = $factory->obtainProducer($configuration);
$messageProducer = $factory->obtainProducer($this->SQSConfiguration);

$message = new AWSMessage(
body: json_encode([
Expand Down Expand Up @@ -151,12 +145,8 @@ public function message_provider_can_send_message_in_queue(): void
*/
public function it_should_send_message_on_topic_without_implied_queue_url(): void
{
$configuration = SQSConfigurationBuilder::create()
->withRegion('eu-west-1')
->build();

$factory = MessageProducerFactory::create();
$messageProducer = $factory->obtainProducer($configuration);
$messageProducer = $factory->obtainProducer($this->SQSConfiguration);

$message = new AWSMessage(
body: json_encode([
Expand All @@ -165,7 +155,6 @@ public function it_should_send_message_on_topic_without_implied_queue_url(): voi
]),
occurredAt: $this->occurredAt,
extra: [
'QueueUrl' => $this->getQueueUrl(),
'MessageGroupId' => Uuid::uuid4()->toString(),
'MessageDeduplicationId' => Uuid::uuid4()->toString(),
]
Expand Down
1 change: 1 addition & 0 deletions tools/php-cs-fixer/.php-cs-fixer.dist.php
Expand Up @@ -23,6 +23,7 @@
'phpdoc_to_comment' => false, // Needed to support @Desc annotation
'single_line_throw' => false,
'php_unit_method_casing' => ['case' => 'snake_case'],
'return_assignment' => false,
'blank_line_before_statement' => [
'statements' => [
'break',
Expand Down

0 comments on commit 2f858a1

Please sign in to comment.