Completed
Push — master ( dfd004...b46837 )
by Jitendra
11s
created

CollisionHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeJson() 0 9 1
A handle() 0 7 2
A __construct() 0 3 2
1
<?php
2
3
namespace Ahc\Phint\Generator;
4
5
use Ahc\Phint\Util\Arr;
6
use Ahc\Phint\Util\Path;
7
8
class CollisionHandler implements CollisionHandlerInterface
9
{
10
    public function __construct(Path $pathUtil = null)
11
    {
12
        $this->pathUtil = $pathUtil ?: new Path;
0 ignored issues
show
Bug Best Practice introduced by
The property pathUtil does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
13
    }
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function handle($targetFile, $newContent, array $parameters = null)
19
    {
20
        switch ($this->pathUtil->getExtension($targetFile)) {
21
            case 'json':
22
                $this->mergeJson($targetFile, $newContent);
23
24
                break;
25
        }
26
    }
27
28
    protected function mergeJson($targetFile, $newContent)
29
    {
30
        $oldJson = $this->pathUtil->readAsJson($targetFile);
31
        $newJson = \json_decode($newContent, true);
32
        $merged  = Arr::mergeRecursive($oldJson, $newJson);
33
34
        $this->pathUtil->writeFile(
35
            $targetFile,
36
            \json_encode($merged, \JSON_PRETTY_PRINT) . "\n"
37
        );
38
    }
39
}
40