Skip to content

Commit

Permalink
mess-detector refactor applied
Browse files Browse the repository at this point in the history
  • Loading branch information
abuenosvinos committed Dec 21, 2023
1 parent 9e521fb commit f235eb0
Show file tree
Hide file tree
Showing 17 changed files with 338 additions and 116 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ test-architecture:
# docker exec -w /var/www/html/ -t --user $(id -u):$(id -g) $(PHP_CONTAINER) php -d memory_limit=4G ./vendor/bin/phpstan analyse --error-format=github
docker exec -w /var/www/html/ -t --user $(id -u):$(id -g) $(PHP_CONTAINER) php -d memory_limit=4G ./vendor/bin/phpstan analyse

mess-detector:
# docker exec -w /var/www/html/ -t --user $(id -u):$(id -g) $(PHP_CONTAINER) ./vendor/bin/phpmd src,tests github phpmd.xml
docker exec -w /var/www/html/ -t --user $(id -u):$(id -g) $(PHP_CONTAINER) ./vendor/bin/phpmd src,tests text phpmd.xml

clean-cache: ## Clear the cache
@rm -rf apps/*/*/var
@docker exec -w /var/www/html/ -t --user $(id -u):$(id -g) $(PHP_CONTAINER) ./bin/console cache:warmup
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"ext-xdebug": "*",
"behat/behat": "^3.13",
"phpat/phpat": "^0.10.11",
"phpmd/phpmd": "^2.15",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.5",
"psalm/plugin-mockery": "^1.1",
Expand Down
148 changes: 147 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions phpmd.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0"?>
<ruleset xmlns="https://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://pmd.sf.net/ruleset_xml_schema.xsd">

<!-- <exclude-pattern>apps/*/*/var/*</exclude-pattern>-->
<!-- <exclude-pattern>*SimilarComparator*</exclude-pattern>-->
<!-- <exclude-pattern>*IsSimilar*</exclude-pattern>-->
<!-- Fix CyclomaticComplexity -->
<!-- <exclude-pattern>src/Shared/Infrastructure/Symfony/AddJsonBodyToRequestListener.php</exclude-pattern>-->
<!-- Fix ExcessiveClassLength -->
<!-- <exclude-pattern>tests/Shared/Infrastructure/Bus/Event/RabbitMq/RabbitMqEventBusTest.php</exclude-pattern>-->
<!-- Fix TooManyMethods -->
<!-- <exclude-pattern>tests/Shared/Infrastructure/PhpUnit/UnitTestCase.php</exclude-pattern>-->

<rule ref="rulesets/cleancode.xml/BooleanArgumentFlag"/>

<rule ref="rulesets/codesize.xml/CyclomaticComplexity">
<properties>
<property name="reportLevel" value="5"/>
</properties>
</rule>
<rule ref="rulesets/codesize.xml/ExcessiveMethodLength">
<properties>
<property name="minimum" value="35"/>
</properties>
</rule>
<rule ref="rulesets/codesize.xml/ExcessiveClassLength">
<properties>
<property name="minimum" value="150"/>
</properties>
</rule>
<rule ref="rulesets/codesize.xml/ExcessiveParameterList">
<properties>
<property name="minimum" value="10"/>
</properties>
</rule>
<rule ref="rulesets/codesize.xml/TooManyMethods">
<properties>
<property name="maxmethods" value="12"/>
</properties>
</rule>
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity">
<properties>
<property name="maximum" value="20"/>
</properties>
</rule>
</ruleset>
17 changes: 13 additions & 4 deletions src/Data/Application/LogBook/LogBookEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,30 @@ public function __invoke(LogBookEvent $event): void
$logBook->setOccurredOn(new \DateTime($event->occurredOn()));
$logBook->setObjectType($event->objectType());
$logBook->setObjectId($event->objectId());
$this->setMetadata($logBook, $event);
$this->setUser($logBook, $event);

$this->logBookRepository->save(
$logBook
);
}

private function setMetadata(LogBook $logBook, LogBookEvent $event): void
{
$data = $event->data();
if (count($data) > 0) {
$logBook->setMetadata(json_encode($data));
}
}

public function setUser(LogBook $logBook, LogBookEvent $event): void
{
$email = $event->email();
if ($email !== null) {
$user = $this->userRepository->findByEmail(EmailAddress::create($email));
if ($user) {
$logBook->setUser($user);
}
}

$this->logBookRepository->save(
$logBook
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Data\Infrastructure\UI\Controller\LogBook;

use App\Data\Application\ListLogBook\ListLogBookQuery;
use App\Data\Domain\Event\LogBook;
use App\Event\Domain\Bus\Query\QueryBus;
use App\Shared\Application\Paginator;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -16,7 +17,7 @@ public function __construct(private readonly QueryBus $queryBus)

public function index(Environment $twig): Response
{
/** @var Paginator $results */
/** @var Paginator<LogBook> $results */
$results = $this->queryBus->ask(new ListLogBookQuery());

return new Response(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,49 @@ public function getConfiguration(Event $event, ?EventOptions $eventOptions): arr
*/


$stamps = $this->getRoutingOptions($eventOptions, $stamps);
$stamps = $this->getTransportOptions($eventOptions, $stamps);
$stamps = $this->getDelayOptions($eventOptions, $stamps);

return $stamps;
}

/**
* @param EventOptions $eventOptions
* @param array<StampInterface> $stamps
* @return array<StampInterface>
*/
public function getRoutingOptions(EventOptions $eventOptions, array $stamps): array
{
if (is_string($eventOptions->get('routing'))) {
$stamps[] = new AmqpStamp($eventOptions->get('routing'));
}
return $stamps;
}

/**
* @param EventOptions $eventOptions
* @param array<StampInterface> $stamps
* @return array<StampInterface>
*/
public function getTransportOptions(EventOptions $eventOptions, array $stamps): array
{
if (is_array($eventOptions->get('transport'))) {
$stamps[] = new TransportNamesStamp($eventOptions->get('transport'));
}
return $stamps;
}

/**
* @param EventOptions $eventOptions
* @param array<StampInterface> $stamps
* @return array<StampInterface>
*/
public function getDelayOptions(EventOptions $eventOptions, array $stamps): array
{
if (is_int($eventOptions->get('delay'))) {
$stamps[] = new DelayStamp($eventOptions->get('delay'));
}

return $stamps;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ public function __invoke(ProductBought $event): void
{
$product = $this->productRepository->findByCode($event->code());

$criteria = Criteria::fromFilters(
[
Filter::fromValues([
'field' => 'product',
'operator' => '=',
'value' => $product
]),
Filter::fromValues([
'field' => 'status.value',
'operator' => '=',
'value' => StatusRequest::PENDING
])
]
);
if (!$product) {
throw new \DomainException("No product found");
}

$criteria = Criteria::fromFilters([
Filter::fromValues([
'field' => 'product',
'operator' => '=',
'value' => $product
]),
Filter::fromValues([
'field' => 'status.value',
'operator' => '=',
'value' => StatusRequest::PENDING
])
]);

$requests = $this->requestRepository->search($criteria);
/** @var Request $request */
Expand Down
Loading

0 comments on commit f235eb0

Please sign in to comment.