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/farmfun/reserveren.farmfun.be/app/Komma/Shop/Orders/Kms/OrderMailService.php
<?php

namespace App\Komma\Shop\Orders\Kms;

use App\Komma\Auth\AuthMailService;
use App\Komma\Shop\Orders\Mail\OrderManualActionRequiredStaff;
use App\Komma\Shop\Orders\Mail\OrderStatusUpdatedCustomer;
use App\Komma\Shop\Orders\Mail\OrderStatusUpdatedStaff;
use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Users\Models\KmsUser;
use App\Komma\Users\Models\KmsUserRole;
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
 */
class OrderMailService implements OrderMailServiceInterface
{
    /**
     * 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 (__('shop/orders.mail.'.$order->status.'.customer') === 'shop/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 (__('shop/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'])->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 (__('shop/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));
        });
    }
}