Completed
Push — develop ( eeebfd...e7b4cf )
by Andrea Marco
06:26
created

YamlPipelineRepository::attach()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Cerbero\Workflow\Repositories;
4
5
use Cerbero\Workflow\Wrappers\YamlParserInterface;
6
use Illuminate\Filesystem\Filesystem;
7
8
/**
9
 * Pipeline repository using YAML.
10
 *
11
 * @author	Andrea Marco Sartori
12
 */
13
class YamlPipelineRepository implements PipelineRepositoryInterface
14
{
15
    /**
16
     * @author	Andrea Marco Sartori
17
     *
18
     * @var array $pipelines	Pipelines list.
19
     */
20
    protected $pipelines;
21
22
    /**
23
     * @author	Andrea Marco Sartori
24
     *
25
     * @var Cerbero\Workflow\Wrappers\YamlParserInterface $parser	YAML parser.
26
     */
27
    protected $parser;
28
29
    /**
30
     * @author	Andrea Marco Sartori
31
     *
32
     * @var Illuminate\Filesystem\Filesystem $files	Filesystem.
33
     */
34
    protected $files;
35
36
    /**
37
     * @author	Andrea Marco Sartori
38
     *
39
     * @var string $path	The workflows path.
40
     */
41
    protected $path;
42
43
    /**
44
     * Set the dependencies.
45
     *
46
     * @author	Andrea Marco Sartori
47
     *
48
     * @param Cerbero\Workflow\Wrappers\YamlParserInterface $parser
49
     * @param Illuminate\Filesystem\Filesystem              $files
50
     * @param string                                        $path
51
     *
52
     * @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...
53
     */
54 33
    public function __construct(YamlParserInterface $parser, Filesystem $files, $path)
55
    {
56 33
        $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...
57
58 33
        $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...
59
60 33
        $this->path = $path;
61
62 33
        $this->pipelines = $this->parseYaml();
63 33
    }
64
65
    /**
66
     * Parse the YAML file.
67
     *
68
     * @author	Andrea Marco Sartori
69
     *
70
     * @return array
71
     */
72 33
    private function parseYaml()
73
    {
74 33
        $file = $this->getSource();
75
76 33
        return (array) $this->parser->parse($file);
77
    }
78
79
    /**
80
     * Retrieve the source of the pipelines.
81
     *
82
     * @author	Andrea Marco Sartori
83
     *
84
     * @return string
85
     */
86 33
    public function getSource()
87
    {
88 33
        $path = rtrim($this->path, '/');
89
90 33
        return "{$path}/workflows.yml";
91
    }
92
93
    /**
94
     * Determine whether a given pipeline exists.
95
     *
96
     * @author	Andrea Marco Sartori
97
     *
98
     * @param string $pipeline
99
     *
100
     * @return bool
101
     */
102 6
    public function exists($pipeline)
103
    {
104 6
        $this->normalizePipeline($pipeline);
105
106 6
        return array_key_exists($pipeline, $this->pipelines);
107
    }
108
109
    /**
110
     * Normalize the name of the given pipeline.
111
     *
112
     * @author	Andrea Marco Sartori
113
     *
114
     * @param string $pipeline
115
     *
116
     * @return void
117
     */
118 9
    protected function normalizePipeline(&$pipeline)
119
    {
120 9
        $pipeline = ucfirst($pipeline);
121 9
    }
122
123
    /**
124
     * Retrieve the pipes of a given pipeline.
125
     *
126
     * @author	Andrea Marco Sartori
127
     *
128
     * @param string $pipeline
129
     *
130
     * @return array
131
     */
132 3
    public function getPipesByPipeline($pipeline)
133
    {
134 3
        $this->normalizePipeline($pipeline);
135
136 3
        return $this->pipelines[$pipeline];
137
    }
138
139
    /**
140
     * Create the pipelines storage.
141
     *
142
     * @author	Andrea Marco Sartori
143
     *
144
     * @return void
145
     */
146 3
    public function settle()
147
    {
148 3
        $this->files->makeDirectory($this->path, 0755, true, true);
149
150 3
        $this->files->put($this->getSource(), '');
151 3
    }
152
153
    /**
154
     * Store the given pipeline and its pipes.
155
     *
156
     * @author	Andrea Marco Sartori
157
     *
158
     * @param string $pipeline
159
     * @param array  $pipes
160
     *
161
     * @return void
162
     */
163 3
    public function store($pipeline, array $pipes)
164
    {
165 3
        $workflow = [$pipeline => $pipes];
166
167 3
        $yaml = $this->parser->dump($workflow);
168
169 3
        $this->files->append($this->getSource(), $yaml);
170 3
    }
171
172
    /**
173
     * Update the given pipeline and its pipes.
174
     *
175
     * @author	Andrea Marco Sartori
176
     *
177
     * @param string $pipeline
178
     * @param array  $attachments
179
     * @param array  $detachments
180
     *
181
     * @return void
182
     */
183 9
    public function update($pipeline, array $attachments, array $detachments)
184
    {
185 9
        $this->detach($this->pipelines[$pipeline], $detachments);
186
187 9
        $this->attach($this->pipelines[$pipeline], $attachments);
188
189 9
        $this->refreshPipelines();
190 9
    }
191
192
    /**
193
     * Detach pipes from a given pipeline.
194
     *
195
     * @author	Andrea Marco Sartori
196
     *
197
     * @param array $pipeline
198
     * @param array $pipes
199
     *
200
     * @return void
201
     */
202 9
    protected function detach(array &$pipeline, array $pipes)
203
    {
204 9
        $pipeline = array_diff($pipeline, $pipes);
205 9
    }
206
207
    /**
208
     * Attach pipes to a given pipeline.
209
     *
210
     * @author	Andrea Marco Sartori
211
     *
212
     * @param array $pipeline
213
     * @param array $pipes
214
     *
215
     * @return void
216
     */
217 9
    protected function attach(array &$pipeline, array $pipes)
218
    {
219 9
        $pipeline = array_merge($pipeline, $pipes);
220 9
    }
221
222
    /**
223
     * Refresh the pipelines source.
224
     *
225
     * @author	Andrea Marco Sartori
226
     *
227
     * @return void
228
     */
229 12
    protected function refreshPipelines()
230
    {
231 12
        $yaml = $this->parser->dump($this->pipelines);
232
233 12
        $this->files->put($this->getSource(), $yaml);
234 12
    }
235
236
    /**
237
     * Destroy a given pipeline.
238
     *
239
     * @author	Andrea Marco Sartori
240
     *
241
     * @param string $pipeline
242
     *
243
     * @return void
244
     */
245 3
    public function destroy($pipeline)
246
    {
247 3
        unset($this->pipelines[$pipeline]);
248
249 3
        $this->refreshPipelines();
250 3
    }
251
}
252