<commit_before><?php
namespace jens1o\smashcast\user;

use jens1o\smashcast\SmashcastApi;
use jens1o\smashcast\exception\{SmashcastApiException, SmashcastAuthException};
use jens1o\smashcast\model\AbstractModel;
use jens1o\smashcast\token\SmashcastAuthToken;
use jens1o\smashcast\user\logos\LogoHandler;
use jens1o\smashcast\util\{HttpMethod, LogoSize, RequestUtil};

/**
 * Represents a smashcast User that can access to channels and media
 *
 * @author     jens1o
 * @copyright  <NAME> 2017
 * @license    MIT License
 * @package    jens1o\smashcast
 * @subpackage user
 */
class SmashcastUser extends AbstractModel {

    /**
     * Holds the logos
     * @var LogoHandler
     */
    private $logoHandler = null;

    /**
     * Holds the user auth token
     * @var SmashcastAuthToken
     */
    private $userAuthToken = null;

    /**
     * Creates a new User object.
     * **Warning!** This executes immediately a request to smashcast fetching all data when `$row` is not provided!
     *
     * @param   string|null     $identifier   The name of the user, can be `null` when `$row` is provided
     * @param   mixed[]|null    $row        All information about the user fetched from the api, can be `null` when `$userName` is provided
     * @throws \BadMethodCallException  when `$userName` and `$row` are null
     */
    public function __construct(?string $identifier = null, ?\stdClass $row = null) {
        if($row !== null) {
            // prefer $row when provided, so we don't need to call their api again
            $this->data = $row;
        } elseif($identifier !== null) {
            // call their api
            $this->data = $this->doRequest(HttpMethod::GET, 'user/' . $identifier);
        } else {
            throw new \BadMethodCallException('Try to call ' . self::class . ' with both arguments null. One must be given!');
        }
    }

    /**
     * Returns the id for this user
     *
     * @return int|null
     */
    public function getUserId(): ?int {
        return $this->data->user_id;
    }

    /**
     * Shorthand function to check wether this user exists
     *
     * @return bool
     */
    public function exists(): bool {
        return $this->data->user_id !== null;
    }

    /**
     * Returns the user auth token for this user, or null when it can't be created(because the api didn't provide an auth token)
     *
     * @return SmashcastAuthToken|null
     */
    public function getAuthToken(): ?SmashcastAuthToken {
        if($this->userAuthToken === null && isset($this->data->authToken)) {
            $this->userAuthToken = new SmashcastAuthToken($this->data->authToken);
        }

        return $this->userAuthToken;
    }

    /**
     * Returns the logohandler that manages the logos(or null when the user does not exist)
     *
     * @return LogoHandler|null
     */
    public function getLogos(): ?LogoHandler {
        if($this->logoHandler === null) {
            if(!$this->exists()) {
                throw new \BadMethodCallException('Cannot show logos on non-existing users!');
                return null;
            }

            $this->logoHandler = new LogoHandler([
                LogoSize::SMALL => $this->data->user_logo_small,
                LogoSize::DEFAULT => $this->data->user_logo
            ]);
        }

        return $this->logoHandler;
    }

    /**
     * Returns wether the user is live atm
     *
     * @return bool
     */
    public function isLive(): bool {
        // non existing user can stream? ;)
        if(!$this->exists()) return false;

        if(isset($this->data->media_is_live)) {
            // authorized api
            return (bool) $this->data->media_is_live;
        } elseif(isset($this->data->is_live)) {
            // public api
            return (bool) $this->data->is_live;
        }

        // not implemented, feel free to create a pr adding this!
        return false;
    }

    /**
     * Returns wether this user had validated their email
     *
     * @return bool
     * @throws SmashcastApiException
     */
    public function hasVerifiedEmail(): bool {
        // Smashcast's api handles this right, however we can save some time
        if(!$this->exists()) return false;

        $request = $this->doRequest(HttpMethod::GET, '/user/checkVerifiedEmail/' . $this->data->user_name, ['noAuthToken' => true]);

        if(!isset($request->user->user_activated) || $request->user->user_activated == '0') {
            return false;
        }

        return true;
    }

    /**
     * Returns true when the user has been created with an auth token
     *
     * @return bool
     */
    public function isAuthenticated(): bool {
        return (isset($this->data->login) && $this->data->login === 'true') || isset($this->data->user_email);
    }
<commit_msg>further improving<commit_after><?php
namespace jens1o\smashcast\user;

use jens1o\smashcast\SmashcastApi;
use jens1o\smashcast\exception\{SmashcastApiException, SmashcastAuthException};
use jens1o\smashcast\model\AbstractModel;
use jens1o\smashcast\token\SmashcastAuthToken;
use jens1o\smashcast\user\logos\LogoHandler;
use jens1o\smashcast\util\{HttpMethod, LogoSize, RequestUtil};

/**
 * Represents a smashcast User that can access to channels and media
 *
 * @author     jens1o
 * @copyright  <NAME> 2017
 * @license    MIT License
 * @package    jens1o\smashcast
 * @subpackage user
 */
class SmashcastUser extends AbstractModel {

    /**
     * Holds the logos
     * @var LogoHandler
     */
    private $logoHandler = null;

    /**
     * Holds the user auth token
     * @var SmashcastAuthToken
     */
    private $userAuthToken = null;

    /**
     * Creates a new User object.
     * **Warning!** This executes immediately a request to smashcast fetching all data when `$row` is not provided!
     *
     * @param   string|null     $identifier   The name of the user, can be `null` when `$row` is provided
     * @param   mixed[]|null    $row        All information about the user fetched from the api, can be `null` when `$userName` is provided
     * @throws \BadMethodCallException  when `$userName` and `$row` are null
     */
    public function __construct(?string $identifier = null, ?\stdClass $row = null) {
        if($row !== null) {
            // prefer $row when provided, so we don't need to call their api again
            $this->data = $row;
        } elseif($identifier !== null) {
            // call their api
            $this->data = $this->doRequest(HttpMethod::GET, 'user/' . $identifier);
        } else {
            throw new \BadMethodCallException('Try to call ' . self::class . ' with both arguments null. One must be given!');
        }
    }

    /**
     * Returns the id for this user
     *
     * @return int|null
     */
    public function getUserId(): ?int {
        return $this->data->user_id;
    }

    /**
     * Shorthand function to check wether this user exists
     *
     * @return bool
     */
    public function exists(): bool {
        return $this->data->user_id !== null;
    }

    /**
     * Returns the user auth token for this user, or null when it can't be created(because the api didn't provide an auth token)
     *
     * @return SmashcastAuthToken|null
     */
    public function getAuthToken(): ?SmashcastAuthToken {
        if($this->userAuthToken === null && isset($this->data->authToken)) {
            $this->userAuthToken = new SmashcastAuthToken($this->data->authToken);
        }

        return $this->userAuthToken;
    }

    /**
     * Returns the logohandler that manages the logos(or null when the user does not exist)
     *
     * @return LogoHandler|null
     */
    public function getLogos(): ?LogoHandler {
        if($this->logoHandler === null) {
            if(!$this->exists()) {
                throw new \BadMethodCallException('Cannot show logos on non-existing users!');
                return null;
            }

            $this->logoHandler = new LogoHandler([
                LogoSize::SMALL => $this->data->user_logo_small,
                LogoSize::DEFAULT => $this->data->user_logo
            ]);
        }

        return $this->logoHandler;
    }

    /**
     * Returns wether the user is live atm
     *
     * @return bool
     */
    public function isLive(): bool {
        // non existing user can stream? ;)
        if(!$this->exists()) return false;

        if(isset($this->data->media_is_live)) {
            // authorized api
            return (bool) $this->data->media_is_live;
        } elseif(isset($this->data->is_live)) {
            // public api
            return (bool) $this->data->is_live;
        }

        // not implemented, feel free to create a pr adding this!
        return false;
    }

    /**
     * Returns wether this user had validated their email
     *
     * @return bool
     * @throws SmashcastApiException
     */
    public function hasVerifiedEmail(): bool {
        // Smashcast's api handles this right, however we can save some time
        if(!$this->exists()) return false;

        $request = $this->doRequest(HttpMethod::GET, '/user/checkVerifiedEmail/' . $this->data->user_name, ['noAuthToken' => true]);

        if(!isset($request->user->user_activated) || $request->user->user_activated == '0') {
            return false;
        }

        return true;
    }

    /**
     * Returns true when the user has been created with an auth token
     *
     * @return bool
     */
    public function isAuthenticated(): bool {
        return (isset($this->data->login) && $this->data->login === 'true') || isset($this->data->authToken) || isset($this->data->user_email);
    }

{'fraction_non_alphanumeric': 0.12289056838182007, 'fraction_numerical': 0.0028988508127135316, 'mean_word_length': 2.2569116655428187, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 103, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1751543', 'n_tokens_mistral': 2841, 'n_tokens_neox': 2770, 'n_words': 792}