<commit_before><?php

namespace App\Iep\Legacy\Handlers\Events;

use Queue;
use \Carbon\Carbon;
use App\Iep\Legacy\Pdf;
use App\Iep\Legacy\Events\PdfWasFilled;
use App\Iep\Legacy\Commands\RemoveFile;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class ZipPdfFiles
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
	 * Handle the event.
	 *
	 * @param  PdfWasFilled  $event
	 * @return void
	 */
	public function handle(PdfWasFilled $event)
	{
		$this->queueFiles($event->files);

		if (count($event->files) > 1) {
			if ($event->fileOption == 'zip' && PHP_OS !== 'WINNT') {
				$outfile = $event->concatName . '-' . str_random(4) . '.zip';
				$infile = '';
				foreach ($event->files as $file) {
					$infile .= ' ' . escapeshellarg($file);
				}

				exec("zip $outfile $infile");
			} else {
				foreach ($event->files as $index => $file) {
					if ($index == 0) {
						$pdf = new Pdf($file);
					} else {
						$pdf->addFile($file);
					}
				}

				$outfile = $event->concatName . '-' . str_random(4) . '.pdf';

				$pdf->saveAs($outfile);
			}

            $this->queueFiles([$outfile]);
			return $outfile;
		}

		return $event->files[0];
	}

    /**
	* Queue the file for deleting at a later time
	*
	* @param string $file
	* @return void
	*/
	protected function queueFiles($files)
	{
		$date = Carbon::now()->addMinutes(15);
		Queue::later($date, new RemoveFile($files));
	}
}<commit_msg>fixed some bugs where data was getting passed and some variable name changes didn't get changed everywhere. now will zip dispite the OS using ZipArchive, no longer using exec functions to do zipping. enhanced by pulling out concatenating and zipping into their own functions so a little clearer and cleaner.<commit_after><?php

namespace App\Iep\Legacy\Handlers\Events;

use \Carbon\Carbon;
use Queue, ZipArchive;
use App\Iep\Legacy\Pdf;
use App\Iep\Legacy\Events\PdfWasFilled;
use App\Iep\Legacy\Commands\RemoveFile;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class ZipPdfFiles
{
    public $outFile;
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
	 * Handle the event.
	 *
	 * @param  PdfWasFilled  $event
	 * @return void
	 */
	public function handle(PdfWasFilled $event)
	{
		$this->queueFiles($event->files);

		if (count($event->files) > 1) {
			if ($event->fileOption == 'zip') {
                $this->outFile = $event->concatName . '-' . str_random(4) . '.zip';
                $this->createZip($event->files);
			} else {
                $this->outFile = $event->concatName . '-' . str_random(4) . '.pdf';
                $this->concatPdfs($event->files);
			}

            $this->queueFiles([$this->outFile]);
			return $this->outFile;
		}

		return $event->files[0];
	}

    /**
	* Queue the file for deleting at a later time
	*
	* @param string $file
	* @return void
	*/
	protected function queueFiles($files)
	{
        $queueDriver = config('queue.default');

        if (!is_null($queueDriver) && $queueDriver !== 'sync') {
            $date = Carbon::now()->addMinutes(15);
    		Queue::later($date, new RemoveFile($files));
        }
	}

    /**
     * create zip file of all pdf files
     *
     * @param array $files
     */
     protected function createZip($files) {
         $zip = new ZipArchive();
         $zip->open($this->outFile, ZIPARCHIVE::CREATE);

         foreach ($files as $file) {
             $zip->addFile($file);
         }

         $zip->close();
     }

     /**
      * concat all pdfs into one
      *
      * @param array $files
      */
     protected function concatPdfs($files) {
         foreach ($files as $index => $file) {
             if ($index == 0) {
                 $pdf = new Pdf($file);
             } else {
                 $pdf->addFile($file);
             }
         }

         $pdf->saveAs($this->outFile);
     }
}
{'fraction_non_alphanumeric': 0.1540167570231641, 'fraction_numerical': 0.0034499753573188764, 'mean_word_length': 2.3188879803761244, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2393416', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1321, 'n_words': 301}