<commit_before><?php

namespace Atorscho\Membership;

use Atorscho\Membership\Groups\Group;
use Atorscho\Membership\Permissions\Permission;
use Illuminate\Auth\Guard;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Filesystem\Filesystem;

class Membership
{
    /**
     * @var Guard
     */
    private $auth;

    /**
     * @var Filesystem
     */
    private $file;

    /**
     * @var Permission
     */
    private $permissions;

    /**
     * @var Group
     */
    private $groups;

    /**
     * Membership constructor.
     *
     * @param Guard      $auth
     * @param Filesystem $file
     * @param Group      $groups
     * @param Permission $permissions
     */
    public function __construct(Guard $auth, Filesystem $file, Group $groups, Permission $permissions)
    {
        $this->auth        = $auth;
        $this->file        = $file;
        $this->permissions = $permissions;
        $this->groups      = $groups;
    }

    /**
     * Create a user and assign it to a group.
     *
     * @param array  $attributes The user attributes.
     * @param string $group      Group handle or group model [Optional]
     *
     * @return object
     */
    public function createUser(array $attributes, $group = null)
    {
        // Default group
        if (!$group) {
            $group = $this->groups->findOrFail(config('membership.groups.default'));
        }

        // Get the group by its handle
        if (is_string($group)) {
            $group = $this->groups->whereHandle($group)->firstOrFail();
        }

        // Get user model class name
        $class = config('auth.model');

        // Create the user and assign it to the specific group
        $user = $class::create($attributes);
        $user->assignTo($group);

        return $user;
    }

    /**
     * Create a member.
     *
     * @param array $attributes
     *
     * @return object
     */
    public function createMember(array $attributes)
    {
        return $this->createUser($attributes, 'members');
    }

    /**
     * Create a moderator.
     *
     * @param array $attributes
     *
     * @return object
     */
    public function createModerator(array $attributes)
    {
        return $this->createUser($attributes, 'moderators');
    }

    /**
     * Create a super-moderator.
     *
     * @param array $attributes
     *
     * @return object
     */
    public function createSuperModerator(array $attributes)
    {
        return $this->createUser($attributes, 'super-moderators');
    }

    /**
     * Create an administrator.
     *
     * @param array $attributes
     *
     * @return object
     */
    public function createAdministrator(array $attributes)
    {
        return $this->createUser($attributes, 'administrators');
    }

    /**
     * Create an owner.
     *
     * @param array $attributes
     *
     * @return object
     */
    public function createOwner(array $attributes)
    {
        return $this->createUser($attributes, 'owners');
    }

    /**
     * Get currently logged in user instance or its attribute value.
     *
     * @param string|null $attribute User column name.
     *
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function currentUser($attribute = null)
    {
        if (auth()->guest()) {
            return null;
        }

        $user = $this->getCurrentUserInstance();

        if (!$attribute) {
            return $user;
        }

        if (in_array($attribute, ['email', 'mail'])) {
            return $this->obfuscate($user->{$attribute});
        }

        return $user->{$attribute} ?: null;
    }

    /**
     * Determine if the authenticated user's attribute is not empty.
     *
     * @param string $attribute
     *
     * @return bool
     */
    public function currentUserHas($attribute)
    {
        return auth()->guest() && $this->getCurrentUserInstance()->{$attribute};
    }

    /**
     * Get user's avatar. If none found, return the default one.
     *
     * @param object|null $user User model instance. [Optional]
     *
     * @return string
     */
    public function avatar($user = null)
    {
        if ($this->auth->guest()) {
            return '';
        }

        if (!$user) {
            $user = $this->currentUser();
        }

        $avatar = $this->getAvatarPath() . $user->avatar;

        // Default avatar if user has not chosen one
        if (!$this->avatarExists($user)) {
            return asset($this->getDefaultAvatarPath());
        }

        return asset($avatar);
    }

    /**
     * Check wheter user avatar exists.
     *
     * @param object|null $user User model instance. [Optional]
     *
     * @return bool
     */
    public function avatarExists($user = null)
    {
        if (!$user) {<commit_msg>Avatar fix<commit_after><?php

namespace Atorscho\Membership;

use Atorscho\Membership\Groups\Group;
use Atorscho\Membership\Permissions\Permission;
use Illuminate\Auth\Guard;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Filesystem\Filesystem;

class Membership
{
    /**
     * @var Guard
     */
    private $auth;

    /**
     * @var Filesystem
     */
    private $file;

    /**
     * @var Permission
     */
    private $permissions;

    /**
     * @var Group
     */
    private $groups;

    /**
     * Membership constructor.
     *
     * @param Guard      $auth
     * @param Filesystem $file
     * @param Group      $groups
     * @param Permission $permissions
     */
    public function __construct(Guard $auth, Filesystem $file, Group $groups, Permission $permissions)
    {
        $this->auth        = $auth;
        $this->file        = $file;
        $this->permissions = $permissions;
        $this->groups      = $groups;
    }

    /**
     * Create a user and assign it to a group.
     *
     * @param array  $attributes The user attributes.
     * @param string $group      Group handle or group model [Optional]
     *
     * @return object
     */
    public function createUser(array $attributes, $group = null)
    {
        // Default group
        if (!$group) {
            $group = $this->groups->findOrFail(config('membership.groups.default'));
        }

        // Get the group by its handle
        if (is_string($group)) {
            $group = $this->groups->whereHandle($group)->firstOrFail();
        }

        // Get user model class name
        $class = config('auth.model');

        // Create the user and assign it to the specific group
        $user = $class::create($attributes);
        $user->assignTo($group);

        return $user;
    }

    /**
     * Create a member.
     *
     * @param array $attributes
     *
     * @return object
     */
    public function createMember(array $attributes)
    {
        return $this->createUser($attributes, 'members');
    }

    /**
     * Create a moderator.
     *
     * @param array $attributes
     *
     * @return object
     */
    public function createModerator(array $attributes)
    {
        return $this->createUser($attributes, 'moderators');
    }

    /**
     * Create a super-moderator.
     *
     * @param array $attributes
     *
     * @return object
     */
    public function createSuperModerator(array $attributes)
    {
        return $this->createUser($attributes, 'super-moderators');
    }

    /**
     * Create an administrator.
     *
     * @param array $attributes
     *
     * @return object
     */
    public function createAdministrator(array $attributes)
    {
        return $this->createUser($attributes, 'administrators');
    }

    /**
     * Create an owner.
     *
     * @param array $attributes
     *
     * @return object
     */
    public function createOwner(array $attributes)
    {
        return $this->createUser($attributes, 'owners');
    }

    /**
     * Get currently logged in user instance or its attribute value.
     *
     * @param string|null $attribute User column name.
     *
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function currentUser($attribute = null)
    {
        if (auth()->guest()) {
            return null;
        }

        $user = $this->getCurrentUserInstance();

        if (!$attribute) {
            return $user;
        }

        if (in_array($attribute, ['email', 'mail'])) {
            return $this->obfuscate($user->{$attribute});
        }

        return $user->{$attribute} ?: null;
    }

    /**
     * Determine if the authenticated user's attribute is not empty.
     *
     * @param string $attribute
     *
     * @return bool
     */
    public function currentUserHas($attribute)
    {
        return auth()->guest() && $this->getCurrentUserInstance()->{$attribute};
    }

    /**
     * Get user's avatar. If none found, return the default one.
     *
     * @param object|null $user User model instance. [Optional]
     *
     * @return string
     */
    public function avatar($user = null)
    {
        if ($this->auth->guest() && !$user) {
            return '';
        }

        if (!$user) {
            $user = $this->currentUser();
        }

        $avatar = $this->getAvatarPath() . $user->avatar;

        // Default avatar if user has not chosen one
        if (!$this->avatarExists($user)) {
            return asset($this->getDefaultAvatarPath());
        }

        return asset($avatar);
    }

    /**
     * Check wheter user avatar exists.
     *
     * @param object|null $user User model instance. [Optional]
     *
     * @return bool
     */
    public function avatarExists($user = null)
    {
        if (!$user) {
{'fraction_non_alphanumeric': 0.12884515794924908, 'fraction_numerical': 0.0, 'mean_word_length': 1.894484412470024, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, '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': '6025630', 'n_tokens_mistral': 2764, 'n_tokens_neox': 2717, 'n_words': 678}