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: 120: 121: 122: 123: 124: 125: 126: 127: 
<?php
namespace Omeka\Authentication\Storage;

use Doctrine\DBAL\DBALException;
use Doctrine\ORM\EntityRepository;
use Zend\Authentication\Storage\StorageInterface;

/**
 * Auth storage wrapper for doctrine objects.
 *
 * Stores the ID instead of the full object, translates between ID and object
 * automatically on read/write.
 */
class DoctrineWrapper implements StorageInterface
{
    /**
     * @var StorageInterface
     */
    protected $storage;

    /**
     * @var EntityRepository
     */
    protected $repository;

    /**
     * Create the wrapper around the given storage method, looking up users
     * from the given repository.
     *
     * @param StorageInterface $storage "Base" storage class
     * @param EntityRepository $repository Repository storing Users
     */
    public function __construct(StorageInterface $storage, EntityRepository $repository)
    {
        $this->setStorage($storage);
        $this->setRepository($repository);
    }

    /**
     * {@inheritDoc}
     */
    public function isEmpty()
    {
        if ($this->storage->isEmpty()) {
            return true;
        }
        if (null === $this->read()) {
            // An identity may exist in a cookie but not in the database.
            return true;
        }
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public function read()
    {
        $identity = $this->storage->read();
        if ($identity) {
            try {
                return $this->repository->find($identity);
            } catch (DBALException $e) {
                // The user table does not exist.
                return null;
            }
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public function write($identity)
    {
        $this->storage->write($identity->getId());
    }

    /**
     * {@inheritDoc}
     */
    public function clear()
    {
        $this->storage->clear();
    }

    /**
     * Set the base storage class to wrap.
     *
     * @param StorageInterface $storage
     */
    public function setStorage(StorageInterface $storage)
    {
        $this->storage = $storage;
    }

    /**
     * Get the storage class being wrapped.
     *
     * @return StorageInterface
     */
    public function getStorage()
    {
        return $this->storage;
    }

    /**
     * Set the repository for looking up User objects.
     *
     * @param EntityRepository $repository
     */
    public function setRepository(EntityRepository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * Get the repository for looking up User objects.
     *
     * @return EntityRepository
     */
    public function getRepository()
    {
        return $this->repository;
    }
}