1 | <?php |
||
16 | class ConfigBuilder |
||
17 | { |
||
18 | private static $validCommitAutoValues = [ |
||
19 | 'never', |
||
20 | 'ask', |
||
21 | 'always', |
||
22 | ]; |
||
23 | |||
24 | /** @var string[] */ |
||
25 | private $warnings = []; |
||
26 | |||
27 | /** |
||
28 | * @param array $extra |
||
29 | * @param string $baseDir |
||
30 | * |
||
31 | * @return Config |
||
32 | */ |
||
33 | 22 | public function build(array $extra, $baseDir) |
|
34 | { |
||
35 | 22 | $this->reset(); |
|
36 | |||
37 | 22 | $commitAuto = 'never'; |
|
38 | 22 | $commitBinFile = null; |
|
39 | 22 | $commitMessage = 'Update dependencies'; |
|
40 | |||
41 | 22 | if (array_key_exists('commit-auto', $extra)) { |
|
42 | 16 | if (in_array($extra['commit-auto'], self::$validCommitAutoValues, true)) { |
|
43 | 14 | $commitAuto = $extra['commit-auto']; |
|
44 | 14 | } else { |
|
45 | 2 | $this->warnings[] = self::createWarningFromInvalidValue( |
|
46 | 2 | $extra, |
|
47 | 2 | 'commit-auto', |
|
48 | 2 | $commitAuto, |
|
49 | 2 | sprintf('Valid options are "%s".', implode('", "', self::$validCommitAutoValues)) |
|
50 | 2 | ); |
|
51 | } |
||
52 | 16 | } |
|
53 | |||
54 | 22 | if (array_key_exists('commit-bin-file', $extra)) { |
|
55 | 12 | if ($commitAuto === 'never') { |
|
56 | 2 | $this->warnings[] = '"commit-bin-file" is specified but "commit-auto" option is set to "never". Ignoring.'; |
|
57 | 2 | } else { |
|
58 | 10 | $file = realpath( |
|
59 | 10 | FileSystemHelper::isAbsolute($extra['commit-bin-file']) |
|
60 | 10 | ? $extra['commit-bin-file'] |
|
61 | 10 | : $baseDir . '/' . $extra['commit-bin-file'] |
|
62 | 10 | ); |
|
63 | |||
64 | 10 | if (!file_exists($file)) { |
|
65 | 2 | $this->warnings[] = 'The file pointed by the option "commit-bin-file" was not found. Ignoring.'; |
|
66 | 2 | } else { |
|
67 | 8 | $commitBinFile = $file; |
|
68 | } |
||
69 | } |
||
70 | 12 | } else { |
|
71 | 10 | if ($commitAuto !== 'never') { |
|
72 | 2 | $this->warnings[] = sprintf( |
|
73 | 2 | '"commit-auto" is set to "%s" but "commit-bin-file" was not specified.', |
|
74 | $commitAuto |
||
75 | 2 | ); |
|
76 | 2 | } |
|
77 | } |
||
78 | |||
79 | 22 | if (array_key_exists('commit-message', $extra)) { |
|
80 | 2 | if (strlen(trim($extra['commit-message'])) === 0) { |
|
81 | $this->warnings[] = '"commit-message" is specified but empty. Ignoring and using default commit message.'; |
||
82 | } else { |
||
83 | 2 | $commitMessage = $extra['commit-message']; |
|
84 | } |
||
85 | 2 | } |
|
86 | |||
87 | 22 | return new Config($commitAuto, $commitBinFile, $commitMessage); |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return string[] |
||
92 | */ |
||
93 | 22 | public function getWarnings() |
|
97 | |||
98 | 22 | private function reset() |
|
102 | |||
103 | /** |
||
104 | * @param array $extra |
||
105 | * @param string $key |
||
106 | * @param mixed $default |
||
107 | * @param string $additionalMessage |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | 2 | private static function createWarningFromInvalidValue(array $extra, $key, $default, $additionalMessage = '') |
|
126 | } |
||
127 |