Fluxter /
SaasProviderBundle
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | /* |
||||||
| 4 | * This file is part of the SaasProviderBundle package. |
||||||
| 5 | * (c) Fluxter <http://fluxter.net/> |
||||||
| 6 | * For the full copyright and license information, please view the LICENSE |
||||||
| 7 | * file that was distributed with this source code. |
||||||
| 8 | */ |
||||||
| 9 | |||||||
| 10 | namespace Fluxter\SaasProviderBundle\Service; |
||||||
| 11 | |||||||
| 12 | use Doctrine\ORM\EntityManagerInterface; |
||||||
| 13 | use Exception; |
||||||
| 14 | use Fluxter\SaasProviderBundle\Model\Exception\ClientCouldNotBeDiscoveredException; |
||||||
| 15 | use Fluxter\SaasProviderBundle\Model\SaasClientInterface; |
||||||
| 16 | use Fluxter\SaasProviderBundle\Model\SaasParameterInterface; |
||||||
| 17 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||||||
| 18 | use Symfony\Component\HttpFoundation\RequestStack; |
||||||
| 19 | use Symfony\Component\HttpFoundation\Session\SessionInterface; |
||||||
| 20 | |||||||
| 21 | class SaasClientService |
||||||
| 22 | { |
||||||
| 23 | private const SaasClientSessionIndex = 'SAASCLIENT'; |
||||||
| 24 | |||||||
| 25 | /** @var EntityManagerInterface */ |
||||||
| 26 | private $em; |
||||||
| 27 | |||||||
| 28 | /** @var SessionInterface */ |
||||||
| 29 | private $session; |
||||||
| 30 | |||||||
| 31 | /** @var string */ |
||||||
| 32 | private $saasClientEntity; |
||||||
| 33 | |||||||
| 34 | /** @var RequestStack */ |
||||||
| 35 | private $requestStack; |
||||||
| 36 | |||||||
| 37 | public function __construct(ContainerInterface $container, EntityManagerInterface $em, SessionInterface $session, RequestStack $requestStack) |
||||||
| 38 | { |
||||||
| 39 | $this->em = $em; |
||||||
| 40 | $this->session = $session; |
||||||
| 41 | $this->saasClientEntity = $container->getParameter('saas_provider.client_entity'); |
||||||
| 42 | $this->requestStack = $requestStack; |
||||||
| 43 | } |
||||||
| 44 | |||||||
| 45 | public function createClient(array $parameters) |
||||||
| 46 | { |
||||||
| 47 | /** @var SaasClientInterface $client */ |
||||||
| 48 | $client = new $this->saasClientEntity(); |
||||||
| 49 | |||||||
| 50 | /** @var SaasParameterInterface $parameter */ |
||||||
| 51 | foreach ($parameters as $parameter) { |
||||||
| 52 | $client->addParameter($parameter); |
||||||
| 53 | } |
||||||
| 54 | |||||||
| 55 | $this->em->persist($client); |
||||||
| 56 | $this->em->flush($client); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 57 | |||||||
| 58 | return $client; |
||||||
| 59 | } |
||||||
| 60 | |||||||
| 61 | public function tryGetCurrentClient(bool $autodiscover = true): ?SaasClientInterface |
||||||
| 62 | { |
||||||
| 63 | try { |
||||||
| 64 | if (!$this->session->has(self::SaasClientSessionIndex) || null == $this->session->get(self::SaasClientSessionIndex)) { |
||||||
| 65 | if ($autodiscover) { |
||||||
| 66 | $this->discoverClient(); |
||||||
| 67 | |||||||
| 68 | return $this->tryGetCurrentClient(false); |
||||||
| 69 | } |
||||||
| 70 | |||||||
| 71 | return null; |
||||||
| 72 | } |
||||||
| 73 | |||||||
| 74 | $repo = $this->em->getRepository($this->saasClientEntity); |
||||||
| 75 | /** @var SaasClientInterface $client */ |
||||||
| 76 | $client = $repo->findOneById($this->session->get(self::SaasClientSessionIndex)); |
||||||
|
0 ignored issues
–
show
The method
findOneById() does not exist on Doctrine\Persistence\ObjectRepository. Did you maybe mean findOneBy()?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 77 | if (null == $client) { |
||||||
| 78 | throw new ClientCouldNotBeDiscoveredException(); |
||||||
| 79 | } |
||||||
| 80 | |||||||
| 81 | // Validate |
||||||
| 82 | $url = $this->getCurrentHttpHost(); |
||||||
| 83 | if (strtolower($client->getUrl()) != strtolower($url)) { |
||||||
| 84 | $this->resetSaasClientSession(); |
||||||
| 85 | $this->discoverClient(); |
||||||
| 86 | |||||||
| 87 | return $this->tryGetCurrentClient(false); |
||||||
| 88 | } |
||||||
| 89 | |||||||
| 90 | if (null == $client) { |
||||||
| 91 | throw new ClientCouldNotBeDiscoveredException(); |
||||||
| 92 | } |
||||||
| 93 | |||||||
| 94 | return $client; |
||||||
| 95 | } catch (Exception $ex) { |
||||||
| 96 | return null; |
||||||
| 97 | } |
||||||
| 98 | } |
||||||
| 99 | |||||||
| 100 | /** |
||||||
| 101 | * Returns the current client, recognized by the url and the client entity. |
||||||
| 102 | * |
||||||
| 103 | * @return SaasClientInterface|null |
||||||
| 104 | */ |
||||||
| 105 | public function getCurrentClient(bool $autodiscover = true): ?SaasClientInterface |
||||||
| 106 | { |
||||||
| 107 | $client = $this->tryGetCurrentClient($autodiscover); |
||||||
| 108 | |||||||
| 109 | return $client; |
||||||
| 110 | } |
||||||
| 111 | |||||||
| 112 | /** |
||||||
| 113 | * Returns the current http host in lower case. |
||||||
| 114 | * For example: "http://localhost:8000" returns "localhost" |
||||||
| 115 | * https://test.test.de would return "test.test.de". |
||||||
| 116 | */ |
||||||
| 117 | public function getCurrentHttpHost(): string |
||||||
| 118 | { |
||||||
| 119 | $url = $this->requestStack->getCurrentRequest()->getHttpHost(); |
||||||
| 120 | $url = strtolower($url); |
||||||
| 121 | if (false !== strpos($url, ':')) { |
||||||
| 122 | $url = preg_replace('/:(.*)/', '', $url); |
||||||
| 123 | } |
||||||
| 124 | |||||||
| 125 | return strtolower($url); |
||||||
| 126 | } |
||||||
| 127 | |||||||
| 128 | private function discoverClient(): ?SaasClientInterface |
||||||
| 129 | { |
||||||
| 130 | $url = $this->getCurrentHttpHost(); |
||||||
| 131 | $repo = $this->em->getRepository($this->saasClientEntity); |
||||||
| 132 | $client = $repo->findOneBy(['url' => $url]); |
||||||
| 133 | if (null == $client) { |
||||||
| 134 | return null; |
||||||
| 135 | } |
||||||
| 136 | |||||||
| 137 | $this->saveSaasClientSession($client); |
||||||
| 138 | |||||||
| 139 | return $client; |
||||||
| 140 | } |
||||||
| 141 | |||||||
| 142 | private function resetSaasClientSession() |
||||||
| 143 | { |
||||||
| 144 | $this->session->set(self::SaasClientSessionIndex, null); |
||||||
| 145 | } |
||||||
| 146 | |||||||
| 147 | private function saveSaasClientSession(SaasClientInterface $client) |
||||||
| 148 | { |
||||||
| 149 | $this->session->set(self::SaasClientSessionIndex, $client->getId()); |
||||||
| 150 | } |
||||||
| 151 | } |
||||||
| 152 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.