<commit_before><?php
namespace Oro\Bundle\TestFrameworkBundle\Fixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

use Oro\Bundle\FlexibleEntityBundle\Model\AbstractAttributeOption;
use Oro\Bundle\FlexibleEntityBundle\Model\AbstractAttribute;
use Oro\Bundle\FlexibleEntityBundle\Model\AbstractFlexible;
use Oro\Bundle\FlexibleEntityBundle\Model\FlexibleValueInterface;

use Oro\Bundle\UserBundle\Entity\UserApi;
use Oro\Bundle\UserBundle\Entity\UserManager;

class LoadUserData extends AbstractFixture implements ContainerAwareInterface, OrderedFixtureInterface
{
    /**
     * @var ContainerInterface
     */
    private $container;

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    public function load(ObjectManager $manager)
    {
        /** @var UserManager $userManager */
        $userManager = $this->container->get('oro_user.manager');

        $this->loadAttributes($userManager);

        $admin = $userManager->createUser();
        $api   = new UserApi();
        $role  = $manager
            ->getRepository('OroUserBundle:Role')
            ->findOneBy(array('role' => 'ROLE_SUPER_ADMIN'));
        $group = $manager
            ->getRepository('OroUserBundle:Group')
            ->findOneBy(array('name' => 'Administrators'));

        $api->setApiKey('admin_api_key')
            ->setUser($admin);

        $admin
            ->setUsername('admin')
            ->setPlainPassword('<PASSWORD>')
            ->setFirstname('John')
            ->setLastname('Doe')
            ->setEmail('<EMAIL>')
            ->setApi($api)
            ->addRole($role)
            ->addGroup($group);

        if ($this->hasReference('default_business_unit')) {
            $admin->setOwner($this->getReference('default_business_unit'));
        }

        $this->setFlexibleAttributeValueOption($userManager, $admin, 'gender', 0);
        $this->setFlexibleAttributeValue($userManager, $admin, 'company', '');
        $this->addReference('default_user', $admin);

        $userManager->updateUser($admin);
    }

    /**
     * Load attributes
     *
     * @param UserManager $entityManager
     * @return void
     */
    public function loadAttributes($entityManager)
    {
        $this->assertHasRequiredAttributes($entityManager, array('company', 'gender'));
    }

    /**
     * Asserts required attributes were created
     *
     * @param UserManager $entityManager
     * @param array $attributeCodes
     * @throws \LogicException
     */
    private function assertHasRequiredAttributes($entityManager, $attributeCodes)
    {
        foreach ($attributeCodes as $attributeCode) {
            if (!$this->findAttribute($entityManager, $attributeCode)) {
                throw new \LogicException(<commit_msg>fixture for User<commit_after><?php
namespace Oro\Bundle\TestFrameworkBundle\Fixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

use Oro\Bundle\FlexibleEntityBundle\Model\AbstractAttributeOption;
use Oro\Bundle\FlexibleEntityBundle\Model\AbstractAttribute;
use Oro\Bundle\FlexibleEntityBundle\Model\AbstractFlexible;
use Oro\Bundle\FlexibleEntityBundle\Model\FlexibleValueInterface;

use Oro\Bundle\UserBundle\Entity\UserApi;
use Oro\Bundle\UserBundle\Entity\UserManager;

class LoadUserData extends AbstractFixture implements ContainerAwareInterface, OrderedFixtureInterface
{
    /**
     * @var ContainerInterface
     */
    private $container;

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    public function load(ObjectManager $manager)
    {
        /** @var UserManager $userManager */
        $userManager = $this->container->get('oro_user.manager');

        $this->loadAttributes($userManager);

        $admin = $userManager->createUser();
        $api   = new UserApi();
        $role  = $manager
            ->getRepository('OroUserBundle:Role')
            ->findOneBy(array('role' => 'ROLE_SUPER_ADMIN'));
        $group = $manager
            ->getRepository('OroUserBundle:Group')
            ->findOneBy(array('name' => 'Administrators'));

        $api->setApiKey('admin_api_key')
            ->setUser($admin);

        $admin
            ->setUsername('admin')
            ->setPlainPassword('<PASSWORD>')
            ->setFirstname('John')
            ->setLastname('Doe')
            ->setEmail('<EMAIL>')
            ->setApi($api)
            ->addRole($role)
            ->addGroup($group);

        if ($this->hasReference('default_business_unit')) {
            $admin->setOwner($this->getReference('default_business_unit'));
        }

        //$this->setFlexibleAttributeValueOption($userManager, $admin, 'gender', 0);
        //$this->setFlexibleAttributeValue($userManager, $admin, 'company', '');
        $this->addReference('default_user', $admin);

        $userManager->updateUser($admin);
    }

    /**
     * Load attributes
     *
     * @param UserManager $entityManager
     * @return void
     */
    public function loadAttributes($entityManager)
    {
        $this->assertHasRequiredAttributes($entityManager, array('company', 'gender'));
    }

    /**
     * Asserts required attributes were created
     *
     * @param UserManager $entityManager
     * @param array $attributeCodes
     * @throws \LogicException
     */
    private function assertHasRequiredAttributes($entityManager, $attributeCodes)
    {
        foreach ($attributeCodes as $attributeCode) {
            if (!$this->findAttribute($entityManager, $attributeCode)) {
                throw new \LogicException(
{'fraction_non_alphanumeric': 0.12489755777741354, 'fraction_numerical': 0.00032781511227667593, 'mean_word_length': 3.125760649087221, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 67, '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': '4738598', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1640, 'n_words': 265}