Model.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.6.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 Data\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. PurgeLimiter::setStore($this->getStore());
  64. if (PurgeLimiter::canPurge()) {
  65. $this->getStore()->purge($this->_conf->getKey('batchsize', 'purge'));
  66. }
  67. }
  68. /**
  69. * Gets, and creates if neccessary, a store object
  70. *
  71. * @return Data\AbstractData
  72. */
  73. public function getStore()
  74. {
  75. if ($this->_store === null) {
  76. $class = 'PrivateBin\\Data\\' . $this->_conf->getKey('class', 'model');
  77. $this->_store = new $class($this->_conf->getSection('model_options'));
  78. }
  79. return $this->_store;
  80. }
  81. }