Config::getWriter()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ConfigWriter;
4
5
use ConfigWriter\Exceptions\UnsupportedFormatException;
6
use ConfigWriter\Exceptions\WriteException;
7
use ConfigWriter\Writers\WriterInterface;
8
9
/**
10
 * Configuration class.
11
 *
12
 * @since 2.0.0
13
 *
14
 * @author Filip Š <[email protected]>
15
 *
16
 * @license MIT
17
 *
18
 * @package ConfigWriter
19
 */
20
class Config extends AbstractConfig
21
{
22
    /**
23
     * Stores the supported writers.
24
     *
25
     * @var array
26
     */
27
    protected $supportedWriters = ['\ConfigWriter\Writers\PhpWriter'];
28
29
    /**
30
     * Writes configuration to string.
31
     *
32
     * @param WriterInterface $writer  Configuration writer
33
     * @param array           $options Writer options (optional)
34
     *
35
     * @return string Encoded configuration string
36
     *
37
     * @throws WriteException If there is an error while writing a string
38
     */
39 3
    public function toString(WriterInterface $writer, $options = [])
40
    {
41 3
        return $writer->write($this, $options);
42
    }
43
44
    /**
45
     * Writes configuration to file.
46
     *
47
     * @param string          $filename Configuration file name
48
     * @param WriterInterface $writer   Configuration writer (optional)
49
     * @param array           $options  Writer options (optional)
50
     *
51
     * @return void
52
     *
53
     * @throws UnsupportedFormatException If file extension is unsupported
54
     * @throws WriteException             If there is an error while writing a file
55
     */
56 4
    public function toFile($filename, WriterInterface $writer = null, $options = [])
57
    {
58 4
        if ($writer === null) {
59
            // Get file information
60 3
            $info = pathinfo($filename);
61 3
            $parts = explode('.', $info['basename']);
62 3
            $extension = array_pop($parts);
63
64
            // Skip the `dist` extension
65 3
            if ($extension === 'dist') {
66 1
                $extension = array_pop($parts);
67
            }
68
69
            // Get configuration writer
70 3
            $writer = $this->getWriter($extension);
71
        }
72
73 3
        $data = $this->toString($writer, $options);
74
75
        // @codeCoverageIgnoreStart
76
        if (!is_dir(dirname($filename))) {
77
            mkdir(dirname($filename), 0777, true);
78
        }
79
        // @codeCoverageIgnoreEnd
80
81 3
        file_put_contents($filename, $data);
82 3
    }
83
84
    /**
85
     * Gets a writer for a given file extension.
86
     *
87
     * @param string $extension File extension
88
     *
89
     * @return WriterInterface Writer for a given file extension
90
     *
91
     * @throws UnsupportedFormatException If `$extension` is an unsupported file format
92
     */
93 3
    protected function getWriter($extension)
94
    {
95 3
        foreach ($this->supportedWriters as $writer) {
96 3
            if (in_array($extension, $writer::getSupportedExtensions())) {
97 2
                return new $writer();
98
            }
99
        }
100
101 1
        throw new UnsupportedFormatException('Unsupported configuration format');
102
    }
103
}
104