| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * ImportJobRepository.php | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * Copyright (c) 2017 thegrumpydictator@gmail.com | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * This file is part of Firefly III. | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * Firefly III is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							| 
									
										
										
										
											2017-12-17 14:44:05 +01:00
										 |  |  |  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Repositories\ImportJob; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-10 15:09:41 +02:00
										 |  |  | use Crypt; | 
					
						
							| 
									
										
										
										
											2016-09-17 09:50:40 +02:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  | use FireflyIII\Models\ImportJob; | 
					
						
							| 
									
										
										
										
											2018-01-05 17:29:42 +01:00
										 |  |  | use FireflyIII\Models\TransactionJournalMeta; | 
					
						
							| 
									
										
										
										
											2017-06-10 15:09:41 +02:00
										 |  |  | use FireflyIII\Repositories\User\UserRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  | use FireflyIII\User; | 
					
						
							|  |  |  | use Illuminate\Support\Str; | 
					
						
							| 
									
										
										
										
											2017-06-10 15:09:41 +02:00
										 |  |  | use Log; | 
					
						
							|  |  |  | use SplFileObject; | 
					
						
							|  |  |  | use Storage; | 
					
						
							|  |  |  | use Symfony\Component\HttpFoundation\File\UploadedFile; | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class ImportJobRepository. | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | class ImportJobRepository implements ImportJobRepositoryInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** @var User */ | 
					
						
							|  |  |  |     private $user; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 16:49:32 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param ImportJob $job | 
					
						
							|  |  |  |      * @param int       $index | 
					
						
							|  |  |  |      * @param string    $error | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return ImportJob | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function addError(ImportJob $job, int $index, string $error): ImportJob | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-01-10 18:18:49 +01:00
										 |  |  |         $extended                     = $this->getExtendedStatus($job); | 
					
						
							|  |  |  |         $extended['errors'][$index][] = $error; | 
					
						
							| 
									
										
										
										
											2018-01-10 16:49:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 18:18:49 +01:00
										 |  |  |         return $this->setExtendedStatus($job, $extended); | 
					
						
							| 
									
										
										
										
											2018-01-10 16:49:32 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-05 17:29:42 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param ImportJob $job | 
					
						
							|  |  |  |      * @param int       $steps | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return ImportJob | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function addStepsDone(ImportJob $job, int $steps = 1): ImportJob | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-01-10 18:18:49 +01:00
										 |  |  |         $status         = $this->getExtendedStatus($job); | 
					
						
							|  |  |  |         $status['done'] += $steps; | 
					
						
							|  |  |  |         Log::debug(sprintf('Add %d to steps done for job "%s" making steps done %d', $steps, $job->key, $status['done'])); | 
					
						
							| 
									
										
										
										
											2018-01-05 17:29:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 19:06:27 +01:00
										 |  |  |         return $this->setExtendedStatus($job, $status); | 
					
						
							| 
									
										
										
										
											2018-01-05 17:29:42 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Return number of imported rows with this hash value. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $hash | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function countByHash(string $hash): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $json  = json_encode($hash); | 
					
						
							|  |  |  |         $count = TransactionJournalMeta::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'journal_meta.transaction_journal_id') | 
					
						
							|  |  |  |                                        ->where('data', $json) | 
					
						
							|  |  |  |                                        ->where('name', 'importHash') | 
					
						
							|  |  |  |                                        ->count(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return intval($count); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-12-16 08:03:35 +01:00
										 |  |  |      * @param string $type | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return ImportJob | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-10-09 07:58:27 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-12-16 08:03:35 +01:00
										 |  |  |     public function create(string $type): ImportJob | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-12-16 08:03:35 +01:00
										 |  |  |         $count = 0; | 
					
						
							|  |  |  |         $type  = strtolower($type); | 
					
						
							| 
									
										
										
										
											2016-09-17 09:50:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  |         while ($count < 30) { | 
					
						
							|  |  |  |             $key      = Str::random(12); | 
					
						
							|  |  |  |             $existing = $this->findByKey($key); | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |             if (null === $existing->id) { | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  |                 $importJob = new ImportJob; | 
					
						
							|  |  |  |                 $importJob->user()->associate($this->user); | 
					
						
							| 
									
										
										
										
											2017-12-16 08:03:35 +01:00
										 |  |  |                 $importJob->file_type       = $type; | 
					
						
							| 
									
										
										
										
											2016-08-13 21:51:01 +02:00
										 |  |  |                 $importJob->key             = Str::random(12); | 
					
						
							| 
									
										
										
										
											2017-06-10 15:09:41 +02:00
										 |  |  |                 $importJob->status          = 'new'; | 
					
						
							| 
									
										
										
										
											2018-01-03 07:35:29 +01:00
										 |  |  |                 $importJob->configuration   = []; | 
					
						
							| 
									
										
										
										
											2016-08-14 11:31:09 +02:00
										 |  |  |                 $importJob->extended_status = [ | 
					
						
							| 
									
										
										
										
											2017-06-24 06:57:24 +02:00
										 |  |  |                     'steps'  => 0, | 
					
						
							|  |  |  |                     'done'   => 0, | 
					
						
							|  |  |  |                     'tag'    => 0, | 
					
						
							|  |  |  |                     'errors' => [], | 
					
						
							| 
									
										
										
										
											2016-08-14 11:31:09 +02:00
										 |  |  |                 ]; | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  |                 $importJob->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // breaks the loop:
 | 
					
						
							|  |  |  |                 return $importJob; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |             ++$count; | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-16 08:03:35 +01:00
										 |  |  |         throw new FireflyException('Could not create an import job with a unique key after 30 tries.'); | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string $key | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return ImportJob | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function findByKey(string $key): ImportJob | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-01-04 18:34:51 +01:00
										 |  |  |         /** @var ImportJob $result */ | 
					
						
							| 
									
										
										
										
											2016-08-11 10:21:32 +02:00
										 |  |  |         $result = $this->user->importJobs()->where('key', $key)->first(['import_jobs.*']); | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (null === $result) { | 
					
						
							| 
									
										
										
										
											2016-06-10 21:00:00 +02:00
										 |  |  |             return new ImportJob; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-01-30 16:46:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 18:34:51 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Return configuration of job. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param ImportJob $job | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getConfiguration(ImportJob $job): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $config = $job->configuration; | 
					
						
							|  |  |  |         if (is_array($config)) { | 
					
						
							|  |  |  |             return $config; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return []; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-05 17:29:42 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Return extended status of job. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param ImportJob $job | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getExtendedStatus(ImportJob $job): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $status = $job->extended_status; | 
					
						
							|  |  |  |         if (is_array($status)) { | 
					
						
							|  |  |  |             return $status; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return []; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 18:18:49 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param ImportJob $job | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getStatus(ImportJob $job): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $job->status; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-10 15:09:41 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param ImportJob    $job | 
					
						
							|  |  |  |      * @param UploadedFile $file | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function processConfiguration(ImportJob $job, UploadedFile $file): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var UserRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(UserRepositoryInterface::class); | 
					
						
							|  |  |  |         // demo user's configuration upload is ignored completely.
 | 
					
						
							| 
									
										
										
										
											2017-06-14 20:13:19 +02:00
										 |  |  |         if (!$repository->hasRole($this->user, 'demo')) { | 
					
						
							| 
									
										
										
										
											2017-06-10 15:09:41 +02:00
										 |  |  |             Log::debug( | 
					
						
							| 
									
										
										
										
											2017-11-15 10:52:29 +01:00
										 |  |  |                 'Uploaded configuration file', | 
					
						
							|  |  |  |                 ['name' => $file->getClientOriginalName(), 'size' => $file->getSize(), 'mime' => $file->getClientMimeType()] | 
					
						
							| 
									
										
										
										
											2017-06-10 15:09:41 +02:00
										 |  |  |             ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $configFileObject = new SplFileObject($file->getRealPath()); | 
					
						
							|  |  |  |             $configRaw        = $configFileObject->fread($configFileObject->getSize()); | 
					
						
							|  |  |  |             $configuration    = json_decode($configRaw, true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |             if (null !== $configuration && is_array($configuration)) { | 
					
						
							| 
									
										
										
										
											2017-06-10 15:09:41 +02:00
										 |  |  |                 Log::debug('Found configuration', $configuration); | 
					
						
							|  |  |  |                 $this->setConfiguration($job, $configuration); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-12-16 17:19:10 +01:00
										 |  |  |      * @param ImportJob         $job | 
					
						
							|  |  |  |      * @param null|UploadedFile $file | 
					
						
							| 
									
										
										
										
											2017-06-10 15:09:41 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-12-16 17:19:10 +01:00
										 |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2017-12-22 18:32:43 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-12-16 17:19:10 +01:00
										 |  |  |      * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException | 
					
						
							| 
									
										
										
										
											2017-06-10 15:09:41 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-12-16 17:19:10 +01:00
										 |  |  |     public function processFile(ImportJob $job, ?UploadedFile $file): bool | 
					
						
							| 
									
										
										
										
											2017-06-10 15:09:41 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-12-16 17:19:10 +01:00
										 |  |  |         if (is_null($file)) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-06-10 15:09:41 +02:00
										 |  |  |         /** @var UserRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository       = app(UserRepositoryInterface::class); | 
					
						
							|  |  |  |         $newName          = sprintf('%s.upload', $job->key); | 
					
						
							|  |  |  |         $uploaded         = new SplFileObject($file->getRealPath()); | 
					
						
							| 
									
										
										
										
											2017-12-16 17:19:10 +01:00
										 |  |  |         $content          = trim($uploaded->fread($uploaded->getSize())); | 
					
						
							| 
									
										
										
										
											2017-06-10 15:09:41 +02:00
										 |  |  |         $contentEncrypted = Crypt::encrypt($content); | 
					
						
							|  |  |  |         $disk             = Storage::disk('upload'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // user is demo user, replace upload with prepared file.
 | 
					
						
							|  |  |  |         if ($repository->hasRole($this->user, 'demo')) { | 
					
						
							|  |  |  |             $stubsDisk        = Storage::disk('stubs'); | 
					
						
							|  |  |  |             $content          = $stubsDisk->get('demo-import.csv'); | 
					
						
							|  |  |  |             $contentEncrypted = Crypt::encrypt($content); | 
					
						
							|  |  |  |             $disk->put($newName, $contentEncrypted); | 
					
						
							|  |  |  |             Log::debug('Replaced upload with demo file.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // also set up prepared configuration.
 | 
					
						
							|  |  |  |             $configuration = json_decode($stubsDisk->get('demo-configuration.json'), true); | 
					
						
							|  |  |  |             $this->setConfiguration($job, $configuration); | 
					
						
							|  |  |  |             Log::debug('Set configuration for demo user', $configuration); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$repository->hasRole($this->user, 'demo')) { | 
					
						
							|  |  |  |             // user is not demo, process original upload:
 | 
					
						
							|  |  |  |             $disk->put($newName, $contentEncrypted); | 
					
						
							|  |  |  |             Log::debug('Uploaded file', ['name' => $file->getClientOriginalName(), 'size' => $file->getSize(), 'mime' => $file->getClientMimeType()]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-19 17:54:21 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param ImportJob $job | 
					
						
							|  |  |  |      * @param array     $configuration | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return ImportJob | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setConfiguration(ImportJob $job, array $configuration): ImportJob | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-12-16 17:19:10 +01:00
										 |  |  |         Log::debug(sprintf('Incoming config for job "%s" is: ', $job->key), $configuration); | 
					
						
							|  |  |  |         $currentConfig      = $job->configuration; | 
					
						
							|  |  |  |         $newConfig          = array_merge($currentConfig, $configuration); | 
					
						
							|  |  |  |         $job->configuration = $newConfig; | 
					
						
							| 
									
										
										
										
											2017-03-19 17:54:21 +01:00
										 |  |  |         $job->save(); | 
					
						
							| 
									
										
										
										
											2017-12-16 17:19:10 +01:00
										 |  |  |         Log::debug(sprintf('Set config of job "%s" to: ', $job->key), $newConfig); | 
					
						
							| 
									
										
										
										
											2017-03-19 17:54:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $job; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-05 17:29:42 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param ImportJob $job | 
					
						
							|  |  |  |      * @param array     $array | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return ImportJob | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setExtendedStatus(ImportJob $job, array $array): ImportJob | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-01-06 20:26:21 +01:00
										 |  |  |         // remove 'errors' because it gets larger and larger and larger...
 | 
					
						
							|  |  |  |         $display = $array; | 
					
						
							|  |  |  |         unset($display['errors']); | 
					
						
							|  |  |  |         Log::debug(sprintf('Incoming extended status for job "%s" is (except errors): ', $job->key), $display); | 
					
						
							| 
									
										
										
										
											2018-01-05 17:29:42 +01:00
										 |  |  |         $currentStatus        = $job->extended_status; | 
					
						
							|  |  |  |         $newStatus            = array_merge($currentStatus, $array); | 
					
						
							|  |  |  |         $job->extended_status = $newStatus; | 
					
						
							|  |  |  |         $job->save(); | 
					
						
							| 
									
										
										
										
											2018-01-06 20:26:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // remove 'errors' because it gets larger and larger and larger...
 | 
					
						
							|  |  |  |         unset($newStatus['errors']); | 
					
						
							|  |  |  |         Log::debug(sprintf('Set extended status of job "%s" to (except errors): ', $job->key), $newStatus); | 
					
						
							| 
									
										
										
										
											2018-01-05 17:29:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $job; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-19 17:54:21 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param ImportJob $job | 
					
						
							|  |  |  |      * @param string    $status | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return ImportJob | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-01-10 16:49:32 +01:00
										 |  |  |     public function setStatus(ImportJob $job, string $status): ImportJob | 
					
						
							| 
									
										
										
										
											2017-03-19 17:54:21 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $job->status = $status; | 
					
						
							|  |  |  |         $job->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $job; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-04 19:33:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param ImportJob $job | 
					
						
							| 
									
										
										
										
											2018-01-10 16:49:32 +01:00
										 |  |  |      * @param int       $count | 
					
						
							| 
									
										
										
										
											2018-01-04 19:33:16 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-01-10 16:49:32 +01:00
										 |  |  |      * @return ImportJob | 
					
						
							| 
									
										
										
										
											2018-01-04 19:33:16 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-01-10 16:49:32 +01:00
										 |  |  |     public function setStepsDone(ImportJob $job, int $steps): ImportJob | 
					
						
							| 
									
										
										
										
											2018-01-04 19:33:16 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-01-10 16:49:32 +01:00
										 |  |  |         $job->setStepsDone($steps); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $job; | 
					
						
							| 
									
										
										
										
											2018-01-04 19:33:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-08 20:20:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param ImportJob $job | 
					
						
							|  |  |  |      * @param int       $count | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return ImportJob | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-01-10 16:49:32 +01:00
										 |  |  |     public function setTotalSteps(ImportJob $job, int $count): ImportJob | 
					
						
							| 
									
										
										
										
											2018-01-08 20:20:45 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-01-10 16:49:32 +01:00
										 |  |  |         $job->setTotalSteps($count); | 
					
						
							| 
									
										
										
										
											2018-01-08 20:20:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $job; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 16:49:32 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param User $user | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setUser(User $user) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->user = $user; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-08 20:20:45 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param ImportJob $job | 
					
						
							| 
									
										
										
										
											2018-01-10 16:49:32 +01:00
										 |  |  |      * @param string    $status | 
					
						
							| 
									
										
										
										
											2018-01-08 20:20:45 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return ImportJob | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-01-10 16:49:32 +01:00
										 |  |  |     public function updateStatus(ImportJob $job, string $status): ImportJob | 
					
						
							| 
									
										
										
										
											2018-01-08 20:20:45 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-01-10 16:49:32 +01:00
										 |  |  |         $job->status = $status; | 
					
						
							|  |  |  |         $job->save(); | 
					
						
							| 
									
										
										
										
											2018-01-08 20:20:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $job; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-10 16:49:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Return import file content. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param ImportJob $job | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function uploadFileContents(ImportJob $job): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $job->uploadFileContents(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-12 15:10:03 +02:00
										 |  |  | } |