<commit_before><?php
namespace CtiDigital\Configurator\Model\Component;

use Symfony\Component\Yaml\Yaml;
use Magento\User\Model\UserFactory;
use Magento\Authorization\Model\RoleFactory;
use Magento\Framework\ObjectManagerInterface;
use CtiDigital\Configurator\Model\LoggingInterface;
use CtiDigital\Configurator\Model\Exception\ComponentException;

class AdminUsers extends YamlComponentAbstract
{
    protected $alias = 'adminusers';
    protected $name = 'Admin Users';
    protected $description = 'Component to create Admin Users';

    /**
     * Factory class for user model
     *
     * @var UserFactory
     */
    protected $userFactory;

    /**
     * RoleFactory
     *
     * @var roleFactory
     */
    protected $roleFactory;

    /**
     * AdminUsers constructor.
     * @param LoggingInterface $log
     * @param ObjectManagerInterface $objectManager
     * @param UserFactory $userFactory
     * @param RoleFactory $roleFactory
     */
    public function __construct(
        LoggingInterface $log,
        ObjectManagerInterface $objectManager,
        UserFactory $userFactory,
        RoleFactory $roleFactory
    ) {
        parent::__construct($log, $objectManager);

        $this->userFactory = $userFactory;
        $this->roleFactory = $roleFactory;
    }

    /**
     * @param data
     */
    protected function processData($data = null)
    {
        //Get Each Role
        foreach ($data['adminusers'] as $roleSet) {

            $roleName = $roleSet['rolename'];
            $roleId = $this->getUserRoleFromName($roleName);

            if ($roleId == null) {
                $this->log->logError(
                    sprintf('Admin Role "%s" does not exist', $roleName)
                );

                return;
            }

            //Run through users in this Role
            foreach ($roleSet['users'] as $userData) {

                $validData = $this->dataValidator($userData);
                try {
                    if (!$validData) {
                        return;
                    }

                    $this->createAdminUser($userData, $roleId);

                } catch (ComponentException $e) {
                    $this->log->logError($e->getMessage());
                }
            }
        }
    }

    /**
     * Create new Admin User
     *
     * @param $userData
     * @param $roleId
     */
    private function createAdminUser($userData, $roleId)
    {
        $user = $this->userFactory->create();
        $userCount = $user->getCollection()->addFieldToFilter('email', $userData['email'])->getSize();

        if ($userCount > 0) {
            $this->log->logInfo(
                sprintf(
                    'Admin User "%s" creation skipped: User with the email "%s" already exists',
                    $userData['firstname'] . ' ' . $userData['secondname'],
                    $userData['email']
                )
            );

            return;<commit_msg>Catch Magento's Validation Error<commit_after><?php
namespace CtiDigital\Configurator\Model\Component;

use Symfony\Component\Yaml\Yaml;
use Magento\User\Model\UserFactory;
use Magento\Authorization\Model\RoleFactory;
use Magento\Framework\ObjectManagerInterface;
use CtiDigital\Configurator\Model\LoggingInterface;
use CtiDigital\Configurator\Model\Exception\ComponentException;

class AdminUsers extends YamlComponentAbstract
{
    protected $alias = 'adminusers';
    protected $name = 'Admin Users';
    protected $description = 'Component to create Admin Users';

    /**
     * Factory class for user model
     *
     * @var UserFactory
     */
    protected $userFactory;

    /**
     * RoleFactory
     *
     * @var roleFactory
     */
    protected $roleFactory;

    /**
     * AdminUsers constructor.
     * @param LoggingInterface $log
     * @param ObjectManagerInterface $objectManager
     * @param UserFactory $userFactory
     * @param RoleFactory $roleFactory
     */
    public function __construct(
        LoggingInterface $log,
        ObjectManagerInterface $objectManager,
        UserFactory $userFactory,
        RoleFactory $roleFactory
    ) {
        parent::__construct($log, $objectManager);

        $this->userFactory = $userFactory;
        $this->roleFactory = $roleFactory;
    }

    /**
     * @param data
     */
    protected function processData($data = null)
    {
        //Get Each Role
        foreach ($data['adminusers'] as $roleSet) {

            $roleName = $roleSet['rolename'];
            $roleId = $this->getUserRoleFromName($roleName);

            if ($roleId == null) {
                $this->log->logError(
                    sprintf('Admin Role "%s" does not exist', $roleName)
                );

                return;
            }

            //Run through users in this Role
            foreach ($roleSet['users'] as $userData) {

                $validData = $this->dataValidator($userData);
                try {
                    if (!$validData) {
                        return;
                    }

                    $this->createAdminUser($userData, $roleId);

                } catch (\Magento\Framework\Validator\Exception $e) {
                    $this->log->logError(sprintf('Magento Framework Validation Exception: %s', $e->getMessage()));
                } catch (ComponentException $e) {
                    $this->log->logError($e->getMessage());
                }
            }
        }
    }

    /**
     * Create new Admin User
     *
     * @param $userData
     * @param $roleId
     */
    private function createAdminUser($userData, $roleId)
    {
        $user = $this->userFactory->create();
        $userCount = $user->getCollection()->addFieldToFilter('email', $userData['email'])->getSize();

        if ($userCount > 0) {
            $this->log->logComment(
                sprintf(
                    'Admin User "%s" creation skipped: User with the email "%s" already exists',
                    $userData['firstname'] . ' ' . $userData['secondname'],
                    $userData['email']
                )
            );

            return;
{'fraction_non_alphanumeric': 0.11764705882352941, 'fraction_numerical': 0.00032770768474520726, 'mean_word_length': 1.8738229755178908, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 42, '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': '4096916', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1582, 'n_words': 345}