Skip to content

Instantly share code, notes, and snippets.

@achraf-jeday
Created June 28, 2019 10:09
Show Gist options
  • Save achraf-jeday/278da595be7bfd4d230d5b916dbcb2f4 to your computer and use it in GitHub Desktop.
Save achraf-jeday/278da595be7bfd4d230d5b916dbcb2f4 to your computer and use it in GitHub Desktop.
Dependency injection of a service that is already in the container (Symfony 4)
<?php
namespace CoreBundle\Service;
use CoreBundle\Entity\User;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Adapter\AdapterInterface;
class UserTokenService
{
const RESET_TOKEN = "RESET_PASSWORD_";
const CONFIRMATION_TOKEN = "CONFIRMATION_TOKEN_";
/**
* @var AdapterInterface
*/
private $cacheService;
/**
* UserTokenService constructor.
* @param $cacheService
*/
public function __construct(CacheItemPoolInterface $cacheService)
{
$this->cacheService = $cacheService;
}
/**
* @param string $token
* @param User $user
*/
public function bindPasswordResetToken($token,User $user)
{
$cacheItem = $this->cacheService->getItem(UserTokenService::RESET_TOKEN . $token);
$cacheItem->expiresAfter(1000);
$cacheItem->set($user);
$this->cacheService->save($cacheItem);
}
/**
* @param string $token
* @return bool|mixed
*/
public function verifyPasswordResetToken($token)
{
if($this->cacheService->hasItem(UserTokenService::RESET_TOKEN . $token)) {
$cacheItem = $this->cacheService->getItem(UserTokenService::RESET_TOKEN . $token);
return $cacheItem->get();
}
return false;
}
/**
* @param string $token
* @param User $user
*/
public function bindConfirmationToken($token,User $user)
{
$cacheItem = $this->cacheService->getItem(UserTokenService::CONFIRMATION_TOKEN . $token);
$cacheItem->expiresAfter(1000);
$cacheItem->set($user);
$this->cacheService->save($cacheItem);
}
/**
* @param string $token
* @return bool|mixed
*/
public function verifyConfirmationToken($token)
{
if($this->cacheService->hasItem(UserTokenService::CONFIRMATION_TOKEN . $token)) {
$cacheItem = $this->cacheService->getItem(UserTokenService::CONFIRMATION_TOKEN . $token);
return $cacheItem->get();
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment