1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 
<?php
namespace Omeka\Db\Event\Subscriber;

use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Events as DoctrineEvent;
use Omeka\Entity\Resource as OmekaResource;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\Event as ZendEvent;

/**
 * Entity event subscriber.
 *
 * Delegates selected Doctrine lifecycle events to Omeka events.
 */
class Entity implements EventSubscriber
{
    /**
     * @var EventManagerInterface
     */
    protected $events;

    /**
     * Set the service locator.
     */
    public function __construct(EventManagerInterface $events)
    {
        $this->events = $events;
    }

    public function getSubscribedEvents()
    {
        return [
            DoctrineEvent::preRemove, DoctrineEvent::postRemove,
            DoctrineEvent::prePersist, DoctrineEvent::postPersist,
            DoctrineEvent::preUpdate, DoctrineEvent::postUpdate,
        ];
    }

    /**
     * Trigger the entity.remove.pre event.
     *
     * @param LifecycleEventArgs $args
     */
    public function preRemove(LifecycleEventArgs $args)
    {
        $this->trigger('entity.remove.pre', $args);
    }

    /**
     * Trigger the entity.remove.post event.
     *
     * @param LifecycleEventArgs $args
     */
    public function postRemove(LifecycleEventArgs $args)
    {
        $this->trigger('entity.remove.post', $args);
    }

    /**
     * Trigger the entity.persist.pre event.
     *
     * @param LifecycleEventArgs $args
     */
    public function prePersist(LifecycleEventArgs $args)
    {
        $this->trigger('entity.persist.pre', $args);
    }

    /**
     * Trigger the entity.persist.post event.
     *
     * @param LifecycleEventArgs $args
     */
    public function postPersist(LifecycleEventArgs $args)
    {
        $this->trigger('entity.persist.post', $args);
    }

    /**
     * Trigger the entity.update.pre event.
     *
     * @param LifecycleEventArgs $args
     */
    public function preUpdate(LifecycleEventArgs $args)
    {
        $this->trigger('entity.update.pre', $args);
    }

    /**
     * Trigger the entity.update.post event.
     *
     * @param LifecycleEventArgs $args
     */
    public function postUpdate(LifecycleEventArgs $args)
    {
        $this->trigger('entity.update.post', $args);
    }

    /**
     * Compose and trigger the event.
     *
     * @param string $eventName
     * @param LifecycleEventArgs $args
     */
    protected function trigger($eventName, LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();
        $identifiers = [get_class($entity)];
        if ($entity instanceof OmekaResource) {
            // Add the identifier for a generic resource entity.
            $identifiers[] = 'Omeka\Entity\Resource';
        }
        $this->events->setIdentifiers($identifiers);
        $event = new ZendEvent($eventName, $entity);
        $this->events->triggerEvent($event);
    }
}