Skip to content

Instantly share code, notes, and snippets.

@markitosgv
Last active August 29, 2015 14:13
Show Gist options
  • Save markitosgv/1476c4a5594a57a988af to your computer and use it in GitHub Desktop.
Save markitosgv/1476c4a5594a57a988af to your computer and use it in GitHub Desktop.
Wrapper for Object Manager from Request
<?php
namespace xxx\xxxAPIBundle\Services;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Class ObjectManagerRequest
* Wrapper for Object Manager from Request
*
* @package xxx\xxxAPIBundle\Services
*/
class ObjectManagerRequest
{
protected $doctrine;
protected $requestStack;
protected $country;
/**
* @param RegistryInterface $doctrine
* @param RequestStack $requestStack
*/
public function __construct(RegistryInterface $doctrine, RequestStack $requestStack)
{
$this->doctrine = $doctrine;
$this->requestStack = $requestStack;
}
/**
* Get Object Manager from Request
*
* @param null $country
*
* @return \Doctrine\Common\Persistence\ObjectManager
* @throws \InvalidArgumentException
*/
public function getObjectManager($country = null)
{
$this->country = $country;
if (null === $this->country && null !== $this->requestStack->getMasterRequest()) {
$this->country = $this->requestStack->getMasterRequest()->get('country');
}
return $this->doctrine->getManager($this->country);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment