Failed Conditions
Pull Request — master (#40)
by Viktor
09:39
created

src/Middleware/HasControllerMiddlewareTrait.php (2 issues)

Labels
Severity
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2017-2019 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\Middleware;
11
12
/**
13
 * Allow objects to have controller middleware.
14
 */
15
trait HasControllerMiddlewareTrait {
16
	/**
17
	 * Array of middleware.
18
	 *
19
	 * @var ControllerMiddleware[]
20
	 */
21
	protected $middleware = [];
22
23
	/**
24
	 * Get middleware.
25
	 *
26
	 * @param  string   $method
27
	 * @return string[]
28
	 */
29 1
	public function getMiddleware( $method ) {
30
		$middleware = array_filter( $this->middleware, function ( ControllerMiddleware $middleware ) use ( $method ) {
31 1
			return $middleware->appliesTo( $method );
32 1
		} );
33
34
		$middleware = array_map( function ( ControllerMiddleware $middleware ) {
35 1
			return $middleware->get();
36 1
		}, $middleware );
37
38 1
		if ( ! empty( $middleware ) ) {
39 1
			$middleware = call_user_func_array( 'array_merge', $middleware );
0 ignored issues
show
The function call_user_func_array was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
			$middleware = /** @scrutinizer ignore-call */ call_user_func_array( 'array_merge', $middleware );
Loading history...
40
		}
41
42 1
		return $middleware;
43
	}
44
45
	/**
46
	 * Add middleware.
47
	 *
48
	 * @param  string|string[]      $middleware
49
	 * @return ControllerMiddleware
50
	 */
51 1
	public function addMiddleware( $middleware ) {
52 1
		$controller_middleware = new ControllerMiddleware( $middleware );
53
54 1
		$this->middleware = array_merge(
55 1
			$this->middleware,
56 1
			[$controller_middleware]
57
		);
58
59 1
		return $controller_middleware;
60
	}
61
62
	/**
63
	 * Fluent alias for addMiddleware().
64
	 *
65
	 * @codeCoverageIgnore
66
	 * @param  string|string[]      $middleware
67
	 * @return ControllerMiddleware
68
	 */
69
	public function middleware( $middleware ) {
70
		return call_user_func_array( [$this, 'addMiddleware'], func_get_args() );
0 ignored issues
show
The function call_user_func_array was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
		return /** @scrutinizer ignore-call */ call_user_func_array( [$this, 'addMiddleware'], func_get_args() );
Loading history...
71
	}
72
}
73