Skip to content

Instantly share code, notes, and snippets.

@mlebkowski
Created June 21, 2017 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlebkowski/5b3bf74ad795f20005000b746045e064 to your computer and use it in GitHub Desktop.
Save mlebkowski/5b3bf74ad795f20005000b746045e064 to your computer and use it in GitHub Desktop.
<?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