<?php

namespace App\Jobs\Dashboard\UsefulInfoJob;

use App\Models\UsefulInfo;
use Illuminate\Support\Arr;
use App\Http\Requests\Dashboard\UsefulInfoRequest\Update as Request;

class Update
{
    protected $usefulinfo;
    protected $attr;

    /**
     * Update constructor.
     * @param UsefulInfo $usefulinfo
     * @param array $attr
     */
    public function __construct(UsefulInfo $usefulinfo, array $attr = [])
    {
        $this->attr = Arr::only($attr, ['name', 'image', 'position']);
        $this->usefulinfo = $usefulinfo;
    }

    /**
     * @param UsefulInfo $usefulinfo
     * @param Request $request
     * @param $path
     * @return Update
     */
    public static function fromRequest(UsefulInfo $usefulinfo, Request $request, $path)
    {
        return new static($usefulinfo, [
            'name' => $request->getName(),
            'image' => $path,
            'position' => $request->getPosition()
        ]);
    }

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