| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * nextCloud - ocr | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * This file is licensed under the Affero General Public License version 3 or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * later. See the COPYING file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Janis Koehr <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @copyright Janis Koehr 2016 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace OCA\Ocr\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use OCA\Ocr\Service\OcrService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use OCA\Ocr\Service\ServiceException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use OCP\AppFramework\Db\DoesNotExistException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use OCP\AppFramework\Db\MultipleObjectsReturnedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * Class CompleteOCR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @package OCA\Ocr\Command | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | class CompleteOCR extends Command { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 	 * @var OcrService | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  | 	private $ocrService; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 	 * UpdateStatus constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	public function __construct(OcrService $ocrService) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		$this->ocrService = $ocrService; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 	 * Required Arguments configuration | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	protected function configure() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$this->setName('ocr:complete') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			->addArgument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 				'status-id', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 				InputArgument::REQUIRED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 				'status id, integer' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			->addArgument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 				'failed', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 				InputArgument::REQUIRED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 				'failed, boolean' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			->setDescription('Console API for completion of the ocr processing of a file'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 	 * Executes the complete function of the OCRService | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 	 * @param InputInterface $input | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 	 * @param OutputInterface $output | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	protected function execute(InputInterface $input, OutputInterface $output) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$statusId = $input->getArgument('status-id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$failed = $input->getArgument('failed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 			if ($failed === 'false') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 				$failed = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			}elseif ($failed === 'true') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 				$failed = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 				throw new ServiceException('Wrong Arguments.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			$this->ocrService->complete($statusId, $failed); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		} catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			if ($e instanceof MultipleObjectsReturnedException || $e instanceof DoesNotExistException) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 				$output->writeln('<error>Could not complete ocr for status id ' . $statusId . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 					': ' . $e->getMessage() . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 					'</error> '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 				$output->writeln('<error>Unexpected error for status id ' . $statusId . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 					': ' . $e->getMessage() . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 					'</error> '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 91 |  |  | } |