1: <?php
2: namespace Contrib\Bundle\CoverallsV1Bundle\Config;
3:
4: use Contrib\Component\File\Path;
5: use Symfony\Component\Yaml\Yaml;
6: use Symfony\Component\Config\Definition\Processor;
7: use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
8:
9: 10: 11: 12: 13:
14: class Configurator
15: {
16:
17:
18: 19: 20: 21: 22: 23: 24:
25: public function load($coverallsYmlPath, $rootDir)
26: {
27: $yml = $this->parse($coverallsYmlPath);
28: $options = $this->process($yml);
29:
30: return $this->createConfiguration($options, $rootDir);
31: }
32:
33:
34:
35: 36: 37: 38: 39: 40:
41: protected function parse($coverallsYmlPath)
42: {
43: $file = new Path();
44: $path = realpath($coverallsYmlPath);
45:
46: if ($file->isRealFileReadable($path)) {
47: $yml = Yaml::parse($path);
48:
49: return empty($yml) ? array() : $yml;
50: }
51:
52: return array();
53: }
54:
55: 56: 57: 58: 59: 60:
61: protected function process(array $yml)
62: {
63: $processor = new Processor();
64: $configuration = new CoverallsConfiguration();
65:
66: return $processor->processConfiguration($configuration, array('coveralls' => $yml));
67: }
68:
69: 70: 71: 72: 73: 74: 75:
76: protected function createConfiguration(array $options, $rootDir)
77: {
78: $configuration = new Configuration();
79: $file = new Path();
80:
81: $repoToken = $options['repo_token'];
82: $repoSecretToken = $options['repo_secret_token'];
83:
84: return $configuration
85: ->setRepoToken($repoToken !== null ? $repoToken : $repoSecretToken)
86: ->setServiceName($options['service_name'])
87:
88: ->setSrcDir($this->ensureSrcDir($options['src_dir'], $rootDir, $file))
89: ->setCloverXmlPaths($this->ensureCloverXmlPaths($options['coverage_clover'], $rootDir, $file))
90: ->setJsonPath($this->ensureJsonPath($options['json_path'], $rootDir, $file))
91: ->setExcludeNoStatements($options['exclude_no_stmt']);
92: }
93:
94: 95: 96: 97: 98: 99: 100: 101: 102:
103: protected function ensureSrcDir($option, $rootDir, Path $file)
104: {
105:
106: $realpath = $file->getRealPath($option, $rootDir);
107:
108:
109: if (!$file->isRealDirExist($realpath)) {
110: throw new InvalidConfigurationException('src directory is not found');
111: }
112:
113: return $realpath;
114: }
115:
116: 117: 118: 119: 120: 121: 122: 123: 124:
125: protected function ensureCloverXmlPaths($option, $rootDir, Path $file)
126: {
127: if (is_array($option)) {
128: return $this->getGlobPathsFromArrayOption($option, $rootDir, $file);
129: }
130:
131: return $this->getGlobPathsFromStringOption($option, $rootDir, $file);
132: }
133:
134: 135: 136: 137: 138: 139: 140:
141: protected function getGlobPaths($path)
142: {
143: $paths = array();
144: $iterator = new \GlobIterator($path);
145:
146: foreach ($iterator as $fileInfo) {
147:
148: $paths[] = $fileInfo->getPathname();
149: }
150:
151:
152: if (count($paths) === 0) {
153: throw new InvalidConfigurationException('coverage_clover XML file is not readable');
154: }
155:
156: return $paths;
157: }
158:
159: 160: 161: 162: 163: 164: 165: 166: 167:
168: protected function getGlobPathsFromStringOption($option, $rootDir, Path $file)
169: {
170: if (!is_string($option)) {
171: throw new InvalidConfigurationException('coverage_clover XML file is not readable');
172: }
173:
174:
175: $path = $file->toAbsolutePath($option, $rootDir);
176:
177: return $this->getGlobPaths($path);
178: }
179:
180: 181: 182: 183: 184: 185: 186: 187:
188: protected function getGlobPathsFromArrayOption(array $options, $rootDir, Path $file)
189: {
190: $paths = array();
191:
192: foreach ($options as $option) {
193: $paths = array_merge($paths, $this->getGlobPathsFromStringOption($option, $rootDir, $file));
194: }
195:
196: return $paths;
197: }
198:
199: 200: 201: 202: 203: 204: 205: 206: 207:
208: protected function ensureJsonPath($option, $rootDir, Path $file)
209: {
210:
211: $realpath = $file->getRealWritingFilePath($option, $rootDir);
212:
213:
214: $realFilePath = $file->getRealPath($realpath, $rootDir);
215:
216: if ($realFilePath !== false && !$file->isRealFileWritable($realFilePath)) {
217: throw new InvalidConfigurationException('json_path is not writable');
218: }
219:
220:
221: $realDir = $file->getRealDir($realpath, $rootDir);
222:
223: if (!$file->isRealDirWritable($realDir)) {
224: throw new InvalidConfigurationException('json_path is not writable');
225: }
226:
227: return $realpath;
228: }
229: }
230: