|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProcessMaker\Jobs; |
| 4 | + |
| 5 | +use Cache; |
| 6 | +use Illuminate\Bus\Queueable; |
| 7 | +use ProcessMaker\Models\Process; |
| 8 | +use Illuminate\Queue\SerializesModels; |
| 9 | +use Illuminate\Queue\InteractsWithQueue; |
| 10 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 11 | +use Illuminate\Foundation\Bus\Dispatchable; |
| 12 | + |
| 13 | +class ExportProcess implements ShouldQueue |
| 14 | +{ |
| 15 | + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
| 16 | + |
| 17 | + private $process, $filePath, $file; |
| 18 | + |
| 19 | + private $package = []; |
| 20 | + |
| 21 | + /** |
| 22 | + * Create a new job instance. |
| 23 | + * |
| 24 | + * @return void |
| 25 | + */ |
| 26 | + public function __construct(Process $process, $filePath = null) |
| 27 | + { |
| 28 | + $this->process = $process; |
| 29 | + $this->filePath = $filePath; |
| 30 | + } |
| 31 | + |
| 32 | + private function packageFile() |
| 33 | + { |
| 34 | + $this->package['type'] = 'process_package'; |
| 35 | + $this->package['version'] = '0.1'; |
| 36 | + $this->package['process'] = $this->process->toArray(); |
| 37 | + $this->package['process']['bpmn'] = $this->process->bpmn; |
| 38 | + } |
| 39 | + |
| 40 | + private function encodeFile() |
| 41 | + { |
| 42 | + $this->file = json_encode($this->package); |
| 43 | + $this->file = base64_encode($this->file); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Save the file . |
| 48 | + * |
| 49 | + * @return void |
| 50 | + */ |
| 51 | + private function saveFile() |
| 52 | + { |
| 53 | + $bytes = file_put_contents($this->filePath, $this->file); |
| 54 | + if ($bytes !== false) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private function cacheFile() |
| 60 | + { |
| 61 | + $key = sha1($this->file); |
| 62 | + $value = $this->file; |
| 63 | + $expiresAt = now()->addHours(1); |
| 64 | + Cache::put($key, $value, $expiresAt); |
| 65 | + |
| 66 | + return $key; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Execute the job. |
| 71 | + * |
| 72 | + * @return void |
| 73 | + */ |
| 74 | + public function handle() |
| 75 | + { |
| 76 | + $this->packageFile(); |
| 77 | + $this->encodeFile(); |
| 78 | + |
| 79 | + if ($this->filePath) { |
| 80 | + return $this->saveFile(); |
| 81 | + } else { |
| 82 | + return $this->cacheFile(); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments