1
0

Model.php 1.9 KB

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