<commit_before><?php

namespace ShopwareAdapter\ResponseParser\Customer;

use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use InvalidArgumentException;
use PlentyConnector\Connector\IdentityService\IdentityServiceInterface;
use PlentyConnector\Connector\TransferObject\CustomerGroup\CustomerGroup;
use PlentyConnector\Connector\TransferObject\Language\Language;
use PlentyConnector\Connector\TransferObject\Order\Customer\Customer;
use PlentyConnector\Connector\TransferObject\Shop\Shop;
use Psr\Log\LoggerInterface;
use Shopware\Models\Customer\Customer as CustomerModel;
use Shopware\Models\Customer\Group as GroupModel;
use Shopware\Models\Newsletter\Address;
use Shopware\Models\Shop\Repository;
use Shopware\Models\Shop\Shop as ShopModel;
use ShopwareAdapter\ShopwareAdapter;

/**
 * Class CustomerResponseParser
 */
class CustomerResponseParser implements CustomerResponseParserInterface
{
    /**
     * @var IdentityServiceInterface
     */
    private $identityService;

    /**
     * @var EntityManagerInterface
     */
    private $entityManager;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * CustomerResponseParser constructor.
     *
     * @param IdentityServiceInterface $identityService
     * @param EntityManagerInterface   $entityManager
     * @param LoggerInterface          $logger
     */
    public function __construct(
        IdentityServiceInterface $identityService,
        EntityManagerInterface $entityManager,
        LoggerInterface $logger
    ) {
        $this->identityService = $identityService;
        $this->entityManager = $entityManager;
        $this->logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function parse(array $entry)
    {
        $entry['salutation'] = strtolower($entry['salutation']);

        $shopIdentifier = $this->getIdentifier((string) $entry['shopId'], Shop::TYPE);

        $languageIdentifier = $this->getLanguageIdentifier($entry);
        if (null === $languageIdentifier) {
            $this->logger->warning('no customer language found');

            return null;
        }

        $customerGroup = $this->getCustomerGroup($entry);
        if (null === $customerGroup) {
            $this->logger->warning('no customer group found');

            return null;
        }

        $customerGroupIdentifier = $this->getIdentifier((string) $customerGroup->getId(), CustomerGroup::TYPE);

        if ($entry['salutation'] === 'mr') {
            $gender = Customer::GENDER_MALE;
        } elseif ($entry['salutation'] === 'ms') {
            $gender = Customer::GENDER_FEMALE;
        } else {
            $gender = null;
        }

        if (empty($entry['birthday'])) {
            $birthday = null;
        } else {
            $birthday = DateTimeImmutable::createFromMutable($entry['birthday']);

            if (!($birthday instanceof DateTimeImmutable)) {
                $birthday = null;
            }
        }

        if (empty($entry['title'])) {
            $entry['title'] = null;
        }

        $customer = new Customer();
        $customer->setBirthday($birthday);
        $customer->setType($this->getCustomerTypeId($entry['accountMode']));
        $customer->setEmail($entry['email']);
        $customer->setFirstname($entry['firstname']);
        $customer->setLastname($entry['lastname']);
        $customer->setNumber($entry['number']);
        $customer->setGender($gender);
        $customer->setTitle($entry['title']);
        $customer->setShopIdentifier($shopIdentifier);<commit_msg>Add this to fix the gender problem (#315)<commit_after><?php

namespace ShopwareAdapter\ResponseParser\Customer;

use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use InvalidArgumentException;
use PlentyConnector\Connector\IdentityService\IdentityServiceInterface;
use PlentyConnector\Connector\TransferObject\CustomerGroup\CustomerGroup;
use PlentyConnector\Connector\TransferObject\Language\Language;
use PlentyConnector\Connector\TransferObject\Order\Customer\Customer;
use PlentyConnector\Connector\TransferObject\Shop\Shop;
use Psr\Log\LoggerInterface;
use Shopware\Models\Customer\Customer as CustomerModel;
use Shopware\Models\Customer\Group as GroupModel;
use Shopware\Models\Newsletter\Address;
use Shopware\Models\Shop\Repository;
use Shopware\Models\Shop\Shop as ShopModel;
use ShopwareAdapter\ShopwareAdapter;

/**
 * Class CustomerResponseParser
 */
class CustomerResponseParser implements CustomerResponseParserInterface
{
    /**
     * @var IdentityServiceInterface
     */
    private $identityService;

    /**
     * @var EntityManagerInterface
     */
    private $entityManager;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * CustomerResponseParser constructor.
     *
     * @param IdentityServiceInterface $identityService
     * @param EntityManagerInterface   $entityManager
     * @param LoggerInterface          $logger
     */
    public function __construct(
        IdentityServiceInterface $identityService,
        EntityManagerInterface $entityManager,
        LoggerInterface $logger
    ) {
        $this->identityService = $identityService;
        $this->entityManager = $entityManager;
        $this->logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function parse(array $entry)
    {
        $entry['salutation'] = strtolower($entry['salutation']);

        $shopIdentifier = $this->getIdentifier((string) $entry['shopId'], Shop::TYPE);

        $languageIdentifier = $this->getLanguageIdentifier($entry);
        if (null === $languageIdentifier) {
            $this->logger->warning('no customer language found');

            return null;
        }

        $customerGroup = $this->getCustomerGroup($entry);
        if (null === $customerGroup) {
            $this->logger->warning('no customer group found');

            return null;
        }

        $customerGroupIdentifier = $this->getIdentifier((string) $customerGroup->getId(), CustomerGroup::TYPE);

        if ($entry['salutation'] === 'mr' || $entry['salutation'] === 'herr') {
            $gender = Customer::GENDER_MALE;
        } elseif ($entry['salutation'] === 'ms' || $entry['salutation'] === 'frau') {
            $gender = Customer::GENDER_FEMALE;
        } else {
            $gender = null;
        }

        if (empty($entry['birthday'])) {
            $birthday = null;
        } else {
            $birthday = DateTimeImmutable::createFromMutable($entry['birthday']);

            if (!($birthday instanceof DateTimeImmutable)) {
                $birthday = null;
            }
        }

        if (empty($entry['title'])) {
            $entry['title'] = null;
        }

        $customer = new Customer();
        $customer->setBirthday($birthday);
        $customer->setType($this->getCustomerTypeId($entry['accountMode']));
        $customer->setEmail($entry['email']);
        $customer->setFirstname($entry['firstname']);
        $customer->setLastname($entry['lastname']);
        $customer->setNumber($entry['number']);
        $customer->setGender($gender);
        $customer->setTitle($entry['title']);
        $customer->setShopIdentifier($shopIdentifier);
{'fraction_non_alphanumeric': 0.12983273923772964, 'fraction_numerical': 0.0004112969564025226, 'mean_word_length': 3.0527777777777776, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '3022517', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1878, 'n_words': 338}