HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Orders/Kms/OrderMailService.php
<?php


namespace App\Orders\Kms;

use App\Orders\Mail\OrderManualActionRequiredStaff;
use App\Orders\Mail\OrderStatusUpdatedCustomer;
use App\Orders\Mail\OrderStatusUpdatedStaff;
use App\Orders\Models\Order;
use Illuminate\Support\Facades\Lang;
use Komma\KMS\Users\Models\KmsUserRole;
use Komma\KMS\Users\Models\KmsUser;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

/**
 * Class OrderMailService
 *
 * A service responsible for sending mails about orders to staff and customers
 *
 * @see AuthMailService
 * @package App\Orders\Kms
 */
class OrderMailService
{
    /**
     * Mail a customer about their current order status.
     *
     * @param Order $order
     * @param bool $failSilentlyWhenTranslationsMissing When false a proper exception is thrown when the mail translations don't exist. When true, the mail just does not get sent.
     */
    public function mailCustomerAboutCurrentOrderStatus(Order $order, bool $failSilentlyWhenTranslationsMissing = false)
    {
        $order->load(['customer']);

        //Verify translations
        if (!Lang::has('KMS::orders.mail.' . $order->status . '.customer')) {
            //Translations do not exist.
            if(!$failSilentlyWhenTranslationsMissing) throw new \RuntimeException('Could not build a mailable for a customers order with an order status of: ' . $order->status . '. The translations are not present. Most likely by design.');
            return;
        }

        Log::debug('OrderMailService: Notifying customer with email address "'.$order->customer->email.'" (shipping email) about order status change.');
        Mail::to($order->customer->email)->send(new OrderStatusUpdatedCustomer($order));
    }

    /**
     * Mail Staff about an order's current status.
     *
     * @param Order $order
     * @param bool $failSilentlyWhenTranslationsMissing When false a proper exception is thrown when the mail translations don't exist. When true, the mail just does not get sent.
     */
    public function mailStaffAboutCurrentOrderStatus(Order $order, bool $failSilentlyWhenTranslationsMissing = false)
    {
        $order->load(['customer']);

        //Verify translations
        if (__('KMS::orders.mail.' . $order->status . '.staff') === 'shop/orders.mail.' . $order->status . '.staff') {
            //Translations do not exist.
            if(!$failSilentlyWhenTranslationsMissing) throw new \RuntimeException('Could not build a mailable for a customers order with an order status of: ' . $order->status . '. The translations are not present. Most likely by design.');
            return;
        }

        KmsUser::whereIn('role', [KmsUserRole::Admin])->get(['id', 'email', 'first_name', 'last_name'])->each(function(KmsUser $staffMember) use($order) {
            Log::debug('OrderMailService: Notifying staffMember with email address "'.$staffMember->email.'" about order status change.');
            Mail::to($staffMember->email)->send(new OrderStatusUpdatedStaff($order, $staffMember));
        });
    }

    /**
     * Mail Staff that an order needs manual handling. Usually because
     *
     * @param Order $order
     * @param bool $failSilentlyWhenTranslationsMissing When false a proper exception is thrown when the mail translations don't exist. When true, the mail just does not get sent.
     */
    public function mailStaffAboutManualActionOnOrder(Order $order, bool $failSilentlyWhenTranslationsMissing = false)
    {
        $order->load(['customer']);

        //Verify translations
        if (__('KMS::orders.mail.manual_action.staff') === 'shop/orders.mail.manual_action.staff') {
            //Translations do not exist.
            if(!$failSilentlyWhenTranslationsMissing) throw new \RuntimeException('Could not build a mailable for a customers order with an order status of: ' . $order->status . '. The translations are not present.');
            return;
        }

        KmsUser::whereIn('role', [KmsUserRole::Admin])->get(['id', 'email'])->each(function(KmsUser $staffMember) use($order) {
            Log::debug('OrderMailService: Notifying staffMember with email address "'.$staffMember->email.'" that that order with id "'.$order->id.' needs a manual action".');
            Mail::to($staffMember->email)->send(new OrderManualActionRequiredStaff($order, $staffMember));
        });
    }


}