UserConfig.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\ResourceModel;
  25. use Magento\Framework\Json\DecoderInterface;
  26. use Magento\Framework\Json\EncoderInterface;
  27. use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
  28. /**
  29. * @SuppressWarnings(PHPMD.CamelCaseMethodName)
  30. */
  31. class UserConfig extends AbstractDb
  32. {
  33. /**
  34. * @var DecoderInterface
  35. */
  36. private $decoder;
  37. /**
  38. * @var EncoderInterface
  39. */
  40. private $encoder;
  41. public function __construct(
  42. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  43. DecoderInterface $decoder,
  44. EncoderInterface $encoder,
  45. $connectionName = null
  46. ) {
  47. parent::__construct($context, $connectionName);
  48. $this->decoder = $decoder;
  49. $this->encoder = $encoder;
  50. }
  51. protected function _construct()
  52. {
  53. $this->_init('msp_tfa_user_config', 'msp_tfa_user_config_id');
  54. }
  55. public function _afterLoad(\Magento\Framework\Model\AbstractModel $object)
  56. {
  57. parent::_afterLoad($object);
  58. try {
  59. $object->setData('config', $this->decoder->decode($object->getData('encoded_config')));
  60. } catch (\Exception $e) {
  61. $object->setData('config', []);
  62. }
  63. try {
  64. $object->setData('providers', $this->decoder->decode($object->getData('encoded_providers')));
  65. } catch (\Exception $e) {
  66. $object->setData('providers', []);
  67. }
  68. return $this;
  69. }
  70. public function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
  71. {
  72. $object->setData('encoded_config', $this->encoder->encode($object->getData('config')));
  73. $object->setData('encoded_providers', $this->encoder->encode($object->getData('providers')));
  74. parent::_beforeSave($object);
  75. }
  76. }