User.php 5.02 KB
<?php

namespace FootyRoom\User;

use FootyRoom\Core\EventGenerator;
use Illuminate\Auth\Authenticatable;
use FootyRoom\Core\User\UserRegistered;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableInterface;

class User implements AuthenticatableInterface
{
    use EventGenerator;

    // use Accessors;
    use Authenticatable;

    /**
     * Number of days the user has to stay in the newborn status.
     */
    protected static $birthPeriod = 10;

    /**
     * @var int
     */
    protected $user_id;

    public function getUserId()
    {
        return $this->user_id;
    }

    public function getAuthIdentifier()
    {
        return $this->getUserId();
    }

    /**
     * @var string
     */
    protected $username;

    public function getUsername()
    {
        return $this->username;
    }

    /**
     * @var string
     */
    protected $fullname;

    public function getFullname()
    {
        return $this->fullname;
    }

    public function setFullname($fullname)
    {
        $this->fullname = $fullname;
    }

    /**
     * Hashed representation of password.
     *
     * @var string
     */
    protected $password;

    /**
     * Password getter.
     *
     * @return string
     */
    public function getPassword()
    {
        return $this->getAuthPassword();
    }

    protected $email;

    public function getEmail()
    {
        return $this->email;
    }

    protected $gender;

    public function getGender()
    {
        return $this->gender;
    }

    protected $dob;

    public function getDob()
    {
        return $this->dob;
    }

    protected $user_role;

    public function getRole()
    {
        return $this->user_role;
    }

    protected $status;

    public function getStatus()
    {
        return $this->status;
    }

    protected $date_registered;

    public function getDateRegistered()
    {
        return $this->date_registered;
    }

    protected $national_team;

    public function getNationalTeam()
    {
        return $this->national_team;
    }

    protected $national_team_id;

    protected $club_team;

    public function getClubTeam()
    {
        return $this->club_team;
    }

    protected $club_team_id;

    protected $location;

    public function getLocation()
    {
        return $this->location;
    }

    public function getTimezone()
    {
        return $this->tz;
    }

    protected $noSpoilers;

    public function isNoSpoilers()
    {
        return $this->noSpoilers;
    }

    protected $tz;

    protected $activation_key;

    /**
     * Constructor.
     *
     * @param array|object
     */
    public function __construct($options)
    {
        $options = (object) $options;

        foreach ($options as $key => $value) {
            if (property_exists($this, $key)) {
                $this->{$key} = $value;
            }
        }
    }

    /**
     * Register this user under specified user id.
     *
     * @param int $userId
     */
    public function register($userId)
    {
        $this->user_id = $userId;

        $this->raise(new UserRegistered($this));
    }

    public function birth()
    {
        $this->status = '';
    }

    public function readyForBirth()
    {
        return date_diff(
            new \DateTime($this->date_registered),
            new \DateTime()
        )->format('%r%a') > self::$birthPeriod;
    }

    public function isNewborn()
    {
        return $this->status === 'newborn';
    }

    public function isBanned()
    {
        return $this->status === 'banned';
    }

    /**
     * Decides whether user's account is open.
     *
     * @return bool
     */
    public function accountOpen()
    {
        if ($this->status === 'banned' || $this->status === 'activation') {
            return false;
        }

        return true;
    }

    public function canCreateBans()
    {
        if ($this->user_role > 20) {
            return true;
        }

        return false;
    }

    public function canGiveBettingTips(): bool
    {
        if ($this->user_role === 11 || $this->user_role >= 26) {
            return true;
        }

        return false;
    }

    /**
     * Decides whether user is a moderator.
     *
     * @return bool
     */
    public function isModerator()
    {
        return $this->user_role >= 20;
    }

    public function isAdmin(): bool
    {
        return $this->user_role >= 30;
    }

    /**
     * Returns `true` if given password matches stored password.
     *
     * @param  string $plain
     *
     * @return bool
     */
    public function comparePassword($plain)
    {
        return $this->password === md5($plain);
    }

    /**
     * Set the token value for the "remember me" session. We don't do anything
     * here since our remember token is a users hashed password.
     *
     * @param  string  $value
     * @return void
     */
    public function setRememberToken($value)
    {
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        return 'password';
    }
}