WorkflowServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 3
Metric Value
c 4
b 0
f 3
dl 0
loc 10
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php namespace Cerbero\Workflow;
2
3
use Cerbero\Workflow\WorkflowRunner;
4
use Illuminate\Foundation\AliasLoader;
5
use Illuminate\Support\ServiceProvider;
6
use Cerbero\Workflow\Inflectors\Inflector;
7
use Cerbero\Workflow\Wrappers\SymfonyYamlParser;
8
use Cerbero\Workflow\Repositories\YamlPipelineRepository;
9
use Cerbero\Workflow\Wrappers\LaravelTraitNamespaceDetector;
10
11
/**
12
 * Workflow service provider.
13
 *
14
 * @author	Andrea Marco Sartori
15
 */
16
class WorkflowServiceProvider extends ServiceProvider {
17
18
	/**
19
	 * @author	Andrea Marco Sartori
20
	 * @var		array	$commands	List of registered commands.
21
	 */
22
	protected $commands = [
23
		'cerbero.workflow.create',
24
		'cerbero.workflow.read',
25
		'cerbero.workflow.update',
26
		'cerbero.workflow.delete',
27
	];
28
29
	/**
30
	 * Boot the package up.
31
	 *
32
	 * @author	Andrea Marco Sartori
33
	 * @return	void
34
	 */
35
	public function boot()
36
	{
37
		$this->publishConfig();
38
39
		$this->commands($this->commands);
40
41
		$facade = 'Cerbero\Workflow\Facades\Workflow';
42
43
		AliasLoader::getInstance()->alias('Workflow', $facade);
44
	}
45
46
	/**
47
	 * Publish the configuration file.
48
	 *
49
	 * @author	Andrea Marco Sartori
50
	 * @return	void
51
	 */
52
	private function publishConfig()
53
	{
54
		$config = __DIR__ . '/config/workflow.php';
55
56
		$this->publishes([$config => config_path('workflow.php')]);
57
58
		$this->mergeConfigFrom($config, 'workflow');
59
	}
60
61
	/**
62
	 * Register the services.
63
	 *
64
	 * @author	Andrea Marco Sartori
65
	 * @return	void
66
	 */
67
	public function register()
68
	{
69
		$this->registerPipelineRepository();
70
71
		$this->registerInflector();
72
73
		$this->registerDispatcher();
74
75
		$this->registerWorkflow();
76
77
		$this->registerWorkflowRunnersHook();
78
79
		$this->registerCommands();
80
	}
81
82
	/**
83
	 * Register the pipeline repository.
84
	 *
85
	 * @author	Andrea Marco Sartori
86
	 * @return	void
87
	 */
88
	private function registerPipelineRepository()
89
	{
90
		$abstract = 'Cerbero\Workflow\Repositories\PipelineRepositoryInterface';
91
92
		$this->app->bind($abstract, function($app)
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
		{
94
			return new YamlPipelineRepository
95
			(
96
				new SymfonyYamlParser,
97
98
				new \Illuminate\Filesystem\Filesystem,
99
100
				config('workflow.path')
101
			);
102
		});
103
	}
104
105
	/**
106
	 * Register the inflector.
107
	 *
108
	 * @author	Andrea Marco Sartori
109
	 * @return	void
110
	 */
111
	private function registerInflector()
112
	{
113
		$abstract = 'Cerbero\Workflow\Inflectors\InflectorInterface';
114
115
		$this->app->bind($abstract, function()
116
		{
117
			return new Inflector(new LaravelTraitNamespaceDetector);
118
		});
119
	}
120
121
	/**
122
	 * Register the bus dispatcher.
123
	 *
124
	 * @author	Andrea Marco Sartori
125
	 * @return	void
126
	 */
127
	private function registerDispatcher()
128
	{
129
		$abstract = 'Cerbero\Workflow\Wrappers\DispatcherInterface';
130
131
		$this->app->bind($abstract, function($app)
132
		{
133
			return $app['Cerbero\Workflow\Wrappers\MarshalDispatcher'];
134
		});
135
	}
136
137
	/**
138
	 * Register the package main class.
139
	 *
140
	 * @author	Andrea Marco Sartori
141
	 * @return	void
142
	 */
143
	private function registerWorkflow()
144
	{
145
		$this->app->singleton('cerbero.workflow', function($app)
146
		{
147
			return $app['Cerbero\Workflow\Workflow'];
148
		});
149
	}
150
151
	/**
152
	 * Register the hook for the workflow runners.
153
	 *
154
	 * @author	Andrea Marco Sartori
155
	 * @return	void
156
	 */
157
	private function registerWorkflowRunnersHook()
158
	{
159
		$this->app->afterResolving(function(WorkflowRunner $runner, $app)
0 ignored issues
show
Documentation introduced by
function (\Cerbero\Workf...'cerbero.workflow']); } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
160
		{
161
			$runner->setWorkflow($app['cerbero.workflow']);
162
		});
163
	}
164
165
	/**
166
	 * Register the console commands.
167
	 *
168
	 * @author	Andrea Marco Sartori
169
	 * @return	void
170
	 */
171
	private function registerCommands()
172
	{
173
		foreach ($this->commands as $command)
174
		{
175
			$name = ucfirst(last(explode('.', $command)));
176
177
			$this->app->singleton($command, function($app) use($name)
178
			{
179
				return $app["Cerbero\Workflow\Console\Commands\\{$name}WorkflowCommand"];
180
			});
181
		}
182
	}
183
184
}
185