1 | <?php namespace Cerbero\Workflow\Repositories; |
||
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 |
||
|
|||
45 | */ |
||
46 | public function __construct(YamlParserInterface $parser, Filesystem $files, $path) |
||
47 | { |
||
48 | $this->parser = $parser; |
||
49 | |||
50 | $this->files = $files; |
||
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) |
||
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) |
||
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) |
||
196 | |||
197 | /** |
||
198 | * Refresh the pipelines source. |
||
199 | * |
||
200 | * @author Andrea Marco Sartori |
||
201 | * @return void |
||
202 | */ |
||
203 | protected function refreshPipelines() |
||
204 | { |
||
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) |
||
223 | |||
224 | } |
||
225 |
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.