Completed
Push — master ( f91003...7ec665 )
by personal
05:42 queued 03:06
created

ConfigDumper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 71
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B dump() 0 42 4
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Application\Config;
11
use Hal\Component\Config\Hydrator;
12
use Hal\Component\Config\Loader;
13
use Hal\Component\Config\Validator;
14
use Hal\Component\Result\ExportableInterface;
15
use Symfony\Component\Console\Input\InputInterface;
16
17
/**
18
 * Config file generator
19
 *
20
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
21
 */
22
class ConfigDumper
23
{
24
    /**
25
     * @var string
26
     */
27
    private $destination;
28
29
    /**
30
     * @var ExportableInterface
31
     */
32
    private $ruleset;
33
34
    /**
35
     * ConfigFileGenerator constructor.
36
     * @param string $destination
37
     * @param ExportableInterface $ruleset
38
     */
39
    public function __construct($destination, ExportableInterface $ruleset)
40
    {
41
        $this->destination = $destination;
42
        $this->ruleset = $ruleset;
43
    }
44
45
    /**
46
     * Generate config file
47
     *
48
     * @return $this
49
     */
50
    public function dump()
51
    {
52
53
        // rules
54
        $rules = '';
55
        foreach($this->ruleset->asArray() as $key => $values) {
56
            $rules .= sprintf('        %s: [ %s ]%s', $key, implode(', ', $values), PHP_EOL);
57
        }
58
59
        // main content
60
        $content = <<<EOT
61
# This file is used by PhpMetrics
62
# Please visit http://www.phpmetrics.org do get more informations
63
default:
64
    path:
65
        directory: src
66
    logging:
67
        report:
68
            html:   ./phpmetrics.html
69
    rules:
70
{$rules}
71
72
EOT;
73
74
        // write file
75
        if(!$this->destination) {
76
            throw new \LogicException('Please provide a destination');
77
        }
78
79
        $dir = dirname($this->destination);
80
        if(!file_exists($dir)) {
81
            mkdir($dir, 0777, true);
82
        }
83
84
        $handle = fopen($this->destination, 'w');
85
        fwrite($handle, $content);
86
        fflush($handle);
87
        fclose($handle);
88
89
        return $this;
90
91
    }
92
}