Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more if statements to prevent exceptions we can't catch somehow #1647

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions api/src/Service/ApplicationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Exception\GatewayException;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
Expand Down Expand Up @@ -37,15 +38,15 @@ public function __construct(
public function getApplication(): Application
{
// If application is already in the session
if ($this->session->has('application')) {
if (empty($this->session) === false && $this->session->has('application') === true) {
$application = $this->entityManager->getRepository('App:Application')->findOneBy(['id' => $this->session->get('application')]);
if ($application !== null) {
return $application;
}
}

// Find application using the publicKey
$public = ($this->request->headers->get('public') ?? $this->request->query->get('public'));
$public = $this->getHeaderOrQuery('public');
if (empty($public) === false) {
$application = $this->entityManager->getRepository('App:Application')->findOneBy(['public' => $public]);
if ($application !== null) {
Expand All @@ -55,7 +56,7 @@ public function getApplication(): Application
}

// Find application using the host/domain
$host = ($this->request->headers->get('host') ?? $this->request->query->get('host'));
$host = $this->getHeaderOrQuery('host');
if (empty($host) === false) {
$applications = $this->entityManager->getRepository('App:Application')->findByDomain($host);
if (count($applications) > 0) {
Expand All @@ -81,4 +82,29 @@ public function getApplication(): Application
'data' => $data ?? null, 'path' => $public ?? $host ?? 'Header', 'responseType' => Response::HTTP_FORBIDDEN,
]);
}


/**
* Tries to get a given key from the headers of the current request, else from the query params of the current request.
*
* @param string $key The key to get.
*
* @return bool|float|int|string|InputBag|null The value of the header or query or null if none was found.
*/
private function getHeaderOrQuery(string $key)
{
if (empty($this->request) === true) {
return null;
}

if (empty($this->request->headers) === false && $this->request->headers->has($key) === true) {
return $this->request->headers->get($key);
}

if (empty($this->request->query) === false && $this->request->query->has($key) === true) {
return $this->request->query->get($key);
}

return null;
}
}
Loading