Skip to content

Commit

Permalink
[TASK] Code Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
susannemoog committed Jul 28, 2017
1 parent 7b86383 commit 42fd7ff
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 36 deletions.
4 changes: 4 additions & 0 deletions Build/phpunit.xml
Expand Up @@ -11,6 +11,10 @@
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">../Classes/</directory>
<exclude>
<directory suffix=".php">../Classes/Bootstrap</directory>
<directory suffix=".php">../Classes/Commands</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
21 changes: 13 additions & 8 deletions Classes/Commands/BaseCommand.php
@@ -1,5 +1,5 @@
<?php
declare(strict_types = 1);
declare(strict_types=1);

namespace T3G\Elasticorn\Commands;

Expand Down Expand Up @@ -30,14 +30,14 @@ protected function initialize(InputInterface $input, OutputInterface $output)
global $container;
$verbosityLevelMap = [
LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL,
LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL,
];
$container->setParameter('logger.output', $output);
$container->setParameter('logger.verbosityMap', $verbosityLevelMap);
if ($input->hasOption('config-path') && !empty($input->getOption('config-path'))) {
$_ENV['configurationPath'] = $input->getOption('config-path');
}
if($input->hasArgument('indexName')) {
if ($input->hasArgument('indexName')) {
$container->setParameter('index.name', $input->getArgument('indexName'));
} else {
$container->setParameter('index.name', null);
Expand All @@ -48,22 +48,27 @@ protected function initialize(InputInterface $input, OutputInterface $output)

protected function configure()
{
$this->addOption('config-path', 'c', InputArgument::OPTIONAL, 'The full path to the configuration directory (may be relative)');
$this->addOption(
'config-path',
'c',
InputArgument::OPTIONAL,
'The full path to the configuration directory (may be relative)'
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
while(!(isset($_ENV['configurationPath']) && file_exists($_ENV['configurationPath']))) {
while (!(isset($_ENV['configurationPath']) && file_exists($_ENV['configurationPath']))) {
$this->askForConfigDir($input, $output);
}
$_ENV['configurationPath'] = rtrim($_ENV['configurationPath'], DIRECTORY_SEPARATOR) . '/';
}

private function askForConfigDir(InputInterface $input, OutputInterface $output)
{
$helper = $this->getHelper('question');
$question = new Question('Please enter a valid path to your configuration directory:' . "\n");
$helper = $this->getHelper('question');
$question = new Question('Please enter a valid path to your configuration directory:' . "\n");

$_ENV['configurationPath'] = $helper->ask($input, $output, $question);
$_ENV['configurationPath'] = $helper->ask($input, $output, $question);
}
}
8 changes: 5 additions & 3 deletions Classes/Commands/Index/RemapCommand.php
@@ -1,5 +1,6 @@
<?php
declare(strict_types = 1);
declare(strict_types=1);

namespace T3G\Elasticorn\Commands\Index;


Expand Down Expand Up @@ -45,15 +46,16 @@ protected function configure()
}

/**
* @param InputInterface $input
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);
$force = false;
if($input->getOption('force')) {
if ($input->getOption('force')) {
$force = true;
}
if ($input->hasArgument('indexName') && null !== $indexName = $input->getArgument('indexName')) {
Expand Down
68 changes: 47 additions & 21 deletions Classes/Utility/ConfigurationParser.php
@@ -1,5 +1,6 @@
<?php
declare(strict_types = 1);
declare(strict_types=1);

namespace T3G\Elasticorn\Utility;

use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -42,15 +43,15 @@ public function __construct(LoggerInterface $logger)
*
* @return array
*/
public function getIndexConfigurations() : array
public function getIndexConfigurations(): array
{
$indices = [];
$this->logger->info('Loading configuration from ' . $this->configFolder);
$filesInFolder = $this->getFilesInFolder($this->configFolder);
$this->logger->debug('Found: ' . var_export($filesInFolder, true));
foreach ($filesInFolder as $indexName) {
$subFolder = $this->configFolder . $indexName;
if(is_dir($subFolder) === true && file_exists($subFolder . '/' . self::INDEX_CONF_FILENAME)) {
if (is_dir($subFolder) === true && file_exists($subFolder . '/' . self::INDEX_CONF_FILENAME)) {
$indices[strtolower($indexName)] = $this->getIndexConfiguration($indexName);
}
}
Expand All @@ -61,9 +62,10 @@ public function getIndexConfigurations() : array
* Get Configuration for specified index1
*
* @param string $indexName
*
* @return array
*/
public function getIndexConfiguration(string $indexName) : array
public function getIndexConfiguration(string $indexName): array
{
$filePath = $this->getIndexDirectory($indexName) . '/' . self::INDEX_CONF_FILENAME;
return $this->getConfig($filePath);
Expand All @@ -74,28 +76,23 @@ public function getIndexConfiguration(string $indexName) : array
*
* @param string $indexName
* @param string $language
*
* @return array
*/
public function getDocumentTypeConfigurations(string $indexName, string $language = '') : array
public function getDocumentTypeConfigurations(string $indexName, string $language = ''): array
{
$configs = [];
$directory = $this->getDocumentTypesDirectory($indexName);
$configFiles = $this->getFilesInFolder($directory);
foreach ($configFiles as $configFile) {
$filePath = $directory . $configFile;
$pathInfo = pathinfo($filePath);
if($pathInfo['extension'] === 'yaml') {
if ($pathInfo['extension'] === 'yaml') {
$configs[$pathInfo['filename']] = $this->getConfig($filePath);
}
}
if ($language !== '') {
foreach ($configs as $key => $field) {
foreach ($field as $name => $config) {
if (array_key_exists('analyzer', $config)) {
$configs[$key][$name]['analyzer'] = $language;
}
}
}
$configs = $this->addAnalyzerToConfig($language, $configs);
}
return $configs;
}
Expand All @@ -104,9 +101,10 @@ public function getDocumentTypeConfigurations(string $indexName, string $languag
* Convert document type configuration to mapping as returned from elastica (for comparison for example)
*
* @param array $configurations
*
* @return array
*/
public function convertDocumentTypeConfigurationToMappingFromElastica(array $configurations) : array
public function convertDocumentTypeConfigurationToMappingFromElastica(array $configurations): array
{
$mappings = [];
foreach ($configurations as $index => $configuration) {
Expand All @@ -120,51 +118,76 @@ public function convertDocumentTypeConfigurationToMappingFromElastica(array $con
*
* @param string $indexName
* @param string $documentType
*
* @return array
*/
public function getDocumentTypeConfiguration(string $indexName, string $documentType) : array
public function getDocumentTypeConfiguration(string $indexName, string $documentType): array
{
$filePath = $this->getDocumentTypesDirectory($indexName) . strtolower($documentType) . '.yaml';
return $this->getConfig($filePath);
}

/**
* @param string $language
* @param $configs
*
* @return mixed
*/
private function addAnalyzerToConfig(string $language, $configs)
{
foreach ($configs as $key => $field) {
foreach ($field as $name => $config) {
if (array_key_exists('analyzer', $config)) {
$configs[$key][$name]['analyzer'] = $language;
}
}
}
return $configs;
}

/**
* @param string $indexName
*
* @return string
*/
private function getDocumentTypesDirectory(string $indexName) : string
private function getDocumentTypesDirectory(string $indexName): string
{
return $this->getIndexDirectory($indexName) . '/DocumentTypes/';
}

/**
* @param string $directory
*
* @return array
*/
private function getFilesInFolder(string $directory) : array
private function getFilesInFolder(string $directory): array
{
return array_diff(scandir($directory), ['..', '.']);
}

/**
* @param string $indexName
*
* @return string
*/
private function getIndexDirectory(string $indexName) : string
private function getIndexDirectory(string $indexName): string
{
$indexDir = $this->configFolder . $indexName;
if (!file_exists($indexDir)) {
throw new \InvalidArgumentException('Configuration directory ' . $indexDir . ' for index ' . $indexName . ' does not exist.', 666);
throw new \InvalidArgumentException(
'Configuration directory ' . $indexDir . ' for index ' . $indexName . ' does not exist.', 666
);
}
return $indexDir;
}

/**
* @param string $filePath
*
* @return array
* @throws \InvalidArgumentException
*/
private function getConfig(string $filePath) : array
private function getConfig(string $filePath): array
{
if (!file_exists($filePath)) {
throw new \InvalidArgumentException('No configuration found at ' . $filePath, 666);
Expand All @@ -180,7 +203,10 @@ public function createConfigurationForIndex(string $indexName, array $mapping, a
$documentTypesDirectory = $this->getDocumentTypesDirectory($indexName);
file_put_contents($indexDirectory . '/IndexConfiguration.yaml', Yaml::dump($settings));
foreach ($mapping as $documentType => $mappingConfig) {
file_put_contents($documentTypesDirectory . '/' . $documentType . '.yaml', Yaml::dump($mappingConfig['properties']));
file_put_contents(
$documentTypesDirectory . '/' . $documentType . '.yaml',
Yaml::dump($mappingConfig['properties'])
);
}
}

Expand Down
8 changes: 4 additions & 4 deletions bootstrap.php
@@ -1,5 +1,5 @@
<?php
declare(strict_types = 1);
declare(strict_types=1);

use T3G\Elasticorn\Commands\Index\CornifyCommand;
use T3G\Elasticorn\Commands\Mapping\CompareCommand;
Expand All @@ -12,7 +12,7 @@
// env config
// Determine the .env file in package directory ($baseBath === __DIR__) and getcwd()
// this prevent path errors in case of global composer installation and package requirement
foreach([$basePath, getcwd()] as $directory) {
foreach ([$basePath, getcwd()] as $directory) {
if (file_exists($directory . DIRECTORY_SEPARATOR . '.env')) {
$dotenv = new Dotenv\Dotenv($directory);
$dotenv->load();
Expand All @@ -36,14 +36,14 @@
$application->add(new ShowCommand());
$application->add(new CornifyCommand());

if(true === $phar) {
if (true === $phar) {
$application->add(new UpdateCommand());
$application->add(new RollbackCommand());
}


$application->setName(
<<<ASCIIART
<<<ASCIIART
Elasticorn!
\
\
Expand Down

0 comments on commit 42fd7ff

Please sign in to comment.