UserConfigRegistry.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * MageSpecialist
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to info@magespecialist.it so we can send you a copy immediately.
  14. *
  15. * @category MSP
  16. * @package MSP_TwoFactorAuth
  17. * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it)
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. */
  20. /**
  21. * Automatically created by MageSpecialist CodeMonkey
  22. * https://github.com/magespecialist/m2-MSP_CodeMonkey
  23. */
  24. namespace MSP\TwoFactorAuth\Model;
  25. /**
  26. * @SuppressWarnings(PHPMD.ShortVariable)
  27. */
  28. class UserConfigRegistry
  29. {
  30. private $registry = [];
  31. private $registryByKey = [
  32. 'user_id' => [],
  33. ];
  34. /**
  35. * @var \MSP\TwoFactorAuth\Model\UserConfigFactory
  36. */
  37. private $userConfigFactory;
  38. public function __construct(
  39. \MSP\TwoFactorAuth\Model\UserConfigFactory $userConfigFactory
  40. ) {
  41. $this->userConfigFactory = $userConfigFactory;
  42. }
  43. /**
  44. * Remove registry entity by id
  45. * @param int $id
  46. */
  47. public function removeById($id)
  48. {
  49. if (isset($this->registry[$id])) {
  50. unset($this->registry[$id]);
  51. }
  52. foreach (array_keys($this->registryByKey) as $key) {
  53. $reverseMap = array_flip($this->registryByKey[$key]);
  54. if (isset($reverseMap[$id])) {
  55. unset($this->registryByKey[$key][$reverseMap[$id]]);
  56. }
  57. }
  58. }
  59. /**
  60. * Push one object into registry
  61. * @param int $id
  62. * @return \MSP\TwoFactorAuth\Api\Data\UserConfigInterface|null
  63. */
  64. public function retrieveById($id)
  65. {
  66. if (isset($this->registry[$id])) {
  67. return $this->registry[$id];
  68. }
  69. return null;
  70. }
  71. /**
  72. * Retrieve by UserId value
  73. * @param int $value
  74. * @return \MSP\TwoFactorAuth\Api\Data\UserConfigInterface|null
  75. */
  76. public function retrieveByUserId($value)
  77. {
  78. if (isset($this->registryByKey['user_id'][$value])) {
  79. return $this->retrieveById($this->registryByKey['user_id'][$value]);
  80. }
  81. return null;
  82. }
  83. /**
  84. * Push one object into registry
  85. * @param \MSP\TwoFactorAuth\Model\UserConfig $userConfig
  86. */
  87. public function push(\MSP\TwoFactorAuth\Model\UserConfig $userConfig)
  88. {
  89. $this->registry[$userConfig->getId()] = $userConfig->getDataModel();
  90. foreach (array_keys($this->registryByKey) as $key) {
  91. $this->registryByKey[$key][$userConfig->getData($key)] = $userConfig->getId();
  92. }
  93. }
  94. }