<?php

namespace Dipper\Excel\Jobs;

use Dipper\Excel\Writer;
use Dipper\Excel\HasEventBus;
use Illuminate\Bus\Queueable;
use Dipper\Excel\Events\AfterStore;
use Dipper\Excel\Concerns\WithEvents;
use Illuminate\Contracts\Queue\ShouldQueue;

class AfterQueueExportJob implements ShouldQueue
{
    use Queueable, HasEventBus;

    /**
     * @var Writer
     */
    public $writer;

    /**
     * @var object
     */
    public $exportable;

    /**
     * @var string|null
     */
    public $filePath;

    /**
     * @var string|null
     */
    public $disk;

    /**
     * @param Writer $writer
     * @param object $exportable
     */
    public function __construct(Writer $writer, $exportable, $filePath, $disk = null)
    {
        $this->writer     = $writer;
        $this->exportable = $exportable;
        $this->filePath = $filePath;
        $this->disk = $disk;
    }

    /**
     * @param FilesystemManager $filesystem
     */
    public function handle()
    {
        if ($this->exportable instanceof WithEvents) {
            $this->registerListeners($this->exportable->registerEvents());
        }

        $this->raise(new AfterStore($this->writer, $this->exportable, $this->filePath, $this->disk));
    }
}
