AbstractEventDispatcherPipe::fireEventOn()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4286
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php namespace Cerbero\Auth\Pipes;
2
3
use Illuminate\Contracts\Container\Container;
4
use Illuminate\Contracts\Events\Dispatcher;
5
6
/**
7
 * Abstract implementation of a pipe that dispatches events.
8
 *
9
 * @author	Andrea Marco Sartori
10
 */
11
abstract class AbstractEventDispatcherPipe extends AbstractPipe {
12
13
	/**
14
	 * @author	Andrea Marco Sartori
15
	 * @var		Illuminate\Contracts\Events\Dispatcher	$dispatcher	Event dispatcher.
16
	 */
17
	protected $dispatcher;
18
	
19
	/**
20
	 * Set the dependencies.
21
	 *
22
	 * @author	Andrea Marco Sartori
23
	 * @param	Illuminate\Contracts\Container\Container	$container
24
	 * @param	Illuminate\Contracts\Events\Dispatcher	$dispatcher
25
	 * @return	void
26
	 */
27
	public function __construct(Container $container, Dispatcher $dispatcher)
28
	{
29
		parent::__construct($container);
30
31
		$this->dispatcher = $dispatcher;
32
	}
33
34
	/**
35
	 * Run before the job is handled.
36
	 *
37
	 * @param	mixed	$job
38
	 * @return	mixed
39
	 */
40
	public function before($job)
41
	{
42
		$this->fireEventOn('start', $job);
43
	}
44
45
	/**
46
	 * Fire an event at some point.
47
	 *
48
	 * @author	Andrea Marco Sartori
49
	 * @param	string	$action
50
	 * @param	mixed	$payload
51
	 * @return	void
52
	 */
53
	private function fireEventOn($action, $payload)
54
	{
55
		$event = $this->getEventName();
56
57
		$this->dispatcher->fire("auth.{$event}.{$action}", $payload);
58
	}
59
60
	/**
61
	 * Retrieve the event name.
62
	 *
63
	 * @author	Andrea Marco Sartori
64
	 * @return	string
65
	 */
66
	protected function getEventName()
67
	{
68
		$chunks = explode('\\', get_class($this));
69
70
		$name = $chunks[count($chunks) - 2];
71
72
		return strtolower($name);
73
	}
74
75
	/**
76
	 * Run after the handled job.
77
	 *
78
	 * @param	mixed	$handled
79
	 * @param	mixed	$job
80
	 * @return	mixed
81
	 */
82
	public function after($handled, $job)
83
	{
84
		$this->fireEventOn('end', [$handled, $job]);
85
	}
86
87
}
88