<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of the Sonata Project package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\UserBundle\Security\RolesBuilder;

use Sonata\AdminBundle\Admin\Pool;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Translation\TranslatorInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
final class SecurityRolesBuilder implements ExpandableRolesBuilderInterface
{
    /**
     * @var AuthorizationCheckerInterface
     */
    private $authorizationChecker;

    /**
     * @var Pool
     */
    private $pool;

    /**
     * @var TranslatorInterface
     */
    private $translator;

    /**
     * @var string []
     */
    private $rolesHierarchy;

    public function __construct(
        AuthorizationCheckerInterface $authorizationChecker,
        Pool $pool,
        TranslatorInterface $translator,
        array $rolesHierarchy = []
    ) {
        $this->authorizationChecker = $authorizationChecker;
        $this->pool = $pool;
        $this->translator = $translator;
        $this->rolesHierarchy = $rolesHierarchy;
    }

    public function getExpandedRoles(?string $domain = null): array
    {
        $securityRoles = [];
        foreach ($hierarchy = $this->getHierarchy() as $role => $childRoles) {
            $translatedRoles = array_map(
                [$this, 'translateRole'],
                $childRoles,
                array_fill(0, \count($childRoles), $domain)
            );

            $translatedRoles = \count($translatedRoles) > 0 ? ': '.implode(', ', $translatedRoles) : '';
            $securityRoles[$role] = [
                'role' => $role,
                'role_translated' => $role.$translatedRoles,
                'is_granted' => $this->authorizationChecker->isGranted($role),
            ];

            $securityRoles = array_merge(
                $securityRoles,
                $this->getSecurityRoles($hierarchy, $childRoles, $domain)
            );
        }

        return $securityRoles;
    }

    public function getRoles(?string $domain = null): array
    {
        $securityRoles = [];
        foreach ($hierarchy = $this->getHierarchy() as $role => $childRoles) {
            $securityRoles[$role] = $this->getSecurityRole($role, $domain);
            $securityRoles = array_merge(
                $securityRoles,
                $this->getSecurityRoles($hierarchy, $childRoles, $domain)
            );
        }

        return $securityRoles;
    }

    private function getHierarchy(): array
    {
        return array_merge([
            $this->pool->getOption('role_super_admin') => [],
            $this->pool->getOption('role_admin') => [],
        ], $this->rolesHierarchy);
    }

    private function getSecurityRole(string $role, ?string $domain): array
    {
        return [
            'role' => $role,
            'role_translated' => $this->translateRole($role, $domain),
            'is_granted' => $this->authorizationChecker->isGranted($role),
        ];
    }

    private function getSecurityRoles(array $hierarchy, array $roles, ?string $domain): array
    {
        $securityRoles = [];
        foreach ($roles as $role) {
            if (!array_key_exists($role, $hierarchy) && !isset($securityRoles[$role])
                && !$this->recursiveArraySearch($role, $securityRoles)) {
                $securityRoles[$role] = $this->getSecurityRole($role, $domain);
            }
        }

        return $securityRoles;
    }

    private function translateRole(string $role, $domain): string
    {
        if ($domain) {
            return $this->translator->trans($role, [], $domain);
        }

        return $role;
    }

    private function recursiveArraySearch(string $role, array $roles): bool
    {
        foreach ($roles as $key => $value) {
            if ($role === $key || (\is_array($value) && true === $this->recursiveArraySearch($role, $value))) {
                return true;
            }
        }

        return false;
    }
}
<commit_msg>Apply latest cs-fixer rules<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of the Sonata Project package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\UserBundle\Security\RolesBuilder;

use Sonata\AdminBundle\Admin\Pool;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Translation\TranslatorInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
final class SecurityRolesBuilder implements ExpandableRolesBuilderInterface
{
    /**
     * @var AuthorizationCheckerInterface
     */
    private $authorizationChecker;

    /**
     * @var Pool
     */
    private $pool;

    /**
     * @var TranslatorInterface
     */
    private $translator;

    /**
     * @var string []
     */
    private $rolesHierarchy;

    public function __construct(
        AuthorizationCheckerInterface $authorizationChecker,
        Pool $pool,
        TranslatorInterface $translator,
        array $rolesHierarchy = []
    ) {
        $this->authorizationChecker = $authorizationChecker;
        $this->pool = $pool;
        $this->translator = $translator;
        $this->rolesHierarchy = $rolesHierarchy;
    }

    public function getExpandedRoles(?string $domain = null): array
    {
        $securityRoles = [];
        foreach ($hierarchy = $this->getHierarchy() as $role => $childRoles) {
            $translatedRoles = array_map(
                [$this, 'translateRole'],
                $childRoles,
                array_fill(0, \count($childRoles), $domain)
            );

            $translatedRoles = \count($translatedRoles) > 0 ? ': '.implode(', ', $translatedRoles) : '';
            $securityRoles[$role] = [
                'role' => $role,
                'role_translated' => $role.$translatedRoles,
                'is_granted' => $this->authorizationChecker->isGranted($role),
            ];

            $securityRoles = array_merge(
                $securityRoles,
                $this->getSecurityRoles($hierarchy, $childRoles, $domain)
            );
        }

        return $securityRoles;
    }

    public function getRoles(?string $domain = null): array
    {
        $securityRoles = [];
        foreach ($hierarchy = $this->getHierarchy() as $role => $childRoles) {
            $securityRoles[$role] = $this->getSecurityRole($role, $domain);
            $securityRoles = array_merge(
                $securityRoles,
                $this->getSecurityRoles($hierarchy, $childRoles, $domain)
            );
        }

        return $securityRoles;
    }

    private function getHierarchy(): array
    {
        return array_merge([
            $this->pool->getOption('role_super_admin') => [],
            $this->pool->getOption('role_admin') => [],
        ], $this->rolesHierarchy);
    }

    private function getSecurityRole(string $role, ?string $domain): array
    {
        return [
            'role' => $role,
            'role_translated' => $this->translateRole($role, $domain),
            'is_granted' => $this->authorizationChecker->isGranted($role),
        ];
    }

    private function getSecurityRoles(array $hierarchy, array $roles, ?string $domain): array
    {
        $securityRoles = [];
        foreach ($roles as $role) {
            if (!\array_key_exists($role, $hierarchy) && !isset($securityRoles[$role])
                && !$this->recursiveArraySearch($role, $securityRoles)) {
                $securityRoles[$role] = $this->getSecurityRole($role, $domain);
            }
        }

        return $securityRoles;
    }

    private function translateRole(string $role, $domain): string
    {
        if ($domain) {
            return $this->translator->trans($role, [], $domain);
        }

        return $role;
    }

    private function recursiveArraySearch(string $role, array $roles): bool
    {
        foreach ($roles as $key => $value) {
            if ($role === $key || (\is_array($value) && true === $this->recursiveArraySearch($role, $value))) {
                return true;
            }
        }

        return false;
    }
}

{'fraction_non_alphanumeric': 0.1337784666746155, 'fraction_numerical': 0.0007153928699177298, 'mean_word_length': 2.236111111111111, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 87, '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': '3827764', 'n_tokens_mistral': 2448, 'n_tokens_neox': 2371, 'n_words': 471}