Workflow::__call()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php namespace Cerbero\Workflow;
2
3
use Cerbero\Workflow\Repositories\PipelineRepositoryInterface;
4
use Cerbero\Workflow\Inflectors\InflectorInterface;
5
use Cerbero\Workflow\Wrappers\DispatcherInterface;
6
use Illuminate\Contracts\Container\Container;
7
8
/**
9
 * Hub of the pipelines.
10
 *
11
 * @author	Andrea Marco Sartori
12
 */
13
class Workflow {
14
15
	/**
16
	 * @author	Andrea Marco Sartori
17
	 * @var		Cerbero\Workflow\Repositories\PipelineRepositoryInterface	$pipelines	Pipelines repository.
18
	 */
19
	protected $pipelines;
20
21
	/**
22
	 * @author	Andrea Marco Sartori
23
	 * @var		Cerbero\Workflow\Inflectors\InflectorInterface	$inflector	Inflector.
24
	 */
25
	protected $inflector;
26
27
	/**
28
	 * @author	Andrea Marco Sartori
29
	 * @var		Illuminate\Contracts\Container\Container	$container	Service container.
30
	 */
31
	protected $container;
32
33
	/**
34
	 * @author	Andrea Marco Sartori
35
	 * @var		Cerbero\Workflow\Wrappers\DispatcherInterface	$dispatcher	Bus dispatcher.
36
	 */
37
	protected $dispatcher;
38
	
39
	/**
40
	 * Set the dependencies.
41
	 *
42
	 * @author	Andrea Marco Sartori
43
	 * @param	Cerbero\Workflow\Repositories\PipelineRepositoryInterface	$pipelines
44
	 * @param	Cerbero\Workflow\Wrappers\DispatcherInterface	$dispatcher
45
	 * @param	Cerbero\Workflow\Inflectors\InflectorInterface	$inflector
46
	 * @param	Illuminate\Contracts\Container\Container	$container
47
	 * @return	void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
48
	 */
49
	public function __construct(
50
		PipelineRepositoryInterface $pipelines,
51
		InflectorInterface $inflector,
52
		Container $container,
53
		DispatcherInterface $dispatcher
54
	) {
55
		$this->pipelines  = $pipelines;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pipelines of type object<Cerbero\Workflow\...ineRepositoryInterface> is incompatible with the declared type object<Cerbero\Workflow\...ineRepositoryInterface> of property $pipelines.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
		$this->inflector  = $inflector;
0 ignored issues
show
Documentation Bug introduced by
It seems like $inflector of type object<Cerbero\Workflow\...ors\InflectorInterface> is incompatible with the declared type object<Cerbero\Workflow\...ors\InflectorInterface> of property $inflector.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
		$this->container  = $container;
0 ignored issues
show
Documentation Bug introduced by
It seems like $container of type object<Illuminate\Contracts\Container\Container> is incompatible with the declared type object<Cerbero\Workflow\...ts\Container\Container> of property $container.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
		$this->dispatcher = $dispatcher;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dispatcher of type object<Cerbero\Workflow\...rs\DispatcherInterface> is incompatible with the declared type object<Cerbero\Workflow\...rs\DispatcherInterface> of property $dispatcher.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
	}
60
61
	/**
62
	 * Dynamically call pipelines.
63
	 *
64
	 * @author	Andrea Marco Sartori
65
	 * @param	string	$name
66
	 * @param	array	$arguments
67
	 * @return	mixed
68
	 */
69
	public function __call($name, $arguments)
70
	{
71
		$this->failIfNotExists($name);
72
73
		$this->inflector->of($name);
74
75
		return $this->dispatchWorkflow($name);
76
	}
77
78
	/**
79
	 * Throw an exception if the given workflow does not exist.
80
	 *
81
	 * @author	Andrea Marco Sartori
82
	 * @param	string	$workflow
83
	 * @return	void
84
	 */
85
	protected function failIfNotExists($workflow)
86
	{
87
		if( ! $this->pipelines->exists($workflow))
88
		{
89
			$error = "The workflow [$workflow] does not exist.";
90
91
			throw new \BadFunctionCallException($error);
92
		}
93
	}
94
95
	/**
96
	 * Dispatch the given workflow.
97
	 *
98
	 * @author	Andrea Marco Sartori
99
	 * @param	string	$workflow
100
	 * @return	mixed
101
	 */
102
	protected function dispatchWorkflow($workflow)
103
	{
104
		$job = $this->inflector->getJob();
105
106
		$request = $this->resolveRequest();
107
108
		$pipes = $this->pipelines->getPipesByPipeline($workflow);
109
110
		$parameters = $this->container->make('router')->current()->parameters();
111
112
		return $this->dispatcher->pipeThrough($pipes)->dispatchFrom($job, $request, $parameters);
113
	}
114
115
	/**
116
	 * Resolve the apter request.
117
	 *
118
	 * @author	Andrea Marco Sartori
119
	 * @return	Illuminate\Http\Request
120
	 */
121
	protected function resolveRequest()
122
	{
123
		if(class_exists($request = $this->inflector->getRequest()))
124
		{
125
			return $this->container->make($request);
126
		}
127
128
		return $this->container->make('Illuminate\Http\Request');
129
	}
130
131
}
132