<?php
    namespace MNM\Spitzner\Observer;

    use Magento\Customer\Api\CustomerRepositoryInterface;
    use Magento\Framework\Event\ObserverInterface;

    class AddEmailVariables implements ObserverInterface
    {
        protected $customerRepository;

        public function __construct(CustomerRepositoryInterface $customerRepository)
        {
            $this->customerRepository = $customerRepository;
        }

        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            /** @var \Magento\Framework\App\Action\Action $controller */
            $transport = $observer->getEvent()->getTransport();

            if($transport->getOrder() != null)
            {
                $order =        $transport->getOrder();
                $customer =     null;
                if($order->getCustomerId())
                {
                    $customer = $this->customerRepository->getById($transport->getOrder()->getCustomerId());
                }

                /*if ($customer->getCustomAttribute('username'))
                {
                    $transport['username'] = $customer->getCustomAttribute('username')->getValue();
                }*/

                //order date
                $transport['order_date_long'] = $order->getCreatedAtFormatted(\IntlDateFormatter::LONG);

                //payment extra text
                $payment_method = $order->getPayment()->getMethodInstance();
                $paymentText = '';
                switch($payment_method->getTitle())
                {
                    case 'Rechnung':    $paymentText = 'Der Rechnungsbetrag wird sofort nach Erhalt der Rechnung fällig, per Überweisung auf das in der Rechnung angegebene Bankkonto. Alle Preise verstehen sich inklusive der gesetzlich gültigen MwSt.';
                                        break;
                    case 'Paypal':      $paymentText = 'Nicht skontierbar.';
                                        break;
                    case 'Bankeinzug':
                    case 'Bankeinzug / Lastschrift':
                    case 'Bankeinzug (3% Skonto)':
                                        $paymentText = 'Wir gewähren Ihnen 3% Skonto.';
                                        break;
                }
                $transport['payment_text'] = $paymentText;

                //customer number
                $transport['customer_number_text'] = '';
                if($customer)
                {
                    $customerNumber = $customer->getCustomAttribute('customer_number');

                    if($customerNumber)
                    {
                        $transport['customer_number_text'] = "<strong>Kundennummer:</strong> " . $customerNumber->getValue();
                    }
                }

                //order
                $transport['order_id'] = $order->getId();
            }
        }
    }