src/Controller/BaseController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Addon\Installation;
  4. use App\Entity\Config\SmartAlternativeConfig;
  5. use App\Exception\AdminException;
  6. use App\Message\Addon\InstallationConfigureMessage;
  7. use App\Repository\Addon\EshopRepository;
  8. use App\Repository\Addon\InstallationRepository;
  9. use App\Service\ShoptetApiService;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Messenger\MessageBusInterface;
  12. use Symfony\Contracts\Service\Attribute\Required;
  13. abstract class BaseController extends AbstractController
  14. {
  15.     #[Required]
  16.     public InstallationRepository $installationRepo;
  17.     #[Required]
  18.     public EshopRepository $eshopRepo;
  19.     #[Required]
  20.     public ShoptetApiService $apiService;
  21.     /**
  22.      * Finds an installation by Shoptet's own eshop id.
  23.      * @param int $shoptetId Shoptet's Eshop ID.
  24.      * @return Installation|null If found, the corresponding Installation is returned. Otherwise, null.
  25.      */
  26.     protected function findInstallation(int $shoptetId): Installation|null
  27.     {
  28.         $eshop $this->eshopRepo->findOneBy([ 'shoptetId' => $shoptetId ]);
  29.         if (!$eshop) return null;
  30.         $installation $eshop->getInstallation();
  31.         if (!$installation) return null;
  32.         return $installation;
  33.     }
  34.     /**
  35.      * Gets configuration of the installation. Handles situations where the configuration is not yet created.
  36.      * @param Installation $installation Installation for which the configuration should be retrieved.
  37.      * @param int $shoptetId Shoptet ID of the eshop.
  38.      * @param MessageBusInterface $messageBus Message bus to dispatch any needed messages.
  39.      * @return SmartAlternativeConfig Returns the found configuration.
  40.      */
  41.     protected function getConfig(Installation $installationint $shoptetIdMessageBusInterface $messageBus): SmartAlternativeConfig
  42.     {
  43.         $config $installation->getConfig();
  44.         // if config doesn't exist, try sending configuration message again
  45.         if (!$config) {
  46.             $installationMessage = new InstallationConfigureMessage($installation->getId(), $shoptetIdtrue);
  47.             $messageBus->dispatch($installationMessage);
  48.             throw new AdminException(500,
  49.                 "Configuration is not yet created for installation {$installation->getId()}. Without the configuration, the form cannot be loaded.",
  50.                 "installation.missing_configuration"
  51.             );
  52.         }
  53.         return $config;
  54.     }
  55. }