<?php

namespace App\Jobs\Dashboard\UsefulInfoItemJob;

use App\Models\UsefulInfoItem;
use Illuminate\Support\Arr;
use App\Http\Requests\Dashboard\UsefulInfoItemRequest\Update as Request;

class Update
{
    protected $usefulInfoItem;
    protected $attr;

    /**
     * Update constructor.
     * @param UsefulInfoItem $usefulInfoItem
     * @param array $attr
     */
    public function __construct(UsefulInfoItem $usefulInfoItem, array $attr = [])
    {
        $this->attr = Arr::only($attr, ['name', 'file_url', 'description', 'video_url', 'link_url', 'useful_info_id']);
        $this->usefulInfoItem = $usefulInfoItem;
    }

    /**
     * @param UsefulInfoItem $usefulInfoItem
     * @param Request $request
     * @param $file_path
     * @return Update
     */
    public static function fromRequest(UsefulInfoItem $usefulInfoItem, Request $request, $file_path)
    {
        return new static($usefulInfoItem, [
            'name' => $request->getName(),
            'description' => $request->getDescription(),
            'video_url' => $request->video_url,
            'link_url' => $request->link_url,
            'file_url' => $file_path,
        ]);
    }

    /**
     *
     */
    public function handle()
    {
        $this->usefulInfoItem->update($this->attr);
    }
}
