<?php
declare(strict_types=1);
namespace DonCarneTheme\Controller\Storefront;
use DonCarneTheme\Service\GraphQLClient;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Storefront\Page\GenericPageLoaderInterface;
use Shopware\Storefront\Page\MetaInformation;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @RouteScope(scopes={"storefront"})
*/
class AccountBeefClubController extends StorefrontController
{
private GraphQLClient $graphQLClient;
public function __construct(
GraphQLClient $graphQLClient,
GenericPageLoaderInterface $genericLoader,
) {
$this->graphQLClient = $graphQLClient;
$this->genericLoader = $genericLoader;
}
/**
* @Route("/DonCarne/BeefClub", name="frontend.beefclub.membership", methods={"GET"}, defaults={"XmlHttpRequest"=false})
*
* @param Request $request
* @param SalesChannelContext $context
*
* @return Response
*/
public function listAction(Request $request, SalesChannelContext $context): Response
{
$customer = $context->getCustomer();
if (null === $customer) {
$message = $this->trans('beefClub.warning.login-needed');
$this->addFlash('warning', $message);
return $this->redirectToRoute('frontend.account.login.page');
}
$membership = $this->getRuntime($customer);
$page = $this->genericLoader->load($request, $context);
//$page = Page::createFrom($page);
/*$page->setMetaInformation((new MetaInformation())->assign([
'robots' => 'noindex,nofollow',
]));
*/
$page->getMetaInformation()->setMetaTitle(
$this->trans('beefclub.account.meta.title')
);
return $this->renderStorefront(
'@DoncarneTheme/doncarne/account/beefclub.html.twig',
[
'membership' => $membership,
'page' => $page,
]
);
}
public function getRuntime($customer):false|string {
$results = $this->graphQLClient->checkBeefClubValidity($customer->getEmail());
$beefClubValidityResult = $results->getResults();
if(!$beefClubValidityResult) {
return false;
}
$mebership = $beefClubValidityResult->data->membership;
// If customer has a valid beef club subscription return true check for date //cpohl -> where is the date check?
if ($mebership && $mebership->activated) {
$today = new \DateTime();
$today = $today->format('Ymd');
// Loop on all membership dates. If a date greater than today is found, then return true
foreach ($mebership->memberships as $mebershipDate) {
if ($mebershipDate->validUntil->date->date >= $today) {
$date = new \DateTime((string)$mebershipDate->validUntil->date->date);
return $date->format('d.m.Y');
}
}
}
return false;
}
}