Total Complexity | 7 |
Total Lines | 35 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
17 | class CollisionHandler implements CollisionHandlerInterface |
||
18 | { |
||
19 | public function __construct(Path $pathUtil = null) |
||
20 | { |
||
21 | $this->pathUtil = $pathUtil ?: new Path; |
||
|
|||
22 | } |
||
23 | |||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | */ |
||
27 | public function handle(string $targetFile, string $newContent, array $parameters = null): bool |
||
28 | { |
||
29 | switch ($this->pathUtil->getExtension($targetFile)) { |
||
30 | case 'json': |
||
31 | return $this->mergeJson($targetFile, $newContent); |
||
32 | |||
33 | case 'md': |
||
34 | return $this->appendFile($targetFile, "\n---\n" . $newContent); |
||
35 | } |
||
36 | |||
37 | return false; |
||
38 | } |
||
39 | |||
40 | protected function mergeJson(string $targetFile, string $newContent): bool |
||
41 | { |
||
42 | $oldJson = $this->pathUtil->readAsJson($targetFile); |
||
43 | $newJson = \json_decode($newContent, true); |
||
44 | $merged = Arr::mergeRecursive($oldJson, $newJson); |
||
45 | |||
46 | return $this->pathUtil->writeFile($targetFile, $merged); |
||
47 | } |
||
48 | |||
49 | protected function appendFile(string $targetFile, string $newContent): bool |
||
52 | } |
||
53 | } |
||
54 |