[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/web-auth/metadata-service/src/ -> SimpleMetadataStatementRepository.php (source)

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  /*
   6   * The MIT License (MIT)
   7   *
   8   * Copyright (c) 2014-2019 Spomky-Labs
   9   *
  10   * This software may be modified and distributed under the terms
  11   * of the MIT license.  See the LICENSE file for details.
  12   */
  13  
  14  namespace Webauthn\MetadataService;
  15  
  16  use DateTimeImmutable;
  17  use Psr\Cache\CacheItemPoolInterface;
  18  use Throwable;
  19  
  20  class SimpleMetadataStatementRepository implements MetadataStatementRepository
  21  {
  22      /**
  23       * @var CacheItemPoolInterface
  24       */
  25      private $cacheItemPool;
  26  
  27      /**
  28       * @var MetadataService[]
  29       */
  30      private $services = [];
  31  
  32      /**
  33       * @var SingleMetadata[]
  34       */
  35      private $singleStatements = [];
  36  
  37      public function __construct(CacheItemPoolInterface $cacheItemPool)
  38      {
  39          $this->cacheItemPool = $cacheItemPool;
  40      }
  41  
  42      public function addService(string $name, MetadataService $service): void
  43      {
  44          $this->services[$name] = $service;
  45      }
  46  
  47      public function addSingleStatement(string $name, SingleMetadata $singleStatements): void
  48      {
  49          $this->singleStatements[$name] = $singleStatements;
  50      }
  51  
  52      public function findOneByAAGUID(string $aaguid): ?MetadataStatement
  53      {
  54          $metadataStatement = $this->findOneByAAGUIDFromServices($aaguid);
  55          if (null !== $metadataStatement) {
  56              return $metadataStatement;
  57          }
  58  
  59          return $this->findOneByAAGUIDFromSingleStatements($aaguid);
  60      }
  61  
  62      private function findOneByAAGUIDFromSingleStatements(string $aaguid): ?MetadataStatement
  63      {
  64          foreach ($this->singleStatements as $name => $singleStatement) {
  65              try {
  66                  $singleCacheItem = $this->cacheItemPool->getItem(sprintf('MDS-%s', $name));
  67                  if (!$singleCacheItem->isHit()) {
  68                      $metadataStatement = $singleStatement->getMetadataStatement();
  69                      $singleCacheItem->set($metadataStatement);
  70                      $this->cacheItemPool->save($singleCacheItem);
  71                  } else {
  72                      $metadataStatement = $singleCacheItem->get();
  73                  }
  74  
  75                  if ($metadataStatement->getAaguid() === $aaguid) {
  76                      return $metadataStatement;
  77                  }
  78              } catch (Throwable $throwable) {
  79                  continue;
  80              }
  81          }
  82  
  83          return null;
  84      }
  85  
  86      private function findOneByAAGUIDFromServices(string $aaguid): ?MetadataStatement
  87      {
  88          foreach ($this->services as $name => $service) {
  89              try {
  90                  $tocCacheItem = $this->cacheItemPool->getItem(sprintf('TOC-%s', $name));
  91                  if (!$tocCacheItem->isHit()) {
  92                      $tableOfContent = $service->getMetadataTOCPayload();
  93                      $tocCacheItem->set($tableOfContent);
  94                      $this->cacheItemPool->save($tocCacheItem);
  95                      $needCacheUpdate = true;
  96                  } else {
  97                      $tableOfContent = $tocCacheItem->get();
  98                      $nextUpdate = DateTimeImmutable::createFromFormat('Y-m-d', $tableOfContent->getNextUpdate());
  99                      if (false === $nextUpdate) {
 100                          $needCacheUpdate = true;
 101                      } else {
 102                          $needCacheUpdate = $nextUpdate->getTimestamp() < time();
 103                          if ($needCacheUpdate) {
 104                              $tableOfContent = $service->getMetadataTOCPayload();
 105                              $tocCacheItem->set($tableOfContent);
 106                              $this->cacheItemPool->save($tocCacheItem);
 107                          }
 108                      }
 109                  }
 110              } catch (Throwable $throwable) {
 111                  continue;
 112              }
 113              foreach ($tableOfContent->getEntries() as $entry) {
 114                  $url = $entry->getUrl();
 115                  if (null === $url) {
 116                      continue;
 117                  }
 118                  try {
 119                      $mdsCacheItem = $this->cacheItemPool->getItem(sprintf('MDS-%s', urlencode($url)));
 120                      if ($mdsCacheItem->isHit() && !$needCacheUpdate) {
 121                          $metadataStatement = $mdsCacheItem->get();
 122                      } else {
 123                          $metadataStatement = $service->getMetadataStatementFor($entry);
 124                          $mdsCacheItem->set($metadataStatement);
 125                          $this->cacheItemPool->save($mdsCacheItem);
 126                      }
 127                      if ($metadataStatement->getAaguid() === $aaguid) {
 128                          return $metadataStatement;
 129                      }
 130                  } catch (Throwable $throwable) {
 131                      continue;
 132                  }
 133              }
 134          }
 135  
 136          return null;
 137      }
 138  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer