<?php

namespace App\Jobs;

use App\Helpers\AvatarHelper;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class GenerateUserAvatar implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
	
	protected $user;
	
	public function __construct(User $user)
	{
		$this->user = $user;
	}
	
	public function handle()
	{
		$avatarPath = AvatarHelper::generateAvatar(
			$this->user->name,
			$this->user->username,
			$this->user->id
		);
		
		$this->user->update(['avatar' => $avatarPath]);
	}
}
