Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove parents for AggregateRootInterface #47

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ jobs:
- name: 'Upload to Codecov'
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
fail_ci_if_error: true
flags: unittests
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/StfalconApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace StfalconStudio\ApiBundle\DependencyInjection;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ORM\EntityManagerInterface;
use StfalconStudio\ApiBundle\Security\JwtBlackListService;
use StfalconStudio\ApiBundle\Service\Exception\ResponseProcessor\CustomAppExceptionResponseProcessorInterface;
Expand Down Expand Up @@ -49,6 +50,9 @@ public function load(array $configs, ContainerBuilder $container): void
if (\interface_exists(EntityManagerInterface::class)) {
$loader->load('orm.php');
}
if (\interface_exists(DocumentManager::class)) {
$loader->load('odm.php');
}

$container->setParameter('stfalcon_api.api_host', $config['api_host']);
$container->setParameter('stfalcon_api.json_schema_dir', $config['json_schema_dir']);
Expand Down
3 changes: 3 additions & 0 deletions Error/BaseErrorNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class BaseErrorNames
// 405
public final const METHOD_NOT_ALLOWED = 'method_not_allowed';

// 406
public final const NOT_ACCEPTABLE = 'not_acceptable';

// 409
public final const CONFLICT_TARGET_RESOURCE_UPDATE = 'conflict_target_resource_update';

Expand Down
19 changes: 6 additions & 13 deletions EventListener/JWT/JwtSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,12 @@ public static function getSubscribedEvents(): iterable
*/
public function onAuthenticationFailureResponse(AuthenticationFailureEvent $event): void
{
switch (true) {
case $event instanceof JWTInvalidEvent:
$message = 'invalid_jwt_token_message';
break;
case $event instanceof JWTNotFoundEvent:
$message = 'not_found_jwt_token_message';
break;
case $event instanceof JWTExpiredEvent:
$message = 'expired_jwt_token_message';
break;
default:
$message = 'unauthorised_user_message';
}
$message = match (true) {
$event instanceof JWTInvalidEvent => 'invalid_jwt_token_message',
$event instanceof JWTNotFoundEvent => 'not_found_jwt_token_message',
$event instanceof JWTExpiredEvent => 'expired_jwt_token_message',
default => 'unauthorised_user_message',
};

$data = [
'error' => BaseErrorNames::UNAUTHORISED_USER,
Expand Down
9 changes: 9 additions & 0 deletions EventListener/Kernel/ApiExceptionFormatterListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

Expand Down Expand Up @@ -95,6 +96,11 @@ public function __invoke(ExceptionEvent $event): void
$statusCode = Response::HTTP_METHOD_NOT_ALLOWED;
$errorName = BaseErrorNames::METHOD_NOT_ALLOWED;
break;
case $e instanceof NotAcceptableHttpException:
$message = 'not_acceptable_exception_message';
$statusCode = Response::HTTP_NOT_ACCEPTABLE;
$errorName = BaseErrorNames::NOT_ACCEPTABLE;
break;
case $e instanceof NotFoundHttpException:
$message = $e->getMessage();
$statusCode = $e->getStatusCode();
Expand Down Expand Up @@ -127,6 +133,9 @@ public function __invoke(ExceptionEvent $event): void
case Response::HTTP_TOO_MANY_REQUESTS:
$errorName = BaseErrorNames::HTTP_TOO_MANY_REQUESTS;
break;
case Response::HTTP_NOT_ACCEPTABLE:
$errorName = BaseErrorNames::NOT_ACCEPTABLE;
break;
case Response::HTTP_INTERNAL_SERVER_ERROR:
$errorName = BaseErrorNames::INTERNAL_SERVER_ERROR;

Expand Down
75 changes: 75 additions & 0 deletions EventListener/ODM/Aggregate/AggregatePartListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\EventListener\ODM\Aggregate;

use Doctrine\ODM\MongoDB\Event\OnFlushEventArgs;
use Fresh\DateTime\DateTimeHelper;
use StfalconStudio\ApiBundle\Model\ODM\Aggregate\AggregatePartInterface;
use StfalconStudio\ApiBundle\Model\ODM\Aggregate\AggregateRootInterface;

/**
* AggregatePartListener.
*/
final class AggregatePartListener
{
/** @var array<AggregateRootInterface> */
private array $aggregateRoots = [];

/**
* @param DateTimeHelper $dateTimeHelper
*/
public function __construct(private readonly DateTimeHelper $dateTimeHelper)
{
}

/**
* @param OnFlushEventArgs $eventArgs
*/
public function onFlush(OnFlushEventArgs $eventArgs): void
{
$em = $eventArgs->getDocumentManager();
$uow = $em->getUnitOfWork();

foreach ($uow->getScheduledDocumentUpdates() as $entity) {
$this->processEntity($entity);
}
foreach ($uow->getScheduledDocumentInsertions() as $entity) {
$this->processEntity($entity);
}
foreach ($uow->getScheduledDocumentDeletions() as $entity) {
$this->processEntity($entity);
}

foreach ($this->aggregateRoots as $aggregateRoot) {
$aggregateRoot->setUpdatedAt($this->dateTimeHelper->getCurrentDatetime());
$uow->recomputeSingleDocumentChangeSet($em->getClassMetadata(\get_class($aggregateRoot)), $aggregateRoot);
}

$this->aggregateRoots = [];
}

/**
* @param object $entity
*/
private function processEntity(object $entity): void
{
if ($entity instanceof AggregatePartInterface) {
$aggregateRoot = $entity->getAggregateRoot();

if (!\in_array($aggregateRoot, $this->aggregateRoots, true)) {
$this->aggregateRoots[$aggregateRoot->getId()] = $aggregateRoot;
$this->processEntity($aggregateRoot); // Bubble aggregate root to the top root
}
}
}
}
4 changes: 2 additions & 2 deletions EventListener/ORM/Aggregate/AggregatePartListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

use Doctrine\ORM\Event\OnFlushEventArgs;
use Fresh\DateTime\DateTimeHelper;
use StfalconStudio\ApiBundle\Model\Aggregate\AggregatePartInterface;
use StfalconStudio\ApiBundle\Model\Aggregate\AggregateRootInterface;
use StfalconStudio\ApiBundle\Model\ORM\Aggregate\AggregatePartInterface;
use StfalconStudio\ApiBundle\Model\ORM\Aggregate\AggregateRootInterface;

/**
* AggregatePartListener.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Model\Aggregate;
namespace StfalconStudio\ApiBundle\Model\ODM\Aggregate;

/**
* AggregatePartInterface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Model\Aggregate;
namespace StfalconStudio\ApiBundle\Model\ODM\Aggregate;

use StfalconStudio\ApiBundle\Model\Timestampable\TimestampableInterface;
use StfalconStudio\ApiBundle\Model\UUID\UuidInterface;
use StfalconStudio\ApiBundle\Model\ODM\Timestampable\TimestampableInterface;
use StfalconStudio\ApiBundle\Model\ODM\UUID\UuidInterface;

/**
* AggregateRootInterface.
Expand Down
22 changes: 22 additions & 0 deletions Model/ODM/Credentials/CredentialsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Model\ODM\Credentials;

use StfalconStudio\ApiBundle\Model\Credentials\CredentialsInterface as BaseCredentialsInterface;

/**
* CredentialsInterface.
*/
interface CredentialsInterface extends BaseCredentialsInterface
{
}
44 changes: 44 additions & 0 deletions Model/ODM/Credentials/CredentialsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Model\ODM\Credentials;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
* CredentialsTrait.
*/
trait CredentialsTrait
{
#[MongoDB\Field(type: 'date', nullable: true)]
protected ?\DateTimeInterface $credentialsLastChangedAt = null;

/**
* @param \DateTimeInterface|null $credentialsLastChangedAt
*
* @return self
*/
public function setCredentialsLastChangedAt(?\DateTimeInterface $credentialsLastChangedAt): self
{
$this->credentialsLastChangedAt = $credentialsLastChangedAt;

return $this;
}

/**
* @return \DateTimeInterface|null
*/
public function getCredentialsLastChangedAt(): ?\DateTimeInterface
{
return $this->credentialsLastChangedAt;
}
}
44 changes: 44 additions & 0 deletions Model/ODM/Timestampable/TimestampableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Model\ODM\Timestampable;

/**
* TimestampableInterface.
*/
interface TimestampableInterface
{
/**
* @param \DateTimeImmutable|null $createdAt
*/
public function setCreatedAt(\DateTimeInterface $createdAt = null): void;

/**
* @return \DateTimeInterface|null
*/
public function getCreatedAt(): ?\DateTimeInterface;

/**
* @param \DateTimeInterface|null $createdAt
*/
public function setUpdatedAt(\DateTimeInterface $createdAt = null): void;

/**
* @return \DateTimeInterface|null
*/
public function getUpdatedAt(): ?\DateTimeInterface;

/**
* Init timestampable fields.
*/
public function initTimestampableFields(): void;
}
70 changes: 70 additions & 0 deletions Model/ODM/Timestampable/TimestampableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Model\ODM\Timestampable;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Gedmo\Mapping\Annotation as Gedmo;

/**
* TimestampableTrait.
*/
trait TimestampableTrait
{
#[MongoDB\Field(type: 'date_immutable')]
protected \DateTimeInterface|null $createdAt = null;

#[MongoDB\Field(type: 'date')]
#[Gedmo\Timestampable(on: 'update')]
protected \DateTimeInterface|null $updatedAt = null;

/**
* @param \DateTimeImmutable|null $createdAt
*/
public function setCreatedAt(\DateTimeInterface $createdAt = null): void
{
$this->createdAt = $createdAt;
}

/**
* @return \DateTimeImmutable
*/
public function getCreatedAt(): \DateTimeInterface
{
return $this->createdAt;
}

/**
* @param \DateTimeInterface|null $updatedAt
*/
public function setUpdatedAt(\DateTimeInterface $updatedAt = null): void
{
$this->updatedAt = $updatedAt;
}

/**
* @return \DateTimeInterface
*/
public function getUpdatedAt(): \DateTimeInterface
{
return $this->updatedAt;
}

/**
* Init timestampable fields.
*/
public function initTimestampableFields(): void
{
$this->createdAt = new \DateTimeImmutable('now');
$this->updatedAt = new \DateTime('now');
}
}
Loading
Loading