kosmcode/symfony-entity-event-bundle

1.0.2 2024-08-02 15:09 UTC

This package is not auto-updated.

Last update: 2024-09-28 09:02:20 UTC


README

Packagist License: MIT

Simple library for symfony that allows for better management of entity events

Install

  1. composer require kosmcode/symfony-entity-event-bundle
  2. Append in ./config/bundles.php:
<?php

return [
    ...
    KosmCODE\EntityEventBundle\KosmcodeEntityEventBundle::class => ['all'=> true],
];

Use

  1. Create Notifier for you Entity Class (e.g. in /src/EventListener) - for example, for Page Entity Class
<?php

namespace App\EventListener\Entity;

use App\Entity\Page;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use KosmCODE\EntityEventBundle\Enum\Event\EntityEnum;
use KosmCODE\EntityEventBundle\EventListener\Entity\AbstractNotifier;

#[AsEntityListener(event: EntityEnum::prePersist->value, method: EntityEnum::prePersist->name, entity: Page::class)]
#[AsEntityListener(event: EntityEnum::preUpdate->value, method: EntityEnum::preUpdate->name, entity: Page::class)]
#[AsEntityListener(event: EntityEnum::postUpdate->value, method: EntityEnum::postUpdate->name, entity: Page::class)]
#[AsEntityListener(event: EntityEnum::postRemove->value, method: EntityEnum::postRemove->name, entity: Page::class)]
/**
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
 */
final class PageNotifier extends AbstractNotifier
{
    public function getEntityClassName(): string
    {
        return Page::class;
    }
}

  • Notifier class must extends by AbstractNotifier from Bundle (KosmCODE\EntityEventBundle\EventListener\Entity\AbstractNotifier)
  • Class must have defined AsEntityListener annotations for supported events (EntityEnum contains all events)
  • Class must have implements abstract method getEntityClassName which return Entity class name
  1. Make single entity event class - for example Page Pre Slug Check Event (e.g. in directory /src/EventListener/Event/Page/Pre for better files organization)
<?php

namespace App\EventListener\Entity\Event\Page\Pre;

use Doctrine\Common\EventArgs;
use App\Entity\Page;
use KosmCODE\EntityEventBundle\EventListener\Entity\Event\PreUpdateInterface;
use KosmCODE\EntityEventBundle\EventListener\Entity\Event\PrePersistInterface;
use Symfony\Component\String\Slugger\SluggerInterface;

final class SlugCheckEvent implements PrePersistInterface, PreUpdateInterface
{
    public function __construct(
        private readonly SluggerInterface $slugger,
    )
    {

    }

    /** {@inheritDoc} */
    public function getSupportedEntityClassName(): string
    {
        return Page::class;
    }

    /** {@inheritDoc} */
    public function getOrder(): int
    {
        return 1;
    }

    /** 
     * {@inheritDoc} 
     * 
     * @param Page $entity 
     */
    public function do(object $entity, EventArgs $args, array $entityChangeSet = []): void // @phpstan-ignore-line
    {
        if ($entity->getSlug()) {
            $entity->setSlug(
                $this->slugger->slug(
                    $entity->getSlug()
                )
            );

            return;
        }

        $entity->setSlug(
            $this->slugger->slug(
                $entity->getTitle()
            )
        );
    }
}

  • Defined implemented interfaces point to a given event - in this example, it will execute during entity PrePersist and PreUpdate events (all avaliable interaces are in namespace KosmCODE\EntityEventBundle\EventListener\Entity\Event\)
  • Each interface requires the implementation of methods: getSupportedEntityClassName, getOrder and do
    • getSupportedEntityClassName - must return entity event class
    • getOrder - specifies the order of execution (if there are two or more events with this same order they were added in the order added by the factory)
    • do - do what we want to do :)

Tips & Hacks:

  • All logic is in AbstractNotifier (KosmCODE\EntityEventBundle\EventListener\Entity\AbstractNotifier) - if you need to change the way events work, you can extend this class and overwrite methods

Credits

License

MIT License

Copyright (c) 2023 KosmCODE Maksym Kawelski

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.