techdivision /
import-cli
| 1 | <?php |
||||
| 2 | |||||
| 3 | /** |
||||
| 4 | * TechDivision\Import\Cli\Plugins\DebugCreatePlugin |
||||
| 5 | * |
||||
| 6 | * PHP version 7 |
||||
| 7 | * |
||||
| 8 | * @author Tim Wagner <[email protected]> |
||||
| 9 | * @copyright 2020 TechDivision GmbH <[email protected]> |
||||
| 10 | * @license https://opensource.org/licenses/MIT |
||||
| 11 | * @link https://github.com/techdivision/import-cli |
||||
| 12 | * @link http://www.techdivision.com |
||||
| 13 | */ |
||||
| 14 | |||||
| 15 | namespace TechDivision\Import\Cli\Plugins; |
||||
| 16 | |||||
| 17 | use TechDivision\Import\Utils\RegistryKeys; |
||||
| 18 | use TechDivision\Import\ApplicationInterface; |
||||
| 19 | use TechDivision\Import\Utils\DebugUtilInterface; |
||||
| 20 | use Symfony\Component\Console\Question\ChoiceQuestion; |
||||
| 21 | use TechDivision\Import\Utils\InputOptionKeysInterface; |
||||
| 22 | |||||
| 23 | /** |
||||
| 24 | * Plugin that creates and sends a debug report via email. |
||||
| 25 | * |
||||
| 26 | * @author Tim Wagner <[email protected]> |
||||
| 27 | * @copyright 2020 TechDivision GmbH <[email protected]> |
||||
| 28 | * @license https://opensource.org/licenses/MIT |
||||
| 29 | * @link https://github.com/techdivision/import |
||||
| 30 | * @link http://www.techdivision.com |
||||
| 31 | */ |
||||
| 32 | class DebugCreatePlugin extends AbstractConsolePlugin |
||||
| 33 | { |
||||
| 34 | |||||
| 35 | /** |
||||
| 36 | * The debug util instance. |
||||
| 37 | * |
||||
| 38 | * @var \TechDivision\Import\Utils\DebugUtilInterface |
||||
| 39 | */ |
||||
| 40 | private $debugUtil; |
||||
| 41 | |||||
| 42 | /** |
||||
| 43 | * The constructor to initialize the plugin with. |
||||
| 44 | * |
||||
| 45 | * @param \TechDivision\Import\ApplicationInterface $application The application instance |
||||
| 46 | * @param \TechDivision\Import\Utils\DebugUtilInterface $debugUtil The debug util instance |
||||
| 47 | */ |
||||
| 48 | public function __construct(ApplicationInterface $application, DebugUtilInterface $debugUtil) |
||||
| 49 | { |
||||
| 50 | |||||
| 51 | // set the debug utility |
||||
| 52 | $this->debugUtil = $debugUtil; |
||||
| 53 | |||||
| 54 | // pass the application to the parent constructor |
||||
| 55 | parent::__construct($application); |
||||
| 56 | } |
||||
| 57 | |||||
| 58 | |||||
| 59 | /** |
||||
| 60 | * Return's the debug util instance. |
||||
| 61 | * |
||||
| 62 | * @return \TechDivision\Import\Utils\DebugUtilInterface The debug util instance |
||||
| 63 | */ |
||||
| 64 | private function getDebugUtil() : DebugUtilInterface |
||||
| 65 | { |
||||
| 66 | return $this->debugUtil; |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | /** |
||||
| 70 | * Process the plugin functionality. |
||||
| 71 | * |
||||
| 72 | * @return void |
||||
| 73 | * @throws \InvalidArgumentException Is thrown if either the directory nor a artefact for the given serial is available |
||||
| 74 | */ |
||||
| 75 | public function process() |
||||
| 76 | { |
||||
| 77 | if ($this->getConfiguration()->isConfigOutput()) { |
||||
| 78 | $configurationFiles = $this->getConfigurationFiles(); |
||||
| 79 | $this->getSystemLogger()->info( |
||||
| 80 | print_r($configurationFiles, true) |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 81 | ); |
||||
| 82 | return; |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | // load the actual status |
||||
| 86 | $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
||||
| 87 | |||||
| 88 | // query whether or not the configured source directory is available |
||||
| 89 | if (isset($status[RegistryKeys::SOURCE_DIRECTORY])) { |
||||
| 90 | $sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY]; |
||||
| 91 | } else { |
||||
| 92 | throw new \Exception('Source directory is not available!'); |
||||
| 93 | } |
||||
| 94 | |||||
| 95 | // try to load the archive directory |
||||
| 96 | $archiveDir = $this->getConfiguration()->getArchiveDir(); |
||||
| 97 | |||||
| 98 | // try to initialize a default archive directory by concatenating 'archive' to the target directory |
||||
| 99 | if ($archiveDir === null) { |
||||
|
0 ignored issues
–
show
|
|||||
| 100 | $archiveDir = sprintf('var/import_history'); |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | // retrieve the question helper |
||||
| 104 | $questionHelper = $this->getHelper('question'); |
||||
| 105 | |||||
| 106 | // initialize the array for the available serials |
||||
| 107 | $availableSerials = array(); |
||||
| 108 | |||||
| 109 | // load the directories from the actual working directory (broken imports, etc.) |
||||
| 110 | foreach (glob(sprintf('%s/*', $sourceDir), GLOB_ONLYDIR) as $possibleImportDir) { |
||||
| 111 | $availableSerials[basename($possibleImportDir)] = filemtime($possibleImportDir); |
||||
| 112 | } |
||||
| 113 | |||||
| 114 | // load the ZIP artefacts from the archive directory |
||||
| 115 | foreach (glob(sprintf('%s/*.zip', $archiveDir)) as $possibleArtefact) { |
||||
| 116 | $availableSerials[ basename($possibleArtefact, '.zip')] = filemtime($possibleArtefact); |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | // sort the available serials by modification time |
||||
| 120 | uasort($availableSerials, function ($a, $b) { |
||||
| 121 | // return zero, if the passed values are equal |
||||
| 122 | if ($a == $b) { |
||||
| 123 | return 0; |
||||
| 124 | } |
||||
| 125 | // otherwise return -1 or 1 |
||||
| 126 | return ($a > $b) ? -1 : 1; |
||||
| 127 | }); |
||||
| 128 | |||||
| 129 | // finally create the array with the available serials to render on the console |
||||
| 130 | $availableSerials = array_slice(array_keys($availableSerials), 0, $this->getInput()->getOption(InputOptionKeysInterface::RENDER_DEBUG_SERIALS)); |
||||
| 131 | |||||
| 132 | // this is, when the import:debug send command has been invoked |
||||
| 133 | // WITHOUT the --serial=<UUID> parameter or an invalid serial |
||||
| 134 | if (!in_array($serial = $this->getSerial(), $availableSerials, true)) { |
||||
| 135 | // create the question instance to choose the serial |
||||
| 136 | $chooseSerialQuestion = new ChoiceQuestion('Please select the serial to create the debug artefact for', $availableSerials); |
||||
| 137 | $chooseSerialQuestion->setErrorMessage('Selected serial "%s" is invalid.'); |
||||
| 138 | |||||
| 139 | // abort the operation if the user does not confirm with 'y' or enter |
||||
| 140 | if (!$serial = $questionHelper->ask($this->getInput(), $this->getOutput(), $chooseSerialQuestion)) { |
||||
|
0 ignored issues
–
show
The method
ask() does not exist on Symfony\Component\Console\Helper\Helper. It seems like you code against a sub-type of Symfony\Component\Console\Helper\Helper such as Symfony\Component\Console\Helper\QuestionHelper.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 141 | $this->getOutput()->writeln('<info>Aborting operation - debug report has NOT been sent.</info>'); |
||||
| 142 | return; |
||||
| 143 | } |
||||
| 144 | } |
||||
| 145 | |||||
| 146 | // update the registry with the (probably new) serial and the source directory |
||||
| 147 | $this->getRegistryProcessor()->mergeAttributesRecursive( |
||||
| 148 | RegistryKeys::STATUS, |
||||
| 149 | array( |
||||
| 150 | RegistryKeys::DEBUG_SERIAL => $serial, |
||||
| 151 | RegistryKeys::SOURCE_DIRECTORY => sprintf('%s/%s', $sourceDir, $serial) |
||||
| 152 | ) |
||||
| 153 | ); |
||||
| 154 | |||||
| 155 | // prepare the debug dump |
||||
| 156 | $this->getDebugUtil()->extractArchive($serial); |
||||
| 157 | $this->getDebugUtil()->prepareDump($serial); |
||||
| 158 | $dumpFilename = $this->getDebugUtil()->createDump($serial); |
||||
| 159 | |||||
| 160 | // write a success message to the console |
||||
| 161 | $this->getOutput()->writeln(sprintf('<info>Successfully created debug dump "%s"</info>', $dumpFilename)); |
||||
| 162 | } |
||||
| 163 | |||||
| 164 | /** |
||||
| 165 | * @return array |
||||
| 166 | */ |
||||
| 167 | public function getConfigurationFiles() |
||||
| 168 | { |
||||
| 169 | return $this->getConfiguration()->getConfigurationFiles(); |
||||
| 170 | } |
||||
| 171 | } |
||||
| 172 |