<?php
namespace App\Controller;
use App\Entity\Addon\Installation;
use App\Entity\Config\SmartAlternativeConfig;
use App\Exception\AdminException;
use App\Message\Addon\InstallationConfigureMessage;
use App\Repository\Addon\EshopRepository;
use App\Repository\Addon\InstallationRepository;
use App\Service\ShoptetApiService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Contracts\Service\Attribute\Required;
abstract class BaseController extends AbstractController
{
#[Required]
public InstallationRepository $installationRepo;
#[Required]
public EshopRepository $eshopRepo;
#[Required]
public ShoptetApiService $apiService;
/**
* Finds an installation by Shoptet's own eshop id.
* @param int $shoptetId Shoptet's Eshop ID.
* @return Installation|null If found, the corresponding Installation is returned. Otherwise, null.
*/
protected function findInstallation(int $shoptetId): Installation|null
{
$eshop = $this->eshopRepo->findOneBy([ 'shoptetId' => $shoptetId ]);
if (!$eshop) return null;
$installation = $eshop->getInstallation();
if (!$installation) return null;
return $installation;
}
/**
* Gets configuration of the installation. Handles situations where the configuration is not yet created.
* @param Installation $installation Installation for which the configuration should be retrieved.
* @param int $shoptetId Shoptet ID of the eshop.
* @param MessageBusInterface $messageBus Message bus to dispatch any needed messages.
* @return SmartAlternativeConfig Returns the found configuration.
*/
protected function getConfig(Installation $installation, int $shoptetId, MessageBusInterface $messageBus): SmartAlternativeConfig
{
$config = $installation->getConfig();
// if config doesn't exist, try sending configuration message again
if (!$config) {
$installationMessage = new InstallationConfigureMessage($installation->getId(), $shoptetId, true);
$messageBus->dispatch($installationMessage);
throw new AdminException(500,
"Configuration is not yet created for installation {$installation->getId()}. Without the configuration, the form cannot be loaded.",
"installation.missing_configuration"
);
}
return $config;
}
}