Total Complexity | 45 |
Total Lines | 242 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AbstractCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class AbstractCommand extends Command |
||
34 | { |
||
35 | public const CONFIG_FILE = ['cecil.yml', 'config.yml']; |
||
36 | public const TMP_DIR = '.cecil'; |
||
37 | public const THEME_CONFIG_FILE = 'config.yml'; |
||
38 | |||
39 | /** @var InputInterface */ |
||
40 | protected $input; |
||
41 | |||
42 | /** @var OutputInterface */ |
||
43 | protected $output; |
||
44 | |||
45 | /** @var SymfonyStyle */ |
||
46 | protected $io; |
||
47 | |||
48 | /** @var null|string */ |
||
49 | private $path = null; |
||
50 | |||
51 | /** @var array */ |
||
52 | private $configFiles; |
||
53 | |||
54 | /** @var array */ |
||
55 | private $config; |
||
56 | |||
57 | /** @var Builder */ |
||
58 | private $builder; |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
64 | { |
||
65 | $this->input = $input; |
||
66 | $this->output = $output; |
||
67 | $this->io = new SymfonyStyle($input, $output); |
||
68 | |||
69 | // set up configuration |
||
70 | if (!\in_array($this->getName(), ['new:site', 'self-update'])) { |
||
71 | // default configuration file |
||
72 | $this->configFiles[$this->findConfigFile('name')] = $this->findConfigFile('path'); |
||
73 | // from --config=<file> |
||
74 | if ($input->hasOption('config') && $input->getOption('config') !== null) { |
||
75 | foreach (explode(',', (string) $input->getOption('config')) as $configFile) { |
||
76 | $this->configFiles[$configFile] = realpath($configFile); |
||
77 | if (!Util\File::getFS()->isAbsolutePath($configFile)) { |
||
78 | $this->configFiles[$configFile] = realpath(Util::joinFile($this->getPath(), $configFile)); |
||
79 | } |
||
80 | } |
||
81 | $this->configFiles = array_unique($this->configFiles); |
||
82 | } |
||
83 | // checks file(s) |
||
84 | foreach ($this->configFiles as $fileName => $filePath) { |
||
85 | if ($filePath === false || !file_exists($filePath)) { |
||
86 | unset($this->configFiles[$fileName]); |
||
87 | $this->getBuilder()->getLogger()->error(\sprintf('Could not find configuration file "%s".', $fileName)); |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | |||
92 | parent::initialize($input, $output); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | public function run(InputInterface $input, OutputInterface $output): int |
||
99 | { |
||
100 | // disable debug mode if a verbosity level is specified |
||
101 | if ($output->getVerbosity() != OutputInterface::VERBOSITY_NORMAL) { |
||
102 | putenv('CECIL_DEBUG=false'); |
||
103 | } |
||
104 | // force verbosity level to "debug" in debug mode |
||
105 | if (getenv('CECIL_DEBUG') == 'true') { |
||
106 | $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); |
||
107 | } |
||
108 | if ($output->isDebug()) { |
||
109 | // set env. variable in debug mode |
||
110 | putenv('CECIL_DEBUG=true'); |
||
111 | |||
112 | return parent::run($input, $output); |
||
113 | } |
||
114 | // simplified error message |
||
115 | try { |
||
116 | return parent::run($input, $output); |
||
117 | } catch (\Exception $e) { |
||
118 | if ($this->io === null) { |
||
119 | $this->io = new SymfonyStyle($input, $output); |
||
120 | } |
||
121 | $this->io->error($e->getMessage()); |
||
122 | |||
123 | exit(1); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Returns the working path. |
||
129 | */ |
||
130 | protected function getPath(bool $exist = true): ?string |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Returns the configuration file name or path, if file exists, otherwise default name or false. |
||
159 | */ |
||
160 | protected function findConfigFile(string $nameOrPath): string|false |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Returns config file(s) path. |
||
180 | */ |
||
181 | protected function getConfigFiles(): ?array |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Creates or returns a Builder instance. |
||
188 | * |
||
189 | * @throws RuntimeException |
||
190 | */ |
||
191 | protected function getBuilder(array $config = []): Builder |
||
192 | { |
||
193 | try { |
||
194 | // config |
||
195 | if ($this->config === null) { |
||
196 | $filesConfig = []; |
||
197 | foreach ($this->getConfigFiles() as $fileName => $filePath) { |
||
198 | if ($filePath === false || false === $configContent = Util\File::fileGetContents($filePath)) { |
||
199 | throw new RuntimeException(\sprintf('Can\'t read configuration file "%s".', $fileName)); |
||
200 | } |
||
201 | try { |
||
202 | $filesConfig = array_replace_recursive($filesConfig, (array) Yaml::parse($configContent, Yaml::PARSE_DATETIME)); |
||
203 | } catch (ParseException $e) { |
||
204 | throw new RuntimeException(\sprintf('"%s" parsing error: %s', $filePath, $e->getMessage())); |
||
205 | } |
||
206 | } |
||
207 | $this->config = array_replace_recursive($filesConfig, $config); |
||
208 | } |
||
209 | // builder |
||
210 | if ($this->builder === null) { |
||
211 | $this->builder = (new Builder($this->config, new ConsoleLogger($this->output))) |
||
212 | ->setSourceDir($this->getPath()) |
||
213 | ->setDestinationDir($this->getPath()); |
||
214 | // import themes config |
||
215 | $themes = (array) $this->builder->getConfig()->getTheme(); |
||
216 | foreach ($themes as $theme) { |
||
217 | $themeConfigFile = Util::joinFile($this->builder->getConfig()->getThemesPath(), $theme, self::THEME_CONFIG_FILE); |
||
218 | if (Util\File::getFS()->exists($themeConfigFile)) { |
||
219 | if (false === $themeConfigFile = Util\File::fileGetContents($themeConfigFile)) { |
||
220 | throw new ConfigException(\sprintf('Can\'t read file "%s/%s/%s".', (string) $this->builder->getConfig()->get('themes.dir'), $theme, self::THEME_CONFIG_FILE)); |
||
221 | } |
||
222 | $themeConfig = Yaml::parse($themeConfigFile, Yaml::PARSE_DATETIME); |
||
223 | $this->builder->getConfig()->import($themeConfig, Config::PRESERVE); |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | } catch (\Exception $e) { |
||
228 | throw new RuntimeException($e->getMessage()); |
||
229 | } |
||
230 | |||
231 | return $this->builder; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Opens path with editor. |
||
236 | * |
||
237 | * @throws RuntimeException |
||
238 | */ |
||
239 | protected function openEditor(string $path, string $editor): void |
||
240 | { |
||
241 | $command = \sprintf('%s "%s"', $editor, $path); |
||
242 | switch (Util\Platform::getOS()) { |
||
243 | case Util\Platform::OS_WIN: |
||
244 | $command = \sprintf('start /B "" %s "%s"', $editor, $path); |
||
245 | break; |
||
246 | case Util\Platform::OS_OSX: |
||
247 | // Typora on macOS |
||
248 | if ($editor == 'typora') { |
||
249 | $command = \sprintf('open -a typora "%s"', $path); |
||
250 | } |
||
251 | break; |
||
252 | } |
||
253 | $process = Process::fromShellCommandline($command); |
||
254 | $process->run(); |
||
255 | if (!$process->isSuccessful()) { |
||
256 | throw new RuntimeException(\sprintf('Can\'t use "%s" editor.', $editor)); |
||
257 | } |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Validate URL. |
||
262 | * |
||
263 | * @throws RuntimeException |
||
264 | */ |
||
265 | public static function validateUrl(string $url): string |
||
275 | } |
||
276 | } |
||
277 |