Model.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 1.2.2
  11. */
  12. namespace PrivateBin;
  13. use PrivateBin\Model\Paste;
  14. use PrivateBin\Persistence\PurgeLimiter;
  15. /**
  16. * Model
  17. *
  18. * Factory of PrivateBin instance models.
  19. */
  20. class Model
  21. {
  22. /**
  23. * Configuration.
  24. *
  25. * @var Configuration
  26. */
  27. private $_conf;
  28. /**
  29. * Data storage.
  30. *
  31. * @var AbstractData
  32. */
  33. private $_store = null;
  34. /**
  35. * Factory constructor.
  36. *
  37. * @param configuration $conf
  38. */
  39. public function __construct(Configuration $conf)
  40. {
  41. $this->_conf = $conf;
  42. }
  43. /**
  44. * Get a paste, optionally a specific instance.
  45. *
  46. * @param string $pasteId
  47. * @return Paste
  48. */
  49. public function getPaste($pasteId = null)
  50. {
  51. $paste = new Paste($this->_conf, $this->_getStore());
  52. if ($pasteId !== null) {
  53. $paste->setId($pasteId);
  54. }
  55. return $paste;
  56. }
  57. /**
  58. * Checks if a purge is necessary and triggers it if yes.
  59. */
  60. public function purge()
  61. {
  62. PurgeLimiter::setConfiguration($this->_conf);
  63. if (PurgeLimiter::canPurge()) {
  64. $this->_getStore()->purge($this->_conf->getKey('batchsize', 'purge'));
  65. }
  66. }
  67. /**
  68. * Gets, and creates if neccessary, a store object
  69. *
  70. * @return AbstractData
  71. */
  72. private function _getStore()
  73. {
  74. if ($this->_store === null) {
  75. $this->_store = forward_static_call(
  76. 'PrivateBin\\Data\\' . $this->_conf->getKey('class', 'model') . '::getInstance',
  77. $this->_conf->getSection('model_options')
  78. );
  79. }
  80. return $this->_store;
  81. }
  82. }