custom/plugins/SwagAmazonPay/src/DataAbstractionLayer/Entity/SignUpToken/SignUpTokenService.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\AmazonPay\DataAbstractionLayer\Entity\SignUpToken;
  8. use Shopware\Core\Defaults;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  13. use Shopware\Core\Framework\Uuid\Uuid;
  14. class SignUpTokenService implements SignUpTokenServiceInterface
  15. {
  16.     /**
  17.      * @var EntityRepositoryInterface
  18.      */
  19.     private $signUpTokenRepository;
  20.     public function __construct(EntityRepositoryInterface $signUpTokenRepository)
  21.     {
  22.         $this->signUpTokenRepository $signUpTokenRepository;
  23.     }
  24.     public function create(Context $context): string
  25.     {
  26.         $id Uuid::randomHex();
  27.         $this->signUpTokenRepository->create([
  28.             [
  29.                 'id' => $id,
  30.             ],
  31.         ], $context);
  32.         return $id;
  33.     }
  34.     public function validate(string $idContext $context): bool
  35.     {
  36.         if (!Uuid::isValid($id)) {
  37.             return false;
  38.         }
  39.         $criteria = new Criteria([$id]);
  40.         $criteria->setLimit(1);
  41.         $tokenValid $this->signUpTokenRepository->searchIds($criteria$context)->getTotal() === 1;
  42.         if (!$tokenValid) {
  43.             return false;
  44.         }
  45.         $this->signUpTokenRepository->delete([
  46.             [
  47.                 'id' => $id,
  48.             ],
  49.         ], $context);
  50.         return true;
  51.     }
  52.     public function cleanup(Context $context): void
  53.     {
  54.         $lastWeek = new \DateTime('now -7 days');
  55.         $lastWeek $lastWeek->setTimezone(new \DateTimeZone('UTC'));
  56.         $criteria = new Criteria();
  57.         $criteria->setLimit(500);
  58.         $criteria->addFilter(
  59.             new RangeFilter(
  60.                 'createdAt',
  61.                 [
  62.                     RangeFilter::LTE => $lastWeek->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  63.                 ]
  64.             )
  65.         );
  66.         $ids $this->signUpTokenRepository->searchIds($criteria$context)->getIds();
  67.         $deleteData = [];
  68.         foreach ($ids as $id) {
  69.             $deleteData[] = [
  70.                 'id' => $id,
  71.             ];
  72.         }
  73.         if ($deleteData === []) {
  74.             return;
  75.         }
  76.         $this->signUpTokenRepository->delete($deleteData$context);
  77.     }
  78. }