custom/plugins/theme/src/Controller/Storefront/AccountBeefClubController.php line 41

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DonCarneTheme\Controller\Storefront;
  4. use DonCarneTheme\Service\GraphQLClient;
  5. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Shopware\Storefront\Controller\StorefrontController;
  8. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  9. use Shopware\Storefront\Page\MetaInformation;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * @RouteScope(scopes={"storefront"})
  15.  */
  16. class AccountBeefClubController extends StorefrontController
  17. {
  18.     private GraphQLClient $graphQLClient;
  19.     public function __construct(
  20.         GraphQLClient $graphQLClient,
  21.         GenericPageLoaderInterface $genericLoader,
  22.     ) {
  23.         $this->graphQLClient $graphQLClient;
  24.         $this->genericLoader $genericLoader;
  25.     }
  26.     /**
  27.      * @Route("/DonCarne/BeefClub", name="frontend.beefclub.membership", methods={"GET"}, defaults={"XmlHttpRequest"=false})
  28.      *
  29.      * @param Request             $request
  30.      * @param SalesChannelContext $context
  31.      *
  32.      * @return Response
  33.      */
  34.     public function listAction(Request $requestSalesChannelContext $context): Response
  35.     {
  36.         $customer $context->getCustomer();
  37.         if (null === $customer) {
  38.             $message $this->trans('beefClub.warning.login-needed');
  39.             $this->addFlash('warning'$message);
  40.             return $this->redirectToRoute('frontend.account.login.page');
  41.         }
  42.         $membership $this->getRuntime($customer);
  43.         $page $this->genericLoader->load($request$context);
  44.         //$page = Page::createFrom($page);
  45.         /*$page->setMetaInformation((new MetaInformation())->assign([
  46.             'robots' => 'noindex,nofollow',
  47.         ]));
  48.         */
  49.         $page->getMetaInformation()->setMetaTitle(
  50.             $this->trans('beefclub.account.meta.title')
  51.         );
  52.         return $this->renderStorefront(
  53.             '@DoncarneTheme/doncarne/account/beefclub.html.twig',
  54.             [
  55.                 'membership' => $membership,
  56.                 'page' => $page,
  57.             ]
  58.         );
  59.     }
  60.     public function getRuntime($customer):false|string {
  61.         $results =  $this->graphQLClient->checkBeefClubValidity($customer->getEmail());
  62.         $beefClubValidityResult $results->getResults();
  63.         if(!$beefClubValidityResult) {
  64.             return false;
  65.         }
  66.         $mebership $beefClubValidityResult->data->membership;
  67.         // If customer has a valid beef club subscription return true check for date //cpohl -> where is the date check?
  68.         if ($mebership && $mebership->activated) {
  69.             $today = new \DateTime();
  70.             $today $today->format('Ymd');
  71.             // Loop on all membership dates. If a date greater than today is found, then return true
  72.             foreach ($mebership->memberships as $mebershipDate) {
  73.                 if ($mebershipDate->validUntil->date->date >= $today) {
  74.                     $date = new \DateTime((string)$mebershipDate->validUntil->date->date);
  75.                     return $date->format('d.m.Y');
  76.                 }
  77.             }
  78.         }
  79.         return false;
  80.     }
  81. }