<commit_before><?php

namespace App\Http\Controllers;

use App\Http\Requests\AlterAppointmentRequest;
use App\Models\Appointment;
use App\Services\ConciergeService;
use Notifynder;
use Widget;

/**
 * FOR REFACTOR:
 *     - Access auth()->ith constructor dependency injection
 *     - Access Appointments with Appointments repository injected dependency
 *     - Access Notifynder with constructor dependency
 *     - Move switches to proper responsibility class.
 */
class BookingController extends Controller
{
    /**
     * [$concierge description]
     *
     * @var [type]
     */
    private $concierge;

    /**
     * [__construct description]
     *
     * @param ConciergeService $concierge [description]
     */
    public function __construct(ConciergeService $concierge)
    {
        $this->concierge = $concierge;

        parent::__construct();
    }

    /**
     * post Action for booking.
     *
     * @param AlterAppointmentRequest $request
     *
     * @return JSON Action result object
     */
    public function postAction(AlterAppointmentRequest $request)
    {
        $this->log->info(__METHOD__);

        //////////////////
        // FOR REFACOTR //
        //////////////////

        $issuer = auth()->user();
        $businessId = $request->input('business');
        $appointment = Appointment::findOrFail($request->input('appointment'));
        $action = $request->input('action');
        $widgetType = $request->input('widget');

        ///////////////////////////////////
        // TODO: AUTHORIZATION GOES HERE //
        ///////////////////////////////////
        // AUTHORIZE:
        //  (A) auth()->user() is owner of $business
        // OR
        //  (B) auth()->user() is issuer of $appointment

        $this->log->info(sprintf(
            'postAction.request:[issuer:%s, action:%s, business:%s, appointment:%s]',
            $issuer->email,
            $action,
            $businessId,
            $appointment->id
        ));

        try {
            $appointment = $this->concierge->requestAction(auth()->user(), $appointment, $action);
        } catch (\Exception $e) {
            return response()->json(['code' => 'ERROR', 'html' => '']);
        }

        $contents = ['appointment' => $appointment, 'user' => auth()->user()];

        $viewKey = "widgets.appointment.{$widgetType}._body";
        if (!view()->exists($viewKey)) {
            return response()->json(['code' => 'ERROR', 'html' => '']);
        }
        
        // Widgets MUST be rendered before being returned on Response as they need to be interpreted as HTML
        $html = view($viewKey, $contents)->render();

        $this->log->info("postAction.response:[appointment:{$appointment->toJson()}]");
        return response()->json(['code' => 'OK', 'html' => $html]);
    }
}<commit_msg>[Style-CI] Enhance readability<commit_after><?php

namespace App\Http\Controllers;

use App\Http\Requests\AlterAppointmentRequest;
use App\Models\Appointment;
use App\Services\ConciergeService;
use Notifynder;
use Widget;

/**
 * FOR REFACTOR:
 *     - Access auth()->ith constructor dependency injection
 *     - Access Appointments with Appointments repository injected dependency
 *     - Access Notifynder with constructor dependency
 *     - Move switches to proper responsibility class.
 */
class BookingController extends Controller
{
    /**
     * Concierge service implementation.
     *
     * @var App\Services\ConciergeService
     */
    private $concierge;

    /**
     * Create controller.
     *
     * @param ConciergeService $concierge
     */
    public function __construct(ConciergeService $concierge)
    {
        $this->concierge = $concierge;

        parent::__construct();
    }

    /**
     * post Action for booking.
     *
     * @param AlterAppointmentRequest $request
     *
     * @return JSON Action result object
     */
    public function postAction(AlterAppointmentRequest $request)
    {
        $this->log->info(__METHOD__);

        //////////////////
        // FOR REFACOTR //
        //////////////////

        $issuer = auth()->user();
        $businessId = $request->input('business');
        $appointment = Appointment::findOrFail($request->input('appointment'));
        $action = $request->input('action');
        $widgetType = $request->input('widget');

        ///////////////////////////////////
        // TODO: AUTHORIZATION GOES HERE //
        ///////////////////////////////////
        // AUTHORIZE:
        //  (A) auth()->user() is owner of $business
        // OR
        //  (B) auth()->user() is issuer of $appointment

        $this->log->info(sprintf(
            'postAction.request:[issuer:%s, action:%s, business:%s, appointment:%s]',
            $issuer->email,
            $action,
            $businessId,
            $appointment->id
        ));

        try {
            $appointment = $this->concierge->requestAction(auth()->user(), $appointment, $action);
        } catch (\Exception $e) {
            return response()->json(['code' => 'ERROR', 'html' => '']);
        }

        $contents = ['appointment' => $appointment, 'user' => auth()->user()];

        $viewKey = "widgets.appointment.{$widgetType}._body";
        if (!view()->exists($viewKey)) {
            return response()->json(['code' => 'ERROR', 'html' => '']);
        }

        // Widgets MUST be rendered before being returned on Response as they need to be interpreted as HTML
        $html = view($viewKey, $contents)->render();

        $this->log->info("postAction.response:[appointment:{$appointment->toJson()}]");

        return response()->json(['code' => 'OK', 'html' => $html]);
    }
}
{'fraction_non_alphanumeric': 0.18354764840584817, 'fraction_numerical': 0.0, 'mean_word_length': 2.0775067750677505, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 73, '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': '4927507', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1546, 'n_words': 338}