Created
June 21, 2017 09:18
-
-
Save mlebkowski/5b3bf74ad795f20005000b746045e064 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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