PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/DbPatch/Command/Remove.php

http://github.com/dbpatch/DbPatch
PHP | 154 lines | 74 code | 9 blank | 71 comment | 7 complexity | be17a2d979ded9cc4bddf86843c9c84d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * DbPatch
  4. *
  5. * Copyright (c) 2011, Sandy Pleyte.
  6. * Copyright (c) 2010-2011, Martijn De Letter.
  7. *
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of the authors nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @package DbPatch
  40. * @subpackage Command
  41. * @author Sandy Pleyte
  42. * @author Martijn De Letter
  43. * @copyright 2011 Sandy Pleyte
  44. * @copyright 2010-2011 Martijn De Letter
  45. * @license http://www.opensource.org/licenses/MIT MIT License
  46. * @link http://www.github.com/dbpatch/DbPatch
  47. * @since File available since Release 1.0.0
  48. */
  49. /**
  50. * Remove patch from the changelog command
  51. *
  52. * @package DbPatch
  53. * @subpackage Command
  54. * @author Sandy Pleyte
  55. * @author Martijn De Letter
  56. * @copyright 2011 Sandy Pleyte
  57. * @copyright 2010-2011 Martijn De Letter
  58. * @license http://www.opensource.org/licenses/MIT MIT License
  59. * @link http://www.github.com/dbpatch/DbPatch
  60. * @since File available since Release 1.0.0
  61. */
  62. class DbPatch_Command_Remove extends DbPatch_Command_Abstract
  63. {
  64. /**
  65. * @return void
  66. */
  67. public function execute()
  68. {
  69. if ($this->console->issetOption('patch')) {
  70. $patchNumbers = explode(",", $this->console->getOptionValue('patch', null));
  71. $filteredPatchNumbers = array_filter($patchNumbers, 'is_numeric');
  72. if (empty($filteredPatchNumbers)) {
  73. $this->writer->error('no patch defined or patch isn\'t numeric');
  74. return;
  75. }
  76. $branch = $this->getBranch();
  77. foreach ($filteredPatchNumbers as $patchNumber) {
  78. $this->removePatch($patchNumber, $branch);
  79. }
  80. return;
  81. }
  82. $this->writer->error('No patch defined or patch isn\'t numeric');
  83. return;
  84. }
  85. /**
  86. * Remove patch from the changelog table
  87. *
  88. * @param int $patchNumber
  89. * @param string $branchName
  90. * @return void
  91. */
  92. protected function removePatch($patchNumber, $branchName)
  93. {
  94. $db = $this->getDb()->getAdapter();
  95. $branchSQL = "";
  96. if (!empty($branchName)) {
  97. $branchSQL = sprintf("AND branch = '%s'",
  98. $branchName);
  99. }
  100. $query = sprintf("SELECT branch
  101. FROM %s
  102. WHERE patch_number = %d {$branchSQL}",
  103. self::TABLE,
  104. $patchNumber);
  105. $stmt = $db->query($query);
  106. $patchRecords = $stmt->fetchAll();
  107. if (count($patchRecords) == 0) {
  108. $branchMsg = (empty($branchName) ? ""
  109. : "for branch '$branchName' ");
  110. $this->getWriter()->line("Patch $patchNumber not found {$branchMsg} in `" . self::TABLE . "` table");
  111. }
  112. else if (count($patchRecords) > 1) {
  113. // only happens when a specific patch number is used in different branches
  114. $branchArray = array();
  115. foreach ($patchRecords as $branch) {
  116. $branchArray[] = $branch['branch'];
  117. }
  118. $this->getWriter()->line("There's a patch '$patchNumber' in multiple branches: '" . implode("', '", $branchArray) . "'");
  119. $this->getWriter()->line("Specify the correct branch by adding: 'branch=" . implode("' or 'branch=", $branchArray) . "' to the command");
  120. }
  121. else {
  122. $branchMsg = (empty($branchName) ? ""
  123. : "from branch '$branchName' ");
  124. $query = sprintf("DELETE FROM %s
  125. WHERE patch_number = %d {$branchSQL}",
  126. self::TABLE,
  127. $patchNumber);
  128. $db->query($query);
  129. $this->getWriter()->line("Removed patch $patchNumber {$branchMsg}in the `" . self::TABLE . "` table");
  130. }
  131. }
  132. /**
  133. * @return void
  134. */
  135. public function showHelp($command = 'remove')
  136. {
  137. parent::showHelp($command);
  138. $writer = $this->getWriter();
  139. $writer->indent(2)->line('--patch=<int> One or more patchnumbers seperated by a comma to remove')
  140. ->line();
  141. }
  142. }