<?php

/*
 * This file is part of fof/ban-ips.
 *
 * Copyright (c) 2019 FriendsOfFlarum.
 *
 * For the full copyright and license information, please view the LICENSE.md
 * file that was distributed with this source code.
 */

namespace FoF\BanIPs\Commands;

use Carbon\Carbon;
use Flarum\User\AssertPermissionTrait;
use Flarum\User\User;
use FoF\BanIPs\BannedIP;
use FoF\BanIPs\Events\IPWasBanned;
use FoF\BanIPs\Validators\BannedIPValidator;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Arr;

class CreateBannedIPHandler
{
    use AssertPermissionTrait;

    /**
     * @var BannedIPValidator
     */
    protected $validator;

    /**
     * @var Dispatcher
     */
    private $events;

    /**
     * @param Dispatcher        $events
     * @param BannedIPValidator $validator
     */
    public function __construct(Dispatcher $events, BannedIPValidator $validator)
    {
        $this->events = $events;
        $this->validator = $validator;
    }

    /**
     * @param CreateBannedIP $command
     *
     * @return mixed
     */
    public function handle(CreateBannedIP $command)
    {
        $actor = $command->actor;
        $data = $command->data;

        $userId = Arr::get($data, 'attributes.userId');
        $user = $userId != null ? User::where('id', $userId)->orWhere('username', $userId)->firstOrFail() : null;

        $this->assertCan($actor, 'banIP', $user);

        $bannedIP = BannedIP::build(
            $actor->id,
            $user ? $user->id : null,
            Arr::get($data, 'attributes.address'),
            Arr::get($data, 'attributes.reason')
        );
        $bannedIP->created_at = Carbon::now();

        $this->validator->assertValid($bannedIP->getAttributes());

        $bannedIP->save();

        $this->events->fire(
            new IPWasBanned($actor, $bannedIP)
        );

        return $bannedIP;
    }
}
