Model.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 0.22
  11. */
  12. namespace PrivateBin;
  13. use PrivateBin\Data;
  14. use PrivateBin\Model\Paste;
  15. use PrivateBin\Persistence\PurgeLimiter;
  16. /**
  17. * Model
  18. *
  19. * Factory of PrivateBin instance models.
  20. */
  21. class Model
  22. {
  23. /**
  24. * Configuration.
  25. *
  26. * @var Configuration
  27. */
  28. private $_conf;
  29. /**
  30. * Data storage.
  31. *
  32. * @var AbstractData
  33. */
  34. private $_store = null;
  35. /**
  36. * Factory constructor.
  37. *
  38. * @param configuration $conf
  39. * @return void
  40. */
  41. public function __construct(Configuration $conf)
  42. {
  43. $this->_conf = $conf;
  44. }
  45. /**
  46. * Get a paste, optionally a specific instance.
  47. *
  48. * @param string $pasteId
  49. * @return Paste
  50. */
  51. public function getPaste($pasteId = null)
  52. {
  53. $paste = new Paste($this->_conf, $this->_getStore());
  54. if ($pasteId !== null) {
  55. $paste->setId($pasteId);
  56. }
  57. return $paste;
  58. }
  59. /**
  60. * Checks if a purge is necessary and triggers it if yes.
  61. *
  62. * @return void
  63. */
  64. public function purge()
  65. {
  66. PurgeLimiter::setConfiguration($this->_conf);
  67. if (PurgeLimiter::canPurge()) {
  68. $this->_getStore()->purge($this->_conf->getKey('batchsize', 'purge'));
  69. }
  70. }
  71. /**
  72. * Gets, and creates if neccessary, a store object
  73. *
  74. * @return AbstractData
  75. */
  76. private function _getStore()
  77. {
  78. if ($this->_store === null) {
  79. $this->_store = forward_static_call(
  80. 'PrivateBin\\Data\\' . $this->_conf->getKey('class', 'model') . '::getInstance',
  81. $this->_conf->getSection('model_options')
  82. );
  83. }
  84. return $this->_store;
  85. }
  86. }