custom/plugins/MagmodulesWebshopnl/src/Api/Controller/InfoController.php line 128
<?php
declare(strict_types=1);
namespace MagmodulesWebshopnl\Api\Controller;
use Composer\InstalledVersions;
use MagmodulesWebshopnl\Service\WebshopnlFileExportHandler;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin\PluginEntity;
use Shopware\Core\System\SalesChannel\SalesChannelEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route(defaults={"_routeScope"={"storefront"}})
*/
class InfoController extends AbstractController
{
public function __construct(
private SystemConfigService $systemConfigService,
private EntityRepository $pluginRepository,
private EntityRepository $salesChannelRepository,
private WebshopnlFileExportHandler $fileExportHandler,
) {
}
/**
* @Route("/api/webshopnl/info",
* name="api.webshopnl.info", methods={"GET"},
* defaults={"auth_required"=false, "_routeScope"={"administration"}})
*/
public function getInfo(Request $request, Context $context): JsonResponse
{
$bearerToken = $request->headers->get('Authorization');
if (!$bearerToken) {
return new JsonResponse([
"status" => 401,
"title" => 'The resource owner or authorization server denied the request.',
"detail" => 'Missing Authorization header',
]);
}
$bearerToken = str_replace("Bearer ", "", $bearerToken);
$integrationToken = $this->systemConfigService->get('MagmodulesWebshopnl.settings.integrationToken');
if ($bearerToken !== $integrationToken) {
return new JsonResponse([
"status" => 401,
"title" => 'The resource owner or authorization server denied the request.',
"detail" => 'Incorrect Authorization header',
]);
}
$pluginData = $this->pluginData($context);
$installationId = implode('-', sscanf($pluginData->getId(), '%8s%4s%4s%4s%12s'));
$data = [
'enabled' => $this->systemConfigService->get('MagmodulesWebshopnl.settings.enable'),
'type' => 'shopware',
'shopware_version' => $this->getShopwareVersion(),
'plugin_version' => $pluginData->getVersion(),
'installation_id' => $installationId,
'feed_format' => 'https://cart.webshop.nl/static/miab-v3.json',
'order_post_url' => $request->server->get('APP_URL') . '/api/webshopnl/order/',
'last_order_import' => '',
'active_stores' => [],
];
$criteria = new Criteria();
$criteria->addAssociation('domains');
$salesChannels = $this->salesChannelRepository->search($criteria, $context);
/** @var SalesChannelEntity $salesChannel */
foreach ($salesChannels as $salesChannel) {
if (!$this->systemConfigService->get(
'MagmodulesWebshopnl.settings.productEnable',
$salesChannel->getId())
) {
continue;
}
$filename = $this->systemConfigService->get(
'MagmodulesWebshopnl.settings.fileName',
$salesChannel->getId()
);
$data['active_stores'][] = [
'integration' => true,
'store_id' => $salesChannel->getId(),
'store_name' => $salesChannel->getName(),
'feed_url' => sprintf(
'%s/webshopnl/%s-%s.ndjson',
$request->server->get('APP_URL'),
$salesChannel->getId(),
$filename
),
'feed_update' => (new \DateTime())
->setTimestamp($this->fileExportHandler->getUpdateTime($salesChannel->getId()))
->format('Y-m-d H:i:s'),
];
}
return new JsonResponse($data);
}
public function getShopwareVersion(): ?string
{
if (InstalledVersions::isInstalled('shopware/platform')) {
return InstalledVersions::getVersion('shopware/platform');
}
return InstalledVersions::getVersion('shopware/core');
}
public function pluginData($context): PluginEntity
{
$nameSpaceExplode = explode('\\', __NAMESPACE__);
$pluginName = $nameSpaceExplode[0];
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('plugin.name', $pluginName));
return $this->pluginRepository->search($criteria, $context)->first();
}
}