AuthEventListener.php 1.45 KB
<?php

namespace FootyRoom\Listeners;

use FootyRoom\User\User;
use FootyRoom\Config;
use Illuminate\Http\Request;
use Illuminate\Contracts\Events\Dispatcher;
use FootyRoom\Repositories\UserRepository;

class AuthEventListener
{
    /**
     * Create a new AuthEventListener instance.
     *
     * @param \Illuminate\Contracts\Config\Repository $config
     */
    public function __construct(Config $config, UserRepository $userRepo)
    {
        $this->config = $config;
        $this->userRepo = $userRepo;
    }

    /**
     * Removes newborn status if 10 days have passed since the user activated
     * his account, i.e. officially registered.
     *
     * @param  \FootyRoom\User\User $user
     *
     * @return void
     */
    public function newbornCheck(User $user)
    {
        if ($user->isNewborn() && $user->readyForBirth()) {
            $user->birth();
            $this->userRepo->update($user->getUserId(), ['status' => $user->getStatus()]);
        }
    }

    /**
     * Records login if configured to record.
     *
     * @param  \FootyRoom\User\User    $user
     * @param  \Illuminate\Http\Request $request
     *
     * @return void
     */
    public function recordLogin(User $user, Request $request)
    {
        if ($this->config->get('recordLogins')) {
            $this->userRepo->recordLogin(
                $user->getUserId(),
                $request->ip(),
                $request->session()->get('tracker')
            );
        }
    }
}