<?php

namespace App\Classes\Jobs;

use App\Classes\Modules\Documents\Processors\ListDocumentsJobProcessor;
use App\Classes\Modules\Jobs\DataTransferObjects\JobSubmissionObject;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;


class ListDocumentsJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $timeout = 900;

    /** @var JobSubmissionObject */
    private $jobSubmissionObject;

    private $jobId;

    /**
     * ListDocumentsJob constructor.
     * @param JobSubmissionObject $JobSubmissionObject
     */
    public function __construct(JobSubmissionObject $jobSubmissionObject)
    {
        $this->jobSubmissionObject = $jobSubmissionObject;
    }

    public function handle()
    {
        $rawPayload = $this->job->payload();
        if(isset($rawPayload['data']['commandName'])){
            $this->jobSubmissionObject->setJobCommandName($rawPayload['data']['commandName']);
        }

        if(isset($rawPayload['data']['command'])){
            $this->jobSubmissionObject->setJobCommand($rawPayload['data']['command']);
        }

        (App()->make(ListDocumentsJobProcessor::class))->execute($this->jobSubmissionObject);
    }

    public function getJobId(){
        return $this->job->getJobId();
    }
}
