<commit_before><?php

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

use Sonata\Component\Customer\AddressManagerInterface;
use Sonata\Component\Customer\AddressInterface;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;

class AddressManager implements AddressManagerInterface
{
    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * @var EntityRepository
     */
    protected $repository;

    /**
     * @var string
     */
    protected $class;

    /**
     * @param EntityManager $em
     * @param string        $class
     */
    public function __construct(EntityManager $em, $class)
    {
        $this->em    = $em;
        $this->class = $class;

        if (class_exists($class)) {
            $this->repository = $this->em->getRepository($class);
        }
    }

    /**
     * Creates an empty Address instance
     *
     * @return Address
     */
    public function create()
    {
        $class = $this->class;

        return new $class;
    }

    /**
     * Updates an address
     *
     * @param  AddressInterface $address
     * @return void
     */
    public function save(AddressInterface $address)
    {
        $this->em->persist($address);
        $this->em->flush();
    }

    /**
     * Returns the address's fully qualified class name
     *
     * @return string
     */
    public function getClass()
    {
        return $this->class;
    }

    /**
     * Finds one address by the given criteria
     *
     * @param  array   $criteria
     * @return Address
     */
    public function findBy(array $criteria)
    {
        return $this->repository->findBy($criteria);
    }

    public function findOneBy(array $criteria)
    {
        return $this->repository->findOneBy($criteria);
    }

    /**
     * {@inheritdoc}
     */
    public function setCurrent(AddressInterface $address)
    {
        foreach ($address->getCustomer()->getAddressesByType(AddressInterface::TYPE_DELIVERY) as $custAddress) {
            if ($custAddress->getCurrent()) {
                $custAddress->setCurrent(false);
                $this->save($custAddress);
                break;
            }
        }

        $address->setCurrent(true);
        $this->save($address);
    }

    /**
     * Deletes an address
     *
     * @param  AddressInterface $address<commit_msg>SONATA-203 - Fix addresses to be setted as default for each type<commit_after><?php

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

use Sonata\Component\Customer\AddressManagerInterface;
use Sonata\Component\Customer\AddressInterface;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;

class AddressManager implements AddressManagerInterface
{
    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * @var EntityRepository
     */
    protected $repository;

    /**
     * @var string
     */
    protected $class;

    /**
     * @param EntityManager $em
     * @param string        $class
     */
    public function __construct(EntityManager $em, $class)
    {
        $this->em    = $em;
        $this->class = $class;

        if (class_exists($class)) {
            $this->repository = $this->em->getRepository($class);
        }
    }

    /**
     * Creates an empty Address instance
     *
     * @return Address
     */
    public function create()
    {
        $class = $this->class;

        return new $class;
    }

    /**
     * Updates an address
     *
     * @param  AddressInterface $address
     * @return void
     */
    public function save(AddressInterface $address)
    {
        $this->em->persist($address);
        $this->em->flush();
    }

    /**
     * Returns the address's fully qualified class name
     *
     * @return string
     */
    public function getClass()
    {
        return $this->class;
    }

    /**
     * Finds one address by the given criteria
     *
     * @param  array   $criteria
     * @return Address
     */
    public function findBy(array $criteria)
    {
        return $this->repository->findBy($criteria);
    }

    public function findOneBy(array $criteria)
    {
        return $this->repository->findOneBy($criteria);
    }

    /**
     * {@inheritdoc}
     */
    public function setCurrent(AddressInterface $address)
    {
        foreach ($address->getCustomer()->getAddressesByType($address->getType()) as $custAddress) {
            if ($custAddress->getCurrent()) {
                $custAddress->setCurrent(false);
                $this->save($custAddress);
                break;
            }
        }

        $address->setCurrent(true);
        $this->save($address);
    }

    /**
     * Deletes an address
     *
     * @param  AddressInterface $address
{'fraction_non_alphanumeric': 0.12121803630685145, 'fraction_numerical': 0.0005855943782939684, 'mean_word_length': 2.0445632798573974, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 54, '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': '3516036', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1429, 'n_words': 339}