Model.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.1
  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. * @return void
  39. */
  40. public function __construct(Configuration $conf)
  41. {
  42. $this->_conf = $conf;
  43. }
  44. /**
  45. * Get a paste, optionally a specific instance.
  46. *
  47. * @param string $pasteId
  48. * @return Paste
  49. */
  50. public function getPaste($pasteId = null)
  51. {
  52. $paste = new Paste($this->_conf, $this->_getStore());
  53. if ($pasteId !== null) {
  54. $paste->setId($pasteId);
  55. }
  56. return $paste;
  57. }
  58. /**
  59. * Checks if a purge is necessary and triggers it if yes.
  60. *
  61. * @return void
  62. */
  63. public function purge()
  64. {
  65. PurgeLimiter::setConfiguration($this->_conf);
  66. if (PurgeLimiter::canPurge()) {
  67. $this->_getStore()->purge($this->_conf->getKey('batchsize', 'purge'));
  68. }
  69. }
  70. /**
  71. * Gets, and creates if neccessary, a store object
  72. *
  73. * @return AbstractData
  74. */
  75. private function _getStore()
  76. {
  77. if ($this->_store === null) {
  78. $this->_store = forward_static_call(
  79. 'PrivateBin\\Data\\' . $this->_conf->getKey('class', 'model') . '::getInstance',
  80. $this->_conf->getSection('model_options')
  81. );
  82. }
  83. return $this->_store;
  84. }
  85. }