Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
namespace Acme\Services;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache;
use Doctrine\ORM\EntityRepository;
use Kunstmaan\AdminBundle\Entity\User;
use Acme\Entity\Profile;
class ProfileFinder
{
/**
* @var EntityRepository
*/
private $repository;
/**
* @var Cache
*/
private $cache;
public function __construct(EntityRepository $repository, Cache $cache = null)
{
$this->repository = $repository;
$this->cache = $cache ?: new ArrayCache();
}
/**
* @param User|null $user
* @return Profile
*/
public function getProfile(User $user = null)
{
if (null !== $user && $this->cache->contains($user->getEmailCanonical())){
return $this->cache->fetch($user->getEmailCanonical());
}
$profile = ($user ? $this->repository->findOneBy(['user' => $user]) : null) ?: new Profile;
$this->cache->save($user->getEmailCanonical(), $profile);
return $profile;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.