src/Core/Maintenance/System/Service/AppUrlVerifier.php line 62

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Maintenance\System\Service;
  3. use Doctrine\DBAL\Connection;
  4. use GuzzleHttp\Client;
  5. use GuzzleHttp\Exception\GuzzleException;
  6. use GuzzleHttp\RequestOptions;
  7. use Shopware\Core\DevOps\Environment\EnvironmentHelper;
  8. use Shopware\Core\Framework\Log\Package;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. /**
  12.  * @internal
  13.  */
  14. #[Package('core')]
  15. class AppUrlVerifier
  16. {
  17.     public function __construct(private readonly Client $guzzle, private readonly Connection $connection, private readonly string $appEnv, private readonly bool $appUrlCheckDisabled)
  18.     {
  19.     }
  20.     public function isAppUrlReachable(Request $request): bool
  21.     {
  22.         if ($this->appEnv !== 'prod' || $this->appUrlCheckDisabled) {
  23.             // dev and test system are often not reachable and this is totally fine
  24.             // problems occur if a prod system can't be reached
  25.             // the check can be disabled manually e.g. for cloud
  26.             return true;
  27.         }
  28.         /** @var string $appUrl */
  29.         $appUrl EnvironmentHelper::getVariable('APP_URL');
  30.         if (str_starts_with($request->getUri(), $appUrl)) {
  31.             // if the request was made to the same domain as the APP_URL we know that it can be reached
  32.             return true;
  33.         }
  34.         try {
  35.             $response $this->guzzle->get(rtrim($appUrl'/') . '/api/_info/version', [
  36.                 'headers' => [
  37.                     'Authorization' => $request->headers->get('Authorization'),
  38.                 ],
  39.                 RequestOptions::TIMEOUT => 1,
  40.                 RequestOptions::CONNECT_TIMEOUT => 1,
  41.             ]);
  42.             if ($response->getStatusCode() === Response::HTTP_OK) {
  43.                 return true;
  44.             }
  45.         } catch (GuzzleException) {
  46.             return false;
  47.         }
  48.         return false;
  49.     }
  50.     public function hasAppsThatNeedAppUrl(): bool
  51.     {
  52.         $foundApp $this->connection->fetchOne('SELECT 1 FROM app WHERE app_secret IS NOT NULL');
  53.         return $foundApp === '1';
  54.     }
  55. }