master

laravel/framework

Last updated at: 29/12/2023 09:23

ReportableHandler.php

TLDR

This file contains the ReportableHandler class, which is responsible for handling and reporting exceptions. It has methods to invoke the handler, determine if the handler can handle a given exception, and indicate if the handling should stop after invoking the handler.

Methods

__construct

Creates a new instance of the ReportableHandler class. It takes a callable parameter representing the callback function to be invoked when handling an exception.

__invoke

Invokes the callback function associated with the handler and passes the exception as a parameter. Returns a boolean value indicating if the handling should continue or not.

handles

Determines if the callback function can handle the given exception. It checks if the exception is of any of the types specified in the callback function's first closure parameter.

stop

Indicates that the handling should stop after invoking the callback function. Returns the current instance of the ReportableHandler class.

<?php

namespace Illuminate\Foundation\Exceptions;

use Illuminate\Support\Traits\ReflectsClosures;
use Throwable;

class ReportableHandler
{
    use ReflectsClosures;

    /**
     * The underlying callback.
     *
     * @var callable
     */
    protected $callback;

    /**
     * Indicates if reporting should stop after invoking this handler.
     *
     * @var bool
     */
    protected $shouldStop = false;

    /**
     * Create a new reportable handler instance.
     *
     * @param  callable  $callback
     * @return void
     */
    public function __construct(callable $callback)
    {
        $this->callback = $callback;
    }

    /**
     * Invoke the handler.
     *
     * @param  \Throwable  $e
     * @return bool
     */
    public function __invoke(Throwable $e)
    {
        $result = call_user_func($this->callback, $e);

        if ($result === false) {
            return false;
        }

        return ! $this->shouldStop;
    }

    /**
     * Determine if the callback handles the given exception.
     *
     * @param  \Throwable  $e
     * @return bool
     */
    public function handles(Throwable $e)
    {
        foreach ($this->firstClosureParameterTypes($this->callback) as $type) {
            if (is_a($e, $type)) {
                return true;
            }
        }

        return false;
    }

    /**
     * Indicate that report handling should stop after invoking this callback.
     *
     * @return $this
     */
    public function stop()
    {
        $this->shouldStop = true;

        return $this;
    }
}