Skip to content

Commit

Permalink
38234 refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
yeneastgate committed Aug 10, 2023
1 parent 282f4ba commit 065cfce
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 182 deletions.
103 changes: 0 additions & 103 deletions plugin/Field/DefaultValue/DefaultValueRead.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

namespace onOffice\WPlugin\Field\DefaultValue;

use onOffice\WPlugin\Types\Field;
use wpdb;
use const OBJECT;
use function esc_sql;
Expand All @@ -49,21 +48,6 @@ public function __construct(wpdb $pWPDB)
$this->_pWPDB = $pWPDB;
}

/**
* @param int $formId
* @param Field $pField
* @return DefaultValueModelSingleselect
*/
public function readDefaultValuesSingleselect(int $formId, Field $pField): DefaultValueModelSingleselect
{
$query = $this->createBaseQuery($formId, $pField);
$row = $this->_pWPDB->get_row($query, ARRAY_A);
$pDataModel = new DefaultValueModelSingleselect($formId, $pField);
$pDataModel->setDefaultsId(!empty($row['defaults_id']) ? (int)$row['defaults_id'] : 0);
$pDataModel->setValue(!empty($row['value']) ? $row['value'] : '');
return $pDataModel;
}

/**
* @param int $formId
* @param array $pFields
Expand All @@ -78,93 +62,6 @@ public function readDefaultMultiValuesSingleSelect(int $formId, array $pFields):
return $this->_pWPDB->get_results($query, OBJECT);
}

/**
* @param int $formId
* @param Field $pField
* @return DefaultValueModelMultiselect
*/
public function readDefaultValuesMultiSelect(int $formId, Field $pField): DefaultValueModelMultiselect
{
$query = $this->createBaseQuery($formId, $pField);
$rows = $this->_pWPDB->get_results($query, ARRAY_A);
$values = array_column($rows, 'value');
$pDataModel = new DefaultValueModelMultiselect($formId, $pField);
$pDataModel->setValues($values);
return $pDataModel;
}

/**
* @param int $formId
* @param Field $pField
* @return DefaultValueModelBool
*/
public function readDefaultValuesBool(int $formId, Field $pField): DefaultValueModelBool
{
$query = $this->createBaseQuery($formId, $pField);
$row = $this->_pWPDB->get_row($query, ARRAY_A);
$pDataModel = new DefaultValueModelBool($formId, $pField);
$pDataModel->setDefaultsId(!empty($row['defaults_id']) ? (int)$row['defaults_id'] : 0);
$pDataModel->setValue(!empty($row['value']) ? (bool)intval($row['value']) : false);

return $pDataModel;
}

/**
* @param int $formId
* @param Field $pField
* @return DefaultValueModelText
*/
public function readDefaultValuesText(int $formId, Field $pField): DefaultValueModelText
{
$query = $this->createBaseQuery($formId, $pField);
$rows = $this->_pWPDB->get_results($query, OBJECT);
$pDataModel = new DefaultValueModelText($formId, $pField);

foreach ($rows as $pRow) {
$pDataModel->addValueByLocale($pRow->locale, $pRow->value);
}

return $pDataModel;
}

/**
* @param int $formId
* @param Field $pField
* @return DefaultValueModelNumericRange
*/
public function readDefaultValuesNumericRange(int $formId, Field $pField): DefaultValueModelNumericRange
{
$query = $this->createBaseQuery($formId, $pField);
$rows = $this->_pWPDB->get_results($query, OBJECT);
$pDataModel = new DefaultValueModelNumericRange($formId, $pField);

if (count($rows) === 2) {
$pDataModel->setValueFrom((float)$rows[0]->value);
$pDataModel->setValueTo((float)$rows[1]->value);
}
return $pDataModel;
}

/**
* @param int $formId
* @param Field $pField
* @return string
*/
private function createBaseQuery(int $formId, Field $pField): string
{
$prefix = $this->_pWPDB->prefix;
$query = "SELECT {$prefix}oo_plugin_fieldconfig_form_defaults.defaults_id,"
."{$prefix}oo_plugin_fieldconfig_form_defaults_values.locale,\n"
."{$prefix}oo_plugin_fieldconfig_form_defaults_values.value\n"
."FROM {$prefix}oo_plugin_fieldconfig_form_defaults\n"
."INNER JOIN {$prefix}oo_plugin_fieldconfig_form_defaults_values\n"
."ON {$prefix}oo_plugin_fieldconfig_form_defaults.defaults_id = "
." {$prefix}oo_plugin_fieldconfig_form_defaults_values.defaults_id\n"
."WHERE {$prefix}oo_plugin_fieldconfig_form_defaults.fieldname = '".esc_sql($pField->getName())."' AND\n"
." {$prefix}oo_plugin_fieldconfig_form_defaults.form_id = ".esc_sql($formId);
return $query;
}

/**
* @param int $formId
* @param array $pFieldNames
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function getConvertedMultiFields(int $formId, array $pFields): array
case FieldTypes::isDateOrDateTime($pField->getType()):
case FieldTypes::isNumericType($pField->getType()):
case $pField->getType() === FieldTypes::FIELD_TYPE_SINGLESELECT:
$pDataModel = $this->createDefaultValueModelSingleSelect($formId, $pField, $rowData);
$pDataModel = $this->createDefaultValueSingleSelect($formId, $pField, $rowData);
$pDataModels[ $pField->getName() ] = $pDataModel[0] ?? '';
break;
case $pField->getType() === FieldTypes::FIELD_TYPE_MULTISELECT;
Expand Down Expand Up @@ -139,7 +139,7 @@ public function getConvertedMultiFieldsForAdmin(int $formId, array $pFields): ar
case FieldTypes::isDateOrDateTime($pField->getType()):
case FieldTypes::isNumericType($pField->getType()):
case $pField->getType() === FieldTypes::FIELD_TYPE_SINGLESELECT:
$pDataModel = $this->createDefaultValueModelSingleSelect($formId, $pField, $rowData);
$pDataModel = $this->createDefaultValueSingleSelect($formId, $pField, $rowData);
break;
case $pField->getType() === FieldTypes::FIELD_TYPE_MULTISELECT;
$pDataModel = $this->createDefaultValuesMultiSelect($formId, $pField, $rowData);
Expand Down Expand Up @@ -191,7 +191,7 @@ private function createDefaultValuesNumericRange(int $formId, Field $pField, arr
* @throws DependencyException
* @throws NotFoundException
*/
private function createDefaultValueModelSingleSelect(int $formId, Field $pField, array $rows): array
private function createDefaultValueSingleSelect(int $formId, Field $pField, array $rows): array
{
$pDataModel = new DefaultValueModelSingleselect($formId, $pField);
$pDataModel->setDefaultsId(isset($rows[0]->defaults_id) ? (int) $rows[0]->defaults_id : 0);
Expand Down
76 changes: 0 additions & 76 deletions tests/TestClassDefaultValueRead.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,6 @@ public function prepare()
*
*/

public function testReadDefaultValuesSingleselect(int $formId, int $defaultValueId, string $value)
{
$row = [
'defaults_id' => $defaultValueId,
'value' => $value,
];
$this->_pWPDBMock->expects($this->once())->method('get_row')->will($this->returnValue($row));
$pField = new Field('testField', 'testModule');
$pExpectedDataModel = new DefaultValueModelSingleselect($formId, $pField);
$pExpectedDataModel->setValue($value);
$pExpectedDataModel->setDefaultsId($defaultValueId);
$pResult = $this->_pSubject->readDefaultValuesSingleselect($formId, $pField);
$this->assertInstanceOf(DefaultValueModelSingleselect::class, $pResult);
$this->assertEquals($pExpectedDataModel, $pResult);
}


/**
*
* @return array
Expand All @@ -105,21 +88,6 @@ public function dataProviderSingleSelect(): array
];
}

/**
* @dataProvider dataProviderMultiSelect
* @param int $formId
* @param array $rows
* @param DefaultValueModelMultiselect $pReference
*/
public function testReadDefaultValuesMultiSelect(int $formId, array $rows, DefaultValueModelMultiselect $pReference)
{
$this->_pWPDBMock->expects($this->once())->method('get_results')->will($this->returnValue($rows));
$pField = new Field('testField', 'testModule');
$pResult = $this->_pSubject->readDefaultValuesMultiSelect($formId, $pField);
$this->assertInstanceOf(DefaultValueModelMultiselect::class, $pResult);
$this->assertEquals($pReference, $pResult);
}

/**
* @return Generator
*/
Expand Down Expand Up @@ -160,16 +128,6 @@ public function dataProviderMultiSelect(): Generator
*
*/

public function testReadDefaultValuesText(int $formId, array $rows, DefaultValueModelText $pReference)
{
$this->_pWPDBMock->expects($this->once())->method('get_results')->will($this->returnValue($rows));
$pField = new Field('testField', 'testModule');

$pResult = $this->_pSubject->readDefaultValuesText($formId, $pField);
$this->assertInstanceOf(DefaultValueModelText::class, $pResult);
$this->assertEquals($pReference, $pResult);
}

/**
*
* @return Generator
Expand Down Expand Up @@ -206,22 +164,6 @@ public function dataProviderText(): Generator
yield [13, $rows, $pReference2];
}

/**
* @dataProvider dataProviderNumericRange
* @param int $formId
* @param array $rows
* @param DefaultValueModelNumericRange $pReference
*/
public function testReadDefaultValuesNumericRange(int $formId, array $rows, DefaultValueModelNumericRange $pReference)
{
$this->_pWPDBMock->expects($this->once())->method('get_results')->will($this->returnValue($rows));
$pField = new Field('testField', 'testModule');

$pResult = $this->_pSubject->readDefaultValuesNumericRange($formId, $pField);
$this->assertInstanceOf(DefaultValueModelNumericRange::class, $pResult);
$this->assertEquals($pReference, $pResult);
}

/**
* @return array
*/
Expand All @@ -247,24 +189,6 @@ public function dataProviderNumericRange(): array
];
}

/**
* @dataProvider dataProviderBool
* @param int $formId
* @param array $row
* @param bool $expectedResult
*/
public function testReadDefaultValuesBool(int $formId, array $row, bool $expectedResult)
{
$this->_pWPDBMock->expects($this->once())->method('get_row')->will($this->returnValue($row));
$pField = new Field('testField', 'testModule');
$pExpectedDataModel = new DefaultValueModelBool($formId, $pField);
$pExpectedDataModel->setValue($expectedResult);
$pExpectedDataModel->setDefaultsId((int)$row['defaults_id']);
$pResult = $this->_pSubject->readDefaultValuesBool($formId, $pField);
$this->assertInstanceOf(DefaultValueModelBool::class, $pResult);
$this->assertEquals($pExpectedDataModel, $pResult);
}

/**
* @return array
*/
Expand Down

0 comments on commit 065cfce

Please sign in to comment.