<commit_before><?php

namespace Ticketpark\DynamicRoleBundle\Security;

use Symfony\Component\Security\Core\Role\RoleHierarchy as BaseRoleHierarchy;
/**
 * @author <NAME> <<EMAIL>>
 */
class RoleHierarchy extends BaseRoleHierarchy
{
    protected $options;
    protected $connection;

    /**
     * {@inheritdoc}
     */
    public function __construct(array $options, array $hierarchy, $connection)
    {
        $this->options = $options;
        $this->connection = $connection;

        return parent::__construct(array_merge($hierarchy, $this->buildHierarchy()));
    }

    /**
     * Merges the $hierarchies from config and db.
     */
    protected function buildHierarchy()
    {
        $hierarchy = array();

        foreach ($this->connection->executeQuery($this->getFindRolesSql())->fetchAll() as $data) {
            if ($data['parent_role']) {
                if (!isset($hierarchy[$data['parent_role']])) {
                    $hierarchy[$data['parent_role']] = array();
                }
                $hierarchy[$data['parent_role']][] = $data['role'];
            } else {
                if (!isset($hierarchy[$data['role']])) {
                    $hierarchy[$data['role']] = array();
                }
            }
        }

        return $hierarchy;
    }

    /**
     * Constructs the SQL for retrieving roles
     *
     * @return string
     */
    protected function getFindRolesSql()
    {
        $query = <<<QUERY
            SELECT r.name AS role, p.name AS parent_role
            FROM {$this->options['role_table_name']} as r
            LEFT JOIN {$this->options['role_table_name']} as p ON p.id = r.parent_id
QUERY;

        return $query;
    }
}
<commit_msg>added check if table exists before attempting to fetch roles<commit_after><?php

namespace Ticketpark\DynamicRoleBundle\Security;

use Symfony\Component\Security\Core\Role\RoleHierarchy as BaseRoleHierarchy;
/**
 * @author <NAME> <<EMAIL>>
 */
class RoleHierarchy extends BaseRoleHierarchy
{
    protected $options;
    protected $connection;

    /**
     * {@inheritdoc}
     */
    public function __construct(array $options, array $hierarchy, $connection)
    {
        $this->options = $options;
        $this->connection = $connection;

        return parent::__construct(array_merge($hierarchy, $this->buildHierarchy()));
    }

    /**
     * Merges the $hierarchies from config and db.
     */
    protected function buildHierarchy()
    {
        $hierarchy = array();

        if ($this->connection->getSchemaManager()->tablesExist(array($this->options['role_table_name']))) {
            foreach ($this->connection->executeQuery($this->getFindRolesSql())->fetchAll() as $data) {
                if ($data['parent_role']) {
                    if (!isset($hierarchy[$data['parent_role']])) {
                        $hierarchy[$data['parent_role']] = array();
                    }
                    $hierarchy[$data['parent_role']][] = $data['role'];
                } else {
                    if (!isset($hierarchy[$data['role']])) {
                        $hierarchy[$data['role']] = array();
                    }
                }
            }
        }

        return $hierarchy;
    }

    /**
     * Constructs the SQL for retrieving roles
     *
     * @return string
     */
    protected function getFindRolesSql()
    {
        $query = <<<QUERY
            SELECT r.name AS role, p.name AS parent_role
            FROM {$this->options['role_table_name']} as r
            LEFT JOIN {$this->options['role_table_name']} as p ON p.id = r.parent_id
QUERY;

        return $query;
    }
}

{'fraction_non_alphanumeric': 0.1415537738457285, 'fraction_numerical': 0.0, 'mean_word_length': 2.1189655172413793, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 31, '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': '1973232', 'n_tokens_mistral': 1066, 'n_tokens_neox': 1030, 'n_words': 209}