<commit_before><?php
namespace Nodes\Backend\Http\Controllers;

use Exception;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Str;
use Nodes\Backend\Models\Role\RoleRepository;
use Nodes\Backend\Models\Role\Validation\RoleValidator;

/**
 * Class UsersRolesController
 *
 * @package Nodes\Backend\Http\Controllers
 */
class RolesController extends Controller
{
    /**
     * Role repository
     *
     * @var \Nodes\Backend\Models\Role\RoleRepository
     */
    protected $roleRepository;

    /**
     * Constructor
     *
     * @access public
     * @param  \Nodes\Backend\Models\Role\RoleRepository $roleRepository
     */
    public function __construct(RoleRepository $roleRepository)
    {
        if (Gate::denies('backend-developer')) {
            abort(403);
        }

        $this->roleRepository = $roleRepository;
    }

    /**
     * List all roles
     *
     * @author <NAME> <<EMAIL>>
     *
     * @access public
     * @return \Illuminate\View\View
     */
    public function index()
    {
        // Retrieve all roles
        $roles = $this->roleRepository->getPaginatedForBackend();

        return view('nodes.backend::backend-users.roles', compact('roles'));
    }

    /**
     * Save new role to database
     *
     * @author <NAME> <<EMAIL>>
     *
     * @access public
     * @param  \Nodes\Backend\Models\Role\Validation\RoleValidator $roleValidator
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(RoleValidator $roleValidator)
    {
        // Retrieve posted data
        $data = Request::only('title');

        // Slugify role title
        $data['slug'] = Str::slug($data['title']);

        // Validate role and redirect if invalidate
        if (!$roleValidator->with($data)->validate()) {
            return redirect()->route('nodes.backend.users.roles')->with('error', 'Role slug already exists');
        }

        try {
            $this->roleRepository->create($data);
            return redirect()->route('nodes.backend.users.roles')->with('success', 'Role was successfully created.');
        } catch (Exception $e) {
            return redirect()->route('nodes.backend.users.roles')->with('error', 'Could not create role');
        }
    }

    /**
     * We only update title since slug is used for gates
     *
     * @author <NAME> <<EMAIL>>
     *
     * @access public
     * @param  integer $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update($id)
    {
        // Retrieve posted data
        $data = Request::only('title');

        // Retrieve role by ID
        $role = $this->roleRepository->getById($id);
        if (!empty($role)) {
            return redirect()->route('nodes.backend.users.roles')->with('error', 'Role does not exist');
        }

        // Check if an update is required
        if ($role->title == $data['title']) {
            return redirect()->route('nodes.backend.users.roles')->with('info', 'Role title has not changed. No update required');
        }

        try {<commit_msg>Fixed a bug with updating roles<commit_after><?php
namespace Nodes\Backend\Http\Controllers;

use Exception;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Str;
use Nodes\Backend\Models\Role\RoleRepository;
use Nodes\Backend\Models\Role\Validation\RoleValidator;

/**
 * Class UsersRolesController
 *
 * @package Nodes\Backend\Http\Controllers
 */
class RolesController extends Controller
{
    /**
     * Role repository
     *
     * @var \Nodes\Backend\Models\Role\RoleRepository
     */
    protected $roleRepository;

    /**
     * Constructor
     *
     * @access public
     * @param  \Nodes\Backend\Models\Role\RoleRepository $roleRepository
     */
    public function __construct(RoleRepository $roleRepository)
    {
        if (Gate::denies('backend-developer')) {
            abort(403);
        }

        $this->roleRepository = $roleRepository;
    }

    /**
     * List all roles
     *
     * @author <NAME> <<EMAIL>>
     *
     * @access public
     * @return \Illuminate\View\View
     */
    public function index()
    {
        // Retrieve all roles
        $roles = $this->roleRepository->getPaginatedForBackend();

        return view('nodes.backend::backend-users.roles', compact('roles'));
    }

    /**
     * Save new role to database
     *
     * @author <NAME> <<EMAIL>>
     *
     * @access public
     * @param  \Nodes\Backend\Models\Role\Validation\RoleValidator $roleValidator
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(RoleValidator $roleValidator)
    {
        // Retrieve posted data
        $data = Request::only('title');

        // Slugify role title
        $data['slug'] = Str::slug($data['title']);

        // Validate role and redirect if invalidate
        if (!$roleValidator->with($data)->validate()) {
            return redirect()->route('nodes.backend.users.roles')->with('error', 'Role slug already exists');
        }

        try {
            $this->roleRepository->create($data);
            return redirect()->route('nodes.backend.users.roles')->with('success', 'Role was successfully created.');
        } catch (Exception $e) {
            return redirect()->route('nodes.backend.users.roles')->with('error', 'Could not create role');
        }
    }

    /**
     * We only update title since slug is used for gates
     *
     * @author <NAME> <<EMAIL>>
     *
     * @access public
     * @param  integer $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update($id)
    {
        // Retrieve posted data
        $data = Request::only('title');

        // Retrieve role by ID
        $role = $this->roleRepository->getById($id);
        if (empty($role)) {
            return redirect()->route('nodes.backend.users.roles')->with('error', 'Role does not exist');
        }

        // Check if an update is required
        if ($role->title == $data['title']) {
            return redirect()->route('nodes.backend.users.roles')->with('info', 'Role title has not changed. No update required');
        }

        try {
{'fraction_non_alphanumeric': 0.1415830546265329, 'fraction_numerical': 0.0009555661729574773, 'mean_word_length': 2.390928725701944, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 61, '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': '3734964', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1767, 'n_words': 383}