OptionService   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 11
c 2
b 0
f 2
lcom 1
cbo 6
dl 0
loc 127
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 4 1
A getOptions() 0 4 1
A setOptions() 0 4 1
A set() 0 4 1
A __construct() 0 12 2
A get() 0 6 1
A readConfig() 0 10 2
A writeConfig() 0 8 2
1
<?php
2
3
/*
4
 * (c) Jim Martens <[email protected]>
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 TwoMartens\Bundle\CoreBundle\Option;
11
12
use Symfony\Component\Filesystem\Filesystem;
13
use Symfony\Component\Finder\Finder;
14
use Symfony\Component\Finder\SplFileInfo;
15
use Symfony\Component\Yaml\Dumper;
16
use Symfony\Component\Yaml\Parser;
17
use TwoMartens\Bundle\CoreBundle\Model\Option;
18
use TwoMartens\Bundle\CoreBundle\Model\OptionCategory;
19
use TwoMartens\Bundle\CoreBundle\Util\ConfigUtil;
20
21
/**
22
 * Provides core option functionality.
23
 *
24
 * @author    Jim Martens <[email protected]>
25
 * @copyright 2013-2015 Jim Martens
26
 */
27
class OptionService implements OptionServiceInterface
28
{
29
    /**
30
     * the finder
31
     * @var Finder
32
     */
33
    private $finder;
34
35
    /**
36
     * the YAML parser
37
     * @var Parser
38
     */
39
    private $parser;
40
41
    /**
42
     * the YAML dumper
43
     * @var Dumper
44
     */
45
    private $dumper;
46
47
    /**
48
     * the filesystem
49
     * @var Filesystem
50
     */
51
    private $filesystem;
52
53
    /**
54
     * the options in YAML format
55
     * @var array
56
     */
57
    private $optionsYAML;
58
59
    /**
60
     * Initializes the service.
61
     *
62
     * @param Finder     $finder     Must be configured to read ONE config file
63
     * @param Parser     $parser     A YAML parser
64
     * @param Dumper     $dumper     A YAML dumper
65
     * @param Filesystem $filesystem A filesystem
66
     */
67
    public function __construct(Finder $finder, Parser $parser, Dumper $dumper, Filesystem $filesystem)
68
    {
69
        $this->finder = $finder;
70
        $this->parser = $parser;
71
        $this->dumper = $dumper;
72
        $this->filesystem = $filesystem;
73
74
        $this->optionsYAML = $this->readConfig();
75
        if ($this->optionsYAML === null) {
76
            throw new \LogicException('The given finder isn\'t properly configured.');
77
        }
78
    }
79
80
    /**
81
     * Writes the options to the config file.
82
     */
83
    public function __destruct()
84
    {
85
        $this->writeConfig();
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getOptions()
92
    {
93
        return ConfigUtil::convertToOptions($this->optionsYAML);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function setOptions(OptionCategory $options)
100
    {
101
        $this->optionsYAML = ConfigUtil::convertToArray($options);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function get($optionCategory, $optionName)
108
    {
109
        $optionValue = $this->optionsYAML[$optionCategory][$optionName];
110
111
        return ConfigUtil::convertToOption($optionName, $optionValue);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function set($optionCategory, $optionName, Option $value)
118
    {
119
        $this->optionsYAML[$optionCategory][$optionName] = $value->getValue();
120
    }
121
122
    /**
123
     * Reads the config file configured in the finder and returns the
124
     * YAML parsed content for it.
125
     *
126
     * @return array|null
0 ignored issues
show
Documentation introduced by
Should the return type not be array|string|\stdClass|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
127
     */
128
    private function readConfig()
129
    {
130
        $configContents = null;
131
        foreach ($this->finder as $configFile) {
132
            /** @var SplFileInfo $configFile */
133
            $configContents = $this->parser->parse($configFile->getContents());
134
        }
135
136
        return $configContents;
137
    }
138
139
    /**
140
     * Dumps the options into the config file.
141
     *
142
     * ATTENTION: This method DOES NOT perform any sanitize actions. It
143
     * overwrites the config file with the options entirely.
144
     */
145
    private function writeConfig()
146
    {
147
        $dumpedData = $this->dumper->dump($this->optionsYAML, 2);
148
        foreach ($this->finder as $configFile) {
149
            /** @var SplFileInfo $configFile */
150
            $this->filesystem->dumpFile($configFile->getRealPath(), $dumpedData);
151
        }
152
    }
153
}
154