YamlPipelineRepository::destroy()   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
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php namespace Cerbero\Workflow\Repositories;
2
3
use Cerbero\Workflow\Wrappers\YamlParserInterface;
4
use Illuminate\Filesystem\Filesystem;
5
6
/**
7
 * Pipeline repository using YAML.
8
 *
9
 * @author	Andrea Marco Sartori
10
 */
11
class YamlPipelineRepository implements PipelineRepositoryInterface {
12
13
	/**
14
	 * @author	Andrea Marco Sartori
15
	 * @var		array	$pipelines	Pipelines list.
16
	 */
17
	protected $pipelines;
18
19
	/**
20
	 * @author	Andrea Marco Sartori
21
	 * @var		Cerbero\Workflow\Wrappers\YamlParserInterface	$parser	YAML parser.
22
	 */
23
	protected $parser;
24
25
	/**
26
	 * @author	Andrea Marco Sartori
27
	 * @var		Illuminate\Filesystem\Filesystem	$files	Filesystem.
28
	 */
29
	protected $files;
30
31
	/**
32
	 * @author	Andrea Marco Sartori
33
	 * @var		string	$path	The workflows path.
34
	 */
35
	protected $path;
36
	
37
	/**
38
	 * Set the dependencies.
39
	 *
40
	 * @author	Andrea Marco Sartori
41
	 * @param	Cerbero\Workflow\Wrappers\YamlParserInterface	$parser
42
	 * @param	Illuminate\Filesystem\Filesystem	$files
43
	 * @param	string	$path
44
	 * @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...
45
	 */
46
	public function __construct(YamlParserInterface $parser, Filesystem $files, $path)
47
	{
48
		$this->parser = $parser;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parser of type object<Cerbero\Workflow\...rs\YamlParserInterface> is incompatible with the declared type object<Cerbero\Workflow\...rs\YamlParserInterface> of property $parser.

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...
49
50
		$this->files = $files;
0 ignored issues
show
Documentation Bug introduced by
It seems like $files of type object<Illuminate\Filesystem\Filesystem> is incompatible with the declared type object<Cerbero\Workflow\...\Filesystem\Filesystem> of property $files.

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...
51
52
		$this->path = $path;
53
54
		$this->pipelines = $this->parseYaml();
55
	}
56
57
	/**
58
	 * Parse the YAML file.
59
	 *
60
	 * @author	Andrea Marco Sartori
61
	 * @return	array
62
	 */
63
	private function parseYaml()
64
	{
65
		$file = $this->getSource();
66
67
		return (array) $this->parser->parse($file);
68
	}
69
70
	/**
71
	 * Retrieve the source of the pipelines.
72
	 *
73
	 * @author	Andrea Marco Sartori
74
	 * @return	string
75
	 */
76
	public function getSource()
77
	{
78
		$path = rtrim($this->path, '/');
79
80
		return "{$path}/workflows.yml";
81
	}
82
83
	/**
84
	 * Determine whether a given pipeline exists.
85
	 *
86
	 * @author	Andrea Marco Sartori
87
	 * @param	string	$pipeline
88
	 * @return	boolean
89
	 */
90
	public function exists($pipeline)
91
	{
92
		$this->normalizePipeline($pipeline);
93
94
		return array_key_exists($pipeline, $this->pipelines);
95
	}
96
97
	/**
98
	 * Normalize the name of the given pipeline.
99
	 *
100
	 * @author	Andrea Marco Sartori
101
	 * @param	string	$pipeline
102
	 * @return	void
103
	 */
104
	protected function normalizePipeline(&$pipeline)
105
	{
106
		$pipeline = ucfirst($pipeline);
107
	}
108
109
	/**
110
	 * Retrieve the pipes of a given pipeline.
111
	 *
112
	 * @author	Andrea Marco Sartori
113
	 * @param	string	$pipeline
114
	 * @return	array
115
	 */
116
	public function getPipesByPipeline($pipeline)
117
	{
118
		$this->normalizePipeline($pipeline);
119
120
		return $this->pipelines[$pipeline];
121
	}
122
123
	/**
124
	 * Create the pipelines storage.
125
	 *
126
	 * @author	Andrea Marco Sartori
127
	 * @return	void
128
	 */
129
	public function settle()
130
	{
131
		$this->files->makeDirectory($this->path, 0755, true, true);
132
133
		$this->files->put($this->getSource(), '');
134
	}
135
136
	/**
137
	 * Store the given pipeline and its pipes.
138
	 *
139
	 * @author	Andrea Marco Sartori
140
	 * @param	string	$pipeline
141
	 * @param	array	$pipes
142
	 * @return	void
143
	 */
144
	public function store($pipeline, array $pipes)
145
	{
146
		$workflow = [$pipeline => $pipes];
147
148
		$yaml = $this->parser->dump($workflow);
149
150
		$this->files->append($this->getSource(), $yaml);
151
	}
152
153
	/**
154
	 * Update the given pipeline and its pipes.
155
	 *
156
	 * @author	Andrea Marco Sartori
157
	 * @param	string	$pipeline
158
	 * @param	array	$attachments
159
	 * @param	array	$detachments
160
	 * @return	void
161
	 */
162
	public function update($pipeline, array $attachments, array $detachments)
163
	{
164
		$this->detach($this->pipelines[$pipeline], $detachments);
165
166
		$this->attach($this->pipelines[$pipeline], $attachments);
167
168
		$this->refreshPipelines();
169
	}
170
171
	/**
172
	 * Detach pipes from a given pipeline.
173
	 *
174
	 * @author	Andrea Marco Sartori
175
	 * @param	array	$pipeline
176
	 * @param	array	$pipes
177
	 * @return	void
178
	 */
179
	protected function detach(array &$pipeline, array $pipes)
180
	{
181
		$pipeline = array_diff($pipeline, $pipes);
182
	}
183
184
	/**
185
	 * Attach pipes to a given pipeline.
186
	 *
187
	 * @author	Andrea Marco Sartori
188
	 * @param	array	$pipeline
189
	 * @param	array	$pipes
190
	 * @return	void
191
	 */
192
	protected function attach(array &$pipeline, array $pipes)
193
	{
194
		$pipeline = array_merge($pipeline, $pipes);
195
	}
196
197
	/**
198
	 * Refresh the pipelines source.
199
	 *
200
	 * @author	Andrea Marco Sartori
201
	 * @return	void
202
	 */
203
	protected function refreshPipelines()
204
	{
205
		$yaml = $this->parser->dump($this->pipelines);
206
207
		$this->files->put($this->getSource(), $yaml);
208
	}
209
210
	/**
211
	 * Destroy a given pipeline.
212
	 *
213
	 * @author	Andrea Marco Sartori
214
	 * @param	string	$pipeline
215
	 * @return	void
216
	 */
217
	public function destroy($pipeline)
218
	{
219
		unset($this->pipelines[$pipeline]);
220
221
		$this->refreshPipelines();
222
	}
223
224
}
225