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

CollisionHandler::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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