custom/plugins/MagmodulesWebshopnl/src/Core/Api/Controller/Info/InfoController.php line 114

  1. <?php
  2. declare(strict_types=1);
  3. namespace MagmodulesWebshopnl\Core\Api\Controller\Info;
  4. use Composer\InstalledVersions;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Plugin\PluginEntity;
  10. use Shopware\Core\Framework\Store\Services\InstanceService;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. /**
  17.  * @Route(defaults={"_routeScope"={"storefront"}})
  18.  */
  19. class InfoController extends AbstractController
  20. {
  21.     /**
  22.      * @var EntityRepository
  23.      */
  24.     public $webshopnlProductFeedRepository;
  25.     /**
  26.      * @var SystemConfigService
  27.      */
  28.     public $systemConfigService;
  29.     /**
  30.      * @var InstanceService
  31.      */
  32.     public $instanceService;
  33.     /**
  34.      * @var EntityRepository
  35.      */
  36.     private $pluginRepository;
  37.     public function __construct(
  38.         EntityRepository $webshopnlProductFeedRepository,
  39.         SystemConfigService $systemConfigService,
  40.         EntityRepository $pluginRepository
  41.     ) {
  42.         $this->webshopnlProductFeedRepository $webshopnlProductFeedRepository;
  43.         $this->systemConfigService $systemConfigService;
  44.         $this->pluginRepository $pluginRepository;
  45.     }
  46.     /**
  47.      * @Route("/api/webshopnl/info",
  48.      *     name="api.webshopnl.info",  methods={"GET"},
  49.      *     defaults={"auth_required"=false, "_routeScope"={"administration"}})
  50.      */
  51.     public function getInfo(Request $requestContext $context): JsonResponse
  52.     {
  53.         $bearerToken $request->headers->get('Authorization');
  54.         if ($bearerToken) {
  55.             $bearerToken str_replace("Bearer """$bearerToken);
  56.             $integrationToken $this->systemConfigService->get('MagmodulesWebshopnl.settings.integrationToken');
  57.             if ($bearerToken === $integrationToken) {
  58.                 $data $this->getProductFeedData($context);
  59.                 $pluginData $this->pluginData($context);
  60.                 $installationId implode('-'sscanf($pluginData->getId(), '%8s%4s%4s%4s%12s'));
  61.                 return new JsonResponse([
  62.                     'enabled' => $this->systemConfigService->get('MagmodulesWebshopnl.settings.enable'),
  63.                     'shopware_version' => $this->getShopwareVersion(),
  64.                     "plugin_version" => $pluginData->getVersion(),
  65.                     "installation_id" => $installationId,
  66.                     'feed_url' => $this->getFeedUrl($data),
  67.                     'order_post_url' => $request->server->get('APP_URL') . '/api/webshopnl/order',
  68.                     'last_order_import' => 'dassadas',
  69.                     'active_stores' => $this->getActiveStore($data),
  70.                 ]);
  71.             } else {
  72.                 return new JsonResponse([
  73.                     'status' => 401,
  74.                     'title' => 'The resource owner or authorization server denied the request',
  75.                     "detail" => 'Access token could not be verified',
  76.                 ]);
  77.             }
  78.         } else {
  79.             return new JsonResponse([
  80.                 "status" => 401,
  81.                 "title" => 'The resource owner or authorization server denied the request.',
  82.                 "detail" => 'Missing Authorization header',
  83.             ]);
  84.         }
  85.     }
  86.     public function getShopwareVersion()
  87.     {
  88.         if (InstalledVersions::isInstalled('shopware/platform')) {
  89.             return InstalledVersions::getVersion('shopware/platform');
  90.         } else {
  91.             return InstalledVersions::getVersion('shopware/core');
  92.         }
  93.     }
  94.     public function pluginData($context): PluginEntity
  95.     {
  96.         $nameSpaceExplode explode('\\'__NAMESPACE__);
  97.         $pluginName $nameSpaceExplode[0];
  98.         $criteria = new Criteria();
  99.         $criteria->addFilter(new EqualsFilter('plugin.name'$pluginName));
  100.         return $this->pluginRepository->search($criteria$context)->first();
  101.     }
  102.     public function getProductFeedData($context)
  103.     {
  104.         $criteria = new Criteria();
  105.         $criteria->addAssociation('salesChannel');
  106.         $criteria->addFilter(new EqualsFilter('salesChannel.active'true));
  107.         return $this->webshopnlProductFeedRepository->search($criteria$context);
  108.     }
  109.     public function getFeedUrl($data): array
  110.     {
  111.         $feedUrls = [];
  112.         foreach ($data as $feedData) {
  113.             $feedUrls[] = [
  114.                 'salesChannel' => $feedData->getId(),
  115.                 'url' => $feedData->getProductFeedUrl(),
  116.             ];
  117.         }
  118.         return $feedUrls;
  119.     }
  120.     public function getActiveStore($data): array
  121.     {
  122.         $storeIds = [];
  123.         foreach ($data as $feedData) {
  124.             if ($this->systemConfigService->get('MagmodulesWebshopnl.settings.enable',
  125.                 $feedData->getSalesChannelId())
  126.             ) {
  127.                 $storeIds[] = [
  128.                     'integration' => $this->systemConfigService->get('MagmodulesWebshopnl.settings.enable',
  129.                         $feedData->getSalesChannelId()),
  130.                     'store_id' => $feedData->getSalesChannelId(),
  131.                     'store_name' => $feedData->getSalesChannel()->getName(),
  132.                 ];
  133.             }
  134.         }
  135.         return $storeIds;
  136.     }
  137. }