Last active
August 29, 2015 14:13
-
-
Save markitosgv/1476c4a5594a57a988af to your computer and use it in GitHub Desktop.
Wrapper for Object Manager from Request
This file contains hidden or 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 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