protected function _fakeWatcherOnParseError($e, $watcher, $type, $action, $errors)
 {
     $txId = uniqid('parser');
     if (!isset($watcher)) {
         $watcher = $this->_stockSrv->createFileWatcher();
         $watcher->entityIds = array($txId);
         $watcher->params->type = $type;
         $watcher->params->action = $action;
         $watcher->save();
     } else {
         WatcherService::getInstance()->pushEntityId($watcher, $txId);
     }
     $event = new EventModel();
     $event->entityId = $txId;
     $event->entityType = 'transaction';
     $event->namespace = 'connectivity';
     $event->created = time();
     $event->modified = time();
     $event->pushEventData = true;
     $eventData = array();
     if ($errors || $e->getErrorMessages()) {
         $errors = array_merge($errors ?: array(), $e->getErrorMessages() ?: array());
     }
     $eventData['hasFailures'] = true;
     if ($e instanceof InvalidFileContentException) {
         $eventData['simParsed'] = $e->getSimParsed();
     }
     if (!empty($errors) && is_array($errors)) {
         require_once APPLICATION_PATH . '/modules/default/controllers/ErrorController.php';
         foreach ($errors as $errMess) {
             if ($errMess instanceof ErrorModel) {
                 $errMess->code = ErrorController::finishErrorCode($errMess->code);
             }
         }
         $eventData['message'] = array('failed' => $errors);
     }
     $event->eventData = $eventData;
     $event->forceFinish = true;
     $compressor = new ErrorModelCompressEvent();
     $compressor->compress($event);
     WatcherService::getInstance()->publishEvent($event);
     return $watcher;
 }
 public function filesAction()
 {
     if (!$this->getRequest()->isPost()) {
         throw new AppEx\ForbiddenException("Files action must be a post request.");
     }
     $front = Zend_Controller_Front::getInstance();
     $front->registerPlugin(new \Tid_Zend_Controller_Plugin_UploadMax());
     try {
         $upload = new Zend_File_Transfer('App_File_Transfer_Adapter_HttpMultipartMixed', false, array('ignoreNoFile' => true));
     } catch (Zend_File_Transfer_Exception $e) {
         throw new AppEx\InvalidArgumentException($e->getMessage());
     }
     $upload->addValidator('Count', true, array('min' => 1, 'max' => 1))->addValidator('Extension', true, array('xml', 'csv', 'txt'))->addValidator('MimeType', true, array('application/xml', 'text/plain', 'headerCheck' => true));
     if ($upload->isValid()) {
         if ($upload->receive()) {
             try {
                 $fileinfo = current($upload->getFileInfo());
                 $filename = $fileinfo['tmp_name'];
                 // Attempt to parse data from file
                 $parseResult = $this->_stockSrv->getData($filename, $upload->getMimeType());
                 $data = $parseResult['data'];
                 $errors = $parseResult['errors'];
                 if (!empty($errors) && is_array($errors)) {
                     foreach ($errors as $errMess) {
                         require_once APPLICATION_PATH . '/modules/default/controllers/ErrorController.php';
                         $errMess->code = ErrorController::finishErrorCode($errMess->code);
                     }
                 }
                 $method = 'create' . ucfirst($data['_type']);
                 if (!empty($data['_type']) && is_callable(array($this->_stockSrv, $method))) {
                     // Check permissions according to the data type
                     $dumbSim = new Application\Model\SimModel();
                     $this->_helper->allowed($data['_perm'], $dumbSim);
                     try {
                         $watcher = $this->_stockSrv->{$method}($parseResult);
                     } catch (AppEx\GlobalServiceException $ex) {
                         $ex->addErrorMessages($errors);
                         throw $ex;
                     }
                     $txId = uniqid('parser');
                     WatcherService::getInstance()->pushEntityId($watcher, $txId);
                     $event = new EventModel();
                     $event->entityId = $txId;
                     $event->entityType = 'transaction';
                     $event->namespace = 'connectivity';
                     $event->eventData = $errors;
                     $event->created = time();
                     $event->forceFinish = true;
                     WatcherService::getInstance()->publishEvent($event);
                     //                         WatcherService::getInstance()->setStatus($watcher->id, WatcherModel::STATUS_FINISHED);
                     $errors_ex = $this->_loadErrorsFromWatcher($watcher);
                     if (!empty($errors_ex)) {
                         $errors = Zend_Json::encode($errors_ex);
                         App::log()->warn("Error on file upload in stock:\n" . $errors);
                         throw new AppEx\StockParserException("Some errors uploading file to stock.", array('errorMessages' => $errors_ex));
                     }
                 } else {
                     throw new AppEx\UnexpectedException('Unknown data type (' . $data['_type'] . ')');
                 }
             } catch (PermissionException $e) {
                 throw $e;
             } catch (StockParserException $e) {
                 throw $e;
             } catch (GlobalServiceException $e) {
                 $txId = uniqid('parser');
                 if (!isset($watcher)) {
                     $watcher = $this->_stockSrv->createFileWatcher();
                     $watcher->entityIds = array($txId);
                     $watcher->params->type = 'sim';
                     $watcher->params->action = 'stockUpload';
                     $watcher->save();
                 } else {
                     WatcherService::getInstance()->pushEntityId($watcher, $txId);
                 }
                 $event = new EventModel();
                 $event->entityId = $txId;
                 $event->entityType = 'transaction';
                 $event->namespace = 'connectivity';
                 $event->created = time();
                 $event->modified = time();
                 $event->pushEventData = true;
                 $eventData = array();
                 $errors = $e->getErrorMessages();
                 $eventData['hasFailures'] = true;
                 if (!empty($errors) && is_array($errors)) {
                     require_once APPLICATION_PATH . '/modules/default/controllers/ErrorController.php';
                     foreach ($errors as $errMess) {
                         if ($errMess instanceof ErrorModel) {
                             $errMess->code = ErrorController::finishErrorCode($errMess->code);
                         }
                     }
                     $eventData['message'] = array('failed' => $errors);
                 }
                 $event->eventData = $eventData;
                 $event->forceFinish = true;
                 $compressor = new ErrorModelCompressEvent();
                 $compressor->compress($event);
                 WatcherService::getInstance()->publishEvent($event);
                 //                     WatcherService::getInstance()->setStatus($watcher->id, WatcherModel::STATUS_FINISHED);
                 $errors = $this->_loadErrorsFromWatcher($watcher);
                 if (!empty($errors)) {
                     App::log()->warn("Error on file upload in stock:\n" . Zend_Json::encode($errors));
                     throw new AppEx\StockParserException("Some errors uploading file to stock.", array('errorMessages' => $errors));
                 }
             }
         } else {
             throw new AppEx\InvalidArgumentException('Could not receive file');
         }
     } else {
         throw new AppEx\InvalidArgumentException('Invalid file: ' . implode(', ', $upload->getMessages()));
     }
 }