Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
RouteActions
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __call
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 beforeAction
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 afterAction
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Routing Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\Routing;
11
12use BadMethodCallException;
13
14/**
15 * Class RouteActions.
16 *
17 * @package routing
18 */
19abstract class RouteActions
20{
21    /**
22     * @param string $method
23     * @param array<int,mixed> $arguments
24     *
25     * @return mixed
26     */
27    public function __call(string $method, array $arguments) : mixed
28    {
29        if ($method === 'beforeAction') {
30            return $this->beforeAction(...$arguments);
31        }
32        if ($method === 'afterAction') {
33            return $this->afterAction(...$arguments);
34        }
35        $class = static::class;
36        if (\method_exists($this, $method)) {
37            throw new BadMethodCallException(
38                "Action method not allowed: {$class}::{$method}"
39            );
40        }
41        throw new BadMethodCallException("Action method not found: {$class}::{$method}");
42    }
43
44    /**
45     * Runs just before the class action method and after the constructor.
46     *
47     * Used to prepare settings, filter input data, acts as a middleware between
48     * the routing and the class action method.
49     *
50     * @param string $method The action method name
51     * @param array<int,string> $arguments The action method arguments
52     *
53     * @return mixed Returns a response (any value, except null) to prevent the
54     * route action execution or null to continue the process and call the
55     * action method
56     */
57    protected function beforeAction(string $method, array $arguments) : mixed
58    {
59        // Prepare or intercept...
60        return null;
61    }
62
63    /**
64     * Runs just after the class action method and before the destructor.
65     *
66     * Used to finalize settings, filter output data, acts as a middleware between
67     * the action method and the final response.
68     *
69     * @param string $method The action method name
70     * @param array<int,string> $arguments The action method arguments
71     * @param bool $ran Indicates if the class action method was executed, true
72     * if it was not intercepted by the beforeAction method
73     * @param mixed $result The returned value directly from beforeAction or
74     * from the class action method, if it was executed
75     *
76     * @see RouteActions::beforeAction()
77     *
78     * @return mixed
79     */
80    protected function afterAction(
81        string $method,
82        array $arguments,
83        bool $ran,
84        mixed $result
85    ) : mixed {
86        return $result;
87    }
88}