<commit_before><?php
/*
 * This file is part of the Sulu CMS.
 *
 * (c) MASSIVE ART WebServices GmbH
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Sulu\Bundle\Sales\CoreBundle\Manager;

use Sulu\Bundle\Sales\CoreBundle\Exceptions\MissingAttributeException;

abstract class OrderAddressManager
{
    /**
     * Returns the entry from the data with the given key, or the given default value, if the key does not exist
     * @param array $data
     * @param string $key
     * @param string $default
     * @return mixed
     */
    protected function getProperty(array $data, $key, $default = null)
    {
        return array_key_exists($key, $data) ? $data[$key] : $default;
    }

    /**
     * @param OrderAddress $orderAddress
     * @param $addressData
     * @param Contact $contact
     * @param Account|null $account
     * @throws OrderDependencyNotFoundException
     */
    protected function setOrderAddress(OrderAddress $orderAddress, $addressData, $contact = null, $account = null)
    {
        // check if address with id can be found

        $contactData = $this->getContactData($addressData, $contact);
        // add contact data
        $orderAddress->setFirstName($contactData['firstName']);
        $orderAddress->setLastName($contactData['lastName']);
        if (isset($contactData['title'])) {
            $orderAddress->setTitle($contactData['title']);
        }
        if (isset($contactData['salutation'])) {
            $orderAddress->setSalutation($contactData['salutation']);
        }

        // add account data
        if ($account) {
            $orderAddress->setAccountName($account->getName());
            $orderAddress->setUid($account->getUid());
        } else {
            $orderAddress->setAccountName(null);
            $orderAddress->setUid(null);
        }

        // TODO: add phone

        $this->setAddressDataForOrder($orderAddress, $addressData);
    }

    /**
     * @param Address $address
     * @param $contact
     * @param $account
     * @return OrderAddress
     */
    public function getOrderAddressByContactAddress(Address $address, $contact, $account)
    {
        $orderAddress = new OrderAddress();
        $orderAddress->setStreet($address->getStreet());
        $orderAddress->setNumber($address->getNumber());
        $orderAddress->setAddition($address->getAddition());
        $orderAddress->setCity($address->getCity());
        $orderAddress->setZip($address->getZip());
        $orderAddress->setState($address->getState());
        $orderAddress->setCountry($address->getCountry()->getName());

        $orderAddress->setPostboxCity($address->getPostboxCity());
        $orderAddress->setPostboxPostcode($address->getPostboxPostcode());
        $orderAddress->setPostboxNumber($address->getPostboxNumber());

        // add account data
        if ($account) {
            $orderAddress->setAccountName($account->getName());
            $orderAddress->setUid($account->getUid());
        }

        if ($contact) {
            if ($contact->getTitle()) {
                $orderAddress->setTitle($contact->getTitle()->getTitle());
            }
            $orderAddress->setSalutation($contact->getSalutation());
            $orderAddress->setFirstName($contact->getFirstName());
            $orderAddress->setLastName($contact->getLastName());
            $orderAddress->setEmail($contact->getMainEmail());
            $orderAddress->setPhone($contact->getMainPhone());
        }

        return $orderAddress;
    }

    /**
     * copies address data to order address
     * @param OrderAddress $orderAddress
     * @param $addressData
     */
    private function setAddressDataForOrder(OrderAddress &$orderAddress, $addressData)
    {
        $orderAddress->setStreet($this->getProperty($addressData, 'street', ''));
        $orderAddress->setNumber($this->getProperty($addressData, 'number', ''));
        $orderAddress->setAddition($this->getProperty($addressData, 'addition', ''));
        $orderAddress->setCity($this->getProperty($addressData, 'city', ''));
        $orderAddress->setZip($this->getProperty($addressData, 'zip', ''));
        $orderAddress->setState($this->getProperty($addressData, 'state', ''));
        $orderAddress->setCountry($this->getProperty($addressData, 'country', ''));
        $orderAddress->setEmail($this->getProperty($addressData, 'email', ''));
        $orderAddress->setPhone($this->getProperty($addressData, 'phone', ''));

        $orderAddress->setPostboxCity($this->getProperty($addressData, 'postboxCity', ''));
        $orderAddress->setPostboxPostcode($this->getProperty($addressData, 'postboxPostcode', ''));
        $orderAddress->setPostboxNumber($this->getProperty($addressData, 'postboxNumber', ''));
    }

    /**
     * returns contact data as an array. either by provided address or contact
     *
     * @param $addressData
     * @param $contact
     * @return array
     * @throws MissingAttributeException
     */<commit_msg>fixes in order-address-manager<commit_after><?php
/*
 * This file is part of the Sulu CMS.
 *
 * (c) MASSIVE ART WebServices GmbH
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Sulu\Bundle\Sales\CoreBundle\Manager;

use Sulu\Bundle\Sales\CoreBundle\Exceptions\MissingAttributeException;

class OrderAddressManager
{
    protected static $orderAddressEntityName = 'SuluSalesCoreBundle:OrderAddress';
    protected static $orderAddressEntity = 'Sulu\Bundle\Sales\CoreBundle\Entity\OrderAddress';

    /**
     * Returns the entry from the data with the given key, or the given default value, if the key does not exist
     * @param array $data
     * @param string $key
     * @param string $default
     * @return mixed
     */
    protected function getProperty(array $data, $key, $default = null)
    {
        return array_key_exists($key, $data) ? $data[$key] : $default;
    }

    /**
     * @param OrderAddress $orderAddress
     * @param $addressData
     * @param Contact $contact
     * @param Account|null $account
     * @throws OrderDependencyNotFoundException
     */
    protected function setOrderAddress($orderAddress, $addressData, $contact = null, $account = null)
    {
        // check if address with id can be found

        $contactData = $this->getContactData($addressData, $contact);
        // add contact data
        $orderAddress->setFirstName($contactData['firstName']);
        $orderAddress->setLastName($contactData['lastName']);
        if (isset($contactData['title'])) {
            $orderAddress->setTitle($contactData['title']);
        }
        if (isset($contactData['salutation'])) {
            $orderAddress->setSalutation($contactData['salutation']);
        }

        // add account data
        if ($account) {
            $orderAddress->setAccountName($account->getName());
            $orderAddress->setUid($account->getUid());
        } else {
            $orderAddress->setAccountName(null);
            $orderAddress->setUid(null);
        }

        // TODO: add phone

        $this->setAddressDataForOrder($orderAddress, $addressData);
    }

    /**
     * @param Address $address
     * @param $contact
     * @param $account
     * @return OrderAddress
     */
    public function getOrderAddressByContactAddress(Address $address, $contact, $account)
    {
        $orderAddress = new static::$orderAddressEntity;
        $orderAddress->setStreet($address->getStreet());
        $orderAddress->setNumber($address->getNumber());
        $orderAddress->setAddition($address->getAddition());
        $orderAddress->setCity($address->getCity());
        $orderAddress->setZip($address->getZip());
        $orderAddress->setState($address->getState());
        $orderAddress->setCountry($address->getCountry()->getName());

        $orderAddress->setPostboxCity($address->getPostboxCity());
        $orderAddress->setPostboxPostcode($address->getPostboxPostcode());
        $orderAddress->setPostboxNumber($address->getPostboxNumber());

        // add account data
        if ($account) {
            $orderAddress->setAccountName($account->getName());
            $orderAddress->setUid($account->getUid());
        }

        if ($contact) {
            if ($contact->getTitle()) {
                $orderAddress->setTitle($contact->getTitle()->getTitle());
            }
            $orderAddress->setSalutation($contact->getSalutation());
            $orderAddress->setFirstName($contact->getFirstName());
            $orderAddress->setLastName($contact->getLastName());
            $orderAddress->setEmail($contact->getMainEmail());
            $orderAddress->setPhone($contact->getMainPhone());
        }

        return $orderAddress;
    }

    /**
     * copies address data to order address
     * @param OrderAddress $orderAddress
     * @param $addressData
     */
    private function setAddressDataForOrder(&$orderAddress, $addressData)
    {
        $orderAddress->setStreet($this->getProperty($addressData, 'street', ''));
        $orderAddress->setNumber($this->getProperty($addressData, 'number', ''));
        $orderAddress->setAddition($this->getProperty($addressData, 'addition', ''));
        $orderAddress->setCity($this->getProperty($addressData, 'city', ''));
        $orderAddress->setZip($this->getProperty($addressData, 'zip', ''));
        $orderAddress->setState($this->getProperty($addressData, 'state', ''));
        $orderAddress->setCountry($this->getProperty($addressData, 'country', ''));
        $orderAddress->setEmail($this->getProperty($addressData, 'email', ''));
        $orderAddress->setPhone($this->getProperty($addressData, 'phone', ''));

        $orderAddress->setPostboxCity($this->getProperty($addressData, 'postboxCity', ''));
        $orderAddress->setPostboxPostcode($this->getProperty($addressData, 'postboxPostcode', ''));
        $orderAddress->setPostboxNumber($this->getProperty($addressData, 'postboxNumber', ''));
    }

    /**
     * returns contact data as an array. either by provided address or contact
     *
     * @param $addressData
     * @param $contact
     * @return array
     * @throws MissingAttributeException
     */
{'fraction_non_alphanumeric': 0.14864864864864866, 'fraction_numerical': 0.0, 'mean_word_length': 3.1246992782678427, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 153, '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': '4868178', 'n_tokens_mistral': 2758, 'n_tokens_neox': 2702, 'n_words': 509}