02-database_versions_manager.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace cx_newsletter;
  3. use \get_option;
  4. use \update_option;
  5. use \delete_option;
  6. use \exception;
  7. class database_versions_manager {
  8. public function get_installed() : ?int {
  9. $version = get_option($this->get_database_version_name(), null);
  10. if ($version === null) {
  11. return null;
  12. }
  13. $version = intval($version);
  14. if ($version === 0) {
  15. return null;
  16. }
  17. return $version;
  18. }
  19. public function is_installed() : bool {
  20. return $this->get_installed() !== null;
  21. }
  22. public function set_installed(?int $version) : void {
  23. if ($version === null) {
  24. delete_option($this->get_database_version_name());
  25. return;
  26. }
  27. if ($version <= 0) {
  28. throw new exception('Version must be greaten than 0.');
  29. }
  30. update_option($this->get_database_version_name(), strval($version));
  31. }
  32. private function get_database_version_name() : string {
  33. return 'cx_newsletter_database_version';
  34. }
  35. }