Skip to content

Commit

Permalink
Fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
sten authored and github-actions[bot] committed Sep 5, 2022
1 parent b36a50f commit f5d0728
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/Commands/SurveyheroMapperCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function handle(): int
$fileName = 'surveyhero_mapping.php';
$myfile = fopen($fileName, 'w') or exit('Unable to open file!');

fwrite($myfile, "<?php \n\n" . $this->var_export_short($mapping) . "; \n");
fwrite($myfile, "<?php \n\n".$this->var_export_short($mapping)."; \n");
fclose($myfile);

$this->comment("Mapping complete! [$fileName]");
Expand Down
5 changes: 3 additions & 2 deletions src/Exceptions/QuestionMapperNotImplementedException.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace Statikbe\Surveyhero\Exceptions;
namespace Statikbe\Surveyhero\Exceptions;

class QuestionMapperNotImplementedException extends \Exception {
class QuestionMapperNotImplementedException extends \Exception
{
public string $questionType;

public static function create(string $questionType): self
Expand Down
1 change: 1 addition & 0 deletions src/Models/SurveyAnswer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class SurveyAnswer extends Model
use HasTranslations;

const CONVERTED_TYPE_INT = 'int';

const CONVERTED_TYPE_STRING = 'string';

protected $translatable = ['label'];
Expand Down
18 changes: 9 additions & 9 deletions src/Services/Factories/QuestionMapper/AbstractQuestionMapper.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php

namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper;
namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper;

use Statikbe\Surveyhero\Models\SurveyAnswer;

abstract class AbstractQuestionMapper implements QuestionMapper {
abstract class AbstractQuestionMapper implements QuestionMapper
{
/**
* @param int $questionId
* @param string $questionType
* @param string $mappedDataType
* @param int|string $questionFieldSuffix
* @param int $questionId
* @param string $questionType
* @param string $mappedDataType
* @param int|string $questionFieldSuffix
* @return array{'question_id': int, 'type': string, 'field': string, 'mapped_data_type': string }
*/
public function createQuestionMap(int $questionId, string $questionType, string $mappedDataType, int|string $questionFieldSuffix): array {
public function createQuestionMap(int $questionId, string $questionType, string $mappedDataType, int|string $questionFieldSuffix): array
{
return [
'question_id' => $questionId,
'type' => $questionType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php

namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper;
namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper;

use Statikbe\Surveyhero\Models\SurveyAnswer;
use Statikbe\Surveyhero\Services\Factories\QuestionAndAnswerCreator\ChoiceListQuestionAndAnswerCreator;

class ChoiceListQuestionMapper extends AbstractQuestionMapper {
class ChoiceListQuestionMapper extends AbstractQuestionMapper
{
const TYPE = 'choice_list';

public function mapQuestion(\stdClass $question, int $questionCounter): array {
public function mapQuestion(\stdClass $question, int $questionCounter): array
{
$questionData = $this->createQuestionMap($question->element_id,
$question->question->type,
SurveyAnswer::CONVERTED_TYPE_INT,
Expand All @@ -17,6 +18,7 @@ public function mapQuestion(\stdClass $question, int $questionCounter): array {
foreach ($question->question->choice_list->choices as $choiceKey => $choice) {
$questionData['answer_mapping'][$choice->choice_id] = $choiceKey + 1;
}

return $questionData;
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
<?php

namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper;
namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper;

use Statikbe\Surveyhero\Models\SurveyAnswer;

class ChoiceTableQuestionMapper extends AbstractQuestionMapper {
class ChoiceTableQuestionMapper extends AbstractQuestionMapper
{
const TYPE = 'choice_table';

public function mapQuestion(\stdClass $question, int $questionCounter): array {
public function mapQuestion(\stdClass $question, int $questionCounter): array
{
$mappedQuestions = [];
$subquestionIndex = 1;
// make answer mapping which is the same for each question:
$answerMapping = [];
$choiceCounter = 1;
foreach($question->question->choice_table->choices as $questionChoice){
foreach ($question->question->choice_table->choices as $questionChoice) {
$answerMapping[$questionChoice->choice_id] = $choiceCounter;
$choiceCounter ++;
$choiceCounter++;
}

//create subquestions:
foreach ($question->question->choice_table->rows as $rowQuestion){
foreach ($question->question->choice_table->rows as $rowQuestion) {
$questionData = $this->createQuestionMap($rowQuestion->row_id,
$question->question->type,
SurveyAnswer::CONVERTED_TYPE_INT,
Expand Down
8 changes: 5 additions & 3 deletions src/Services/Factories/QuestionMapper/InputQuestionMapper.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper;
namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper;

use Statikbe\Surveyhero\Models\SurveyAnswer;

class InputQuestionMapper extends AbstractQuestionMapper {
class InputQuestionMapper extends AbstractQuestionMapper
{
const TYPE = 'input';

public function mapQuestion(\stdClass $question, int $questionCounter): array {
public function mapQuestion(\stdClass $question, int $questionCounter): array
{
$questionData = $this->createQuestionMap($question->element_id,
$question->question->type,
SurveyAnswer::CONVERTED_TYPE_STRING,
Expand Down
7 changes: 4 additions & 3 deletions src/Services/Factories/QuestionMapper/QuestionMapper.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper;
namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper;

interface QuestionMapper {
public function mapQuestion(\stdClass $question, int $questionCounter): array ;
interface QuestionMapper
{
public function mapQuestion(\stdClass $question, int $questionCounter): array;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper;
namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper;

use Statikbe\Surveyhero\Models\SurveyAnswer;

class RatingScaleQuestionMapper extends AbstractQuestionMapper {
class RatingScaleQuestionMapper extends AbstractQuestionMapper
{
const TYPE = 'rating_scale';

public function mapQuestion(\stdClass $question, int $questionCounter): array {
public function mapQuestion(\stdClass $question, int $questionCounter): array
{
$questionData = $this->createQuestionMap($question->element_id,
$question->question->type,
SurveyAnswer::CONVERTED_TYPE_STRING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,38 @@ protected function findExistingQuestionResponse(string|int $surveyheroQuestionId
}

/**
* @param string $surveyheroQuestionId
* @param string $surveyheroQuestionId
* @return SurveyQuestion
*
* @throws QuestionNotImportedException
*/
protected function findSurveyQuestion(string $surveyheroQuestionId): SurveyQuestion {
protected function findSurveyQuestion(string $surveyheroQuestionId): SurveyQuestion
{
$surveyQuestion = SurveyQuestion::where('surveyhero_question_id', $surveyheroQuestionId)->first();
if(!$surveyQuestion){
if (! $surveyQuestion) {
throw QuestionNotImportedException::create($surveyheroQuestionId, 'The question is not imported');
} else {
return $surveyQuestion;
}
else return $surveyQuestion;
}

protected function findSurveyAnswer(SurveyQuestion $question, string $surveyheroAnswerId): SurveyAnswer {
protected function findSurveyAnswer(SurveyQuestion $question, string $surveyheroAnswerId): SurveyAnswer
{
$surveyAnswer = SurveyAnswer::where('survey_question_id', $question->id)
->where('surveyhero_answer_id', $surveyheroAnswerId)
->first();

if (! $surveyAnswer) {
throw AnswerNotImportedException::create($surveyheroAnswerId, "Make sure to import survey answer with Surveyhero ID $surveyheroAnswerId in the survey_answers table");
} else {
return $surveyAnswer;
}
else return $surveyAnswer;
}

/**
* @param SurveyQuestion $question
* @param SurveyResponse $response
* @param SurveyAnswer|null $answer
* @param SurveyQuestion $question
* @param SurveyResponse $response
* @param SurveyAnswer|null $answer
* @return array{ 'survey_question_id': int, 'survey_response_id': int }
*/
protected function createSurveyQuestionResponseData(SurveyQuestion $question,
Expand Down Expand Up @@ -117,18 +122,18 @@ protected function setChoiceAndConvertToDataType(mixed $mappedChoice,
}
}

protected function fetchOrCreateInputAnswer(SurveyQuestion $surveyQuestion, string $answerDataType, mixed $inputAnswer): SurveyAnswer {
protected function fetchOrCreateInputAnswer(SurveyQuestion $surveyQuestion, string $answerDataType, mixed $inputAnswer): SurveyAnswer
{
//fetch or create answer:
$answerData = [];
$this->setChoiceAndConvertToDataType($this->transformInputToDataType($inputAnswer, $answerDataType),
$answerDataType,
$answerData,
null);
$answerDataType,
$answerData,
null);
$surveyAnswerQuery = SurveyAnswer::where('survey_question_id', $surveyQuestion->id);
if(isset($answerData['converted_int_value'])){
if (isset($answerData['converted_int_value'])) {
$surveyAnswerQuery->where('converted_int_value', $answerData['converted_int_value']);
}
else if(isset($answerData['converted_string_value'])){
} elseif (isset($answerData['converted_string_value'])) {
$surveyAnswerQuery->where('converted_string_value', $answerData['converted_string_value']);
}
$surveyAnswer = $surveyAnswerQuery->first();
Expand All @@ -142,8 +147,9 @@ protected function fetchOrCreateInputAnswer(SurveyQuestion $surveyQuestion, stri
return $surveyAnswer;
}

protected function transformInputToDataType(mixed $input, string $dataType){
switch($dataType){
protected function transformInputToDataType(mixed $input, string $dataType)
{
switch($dataType) {
case SurveyAnswer::CONVERTED_TYPE_INT:
return intval($input);
case SurveyAnswer::CONVERTED_TYPE_STRING:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Statikbe\Surveyhero\Services\Factories\ResponseCreator;

use Statikbe\Surveyhero\Exceptions\AnswerNotImportedException;
use Statikbe\Surveyhero\Models\SurveyAnswer;
use Statikbe\Surveyhero\Models\SurveyQuestionResponse;
use Statikbe\Surveyhero\Models\SurveyResponse;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

namespace Statikbe\Surveyhero\Services\Factories\ResponseCreator;

use Statikbe\Surveyhero\Exceptions\AnswerNotImportedException;
use Statikbe\Surveyhero\Exceptions\QuestionNotImportedException;
use Statikbe\Surveyhero\Models\SurveyAnswer;
use Statikbe\Surveyhero\Models\SurveyQuestion;
use Statikbe\Surveyhero\Models\SurveyQuestionResponse;
use Statikbe\Surveyhero\Models\SurveyResponse;

Expand Down Expand Up @@ -60,8 +56,8 @@ public function updateOrCreateQuestionResponse(
$responseData = $this->createSurveyQuestionResponseData($surveyQuestion, $response, $surveyAnswer);

$responseList[] = SurveyQuestionResponse::updateOrCreate([
'id' => $existingQuestionResponse->id ?? null,
], $responseData);
'id' => $existingQuestionResponse->id ?? null,
], $responseData);
}

return $responseList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Statikbe\Surveyhero\Services\Factories\ResponseCreator;

use Statikbe\Surveyhero\Exceptions\AnswerNotImportedException;
use Statikbe\Surveyhero\Models\SurveyAnswer;
use Statikbe\Surveyhero\Models\SurveyQuestionResponse;
use Statikbe\Surveyhero\Models\SurveyResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Statikbe\Surveyhero\Services\Factories\ResponseCreator;

use Statikbe\Surveyhero\Exceptions\AnswerNotImportedException;
use Statikbe\Surveyhero\Models\SurveyAnswer;
use Statikbe\Surveyhero\Models\SurveyQuestionResponse;
use Statikbe\Surveyhero\Models\SurveyResponse;
Expand Down
21 changes: 5 additions & 16 deletions src/Services/SurveyMappingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,11 @@
use Statikbe\Surveyhero\Exceptions\SurveyNotMappedException;
use Statikbe\Surveyhero\Http\SurveyheroClient;
use Statikbe\Surveyhero\Models\Survey;
use Statikbe\Surveyhero\Models\SurveyAnswer;
use Statikbe\Surveyhero\Services\Factories\QuestionAndAnswerCreator\ChoiceListQuestionAndAnswerCreator;
use Statikbe\Surveyhero\Services\Factories\QuestionAndAnswerCreator\ChoiceTableQuestionAndAnswerCreator;
use Statikbe\Surveyhero\Services\Factories\QuestionAndAnswerCreator\RatingScaleQuestionAndAnswerCreator;
use Statikbe\Surveyhero\Services\Factories\QuestionMapper\ChoiceListQuestionMapper;
use Statikbe\Surveyhero\Services\Factories\QuestionMapper\ChoiceTableQuestionMapper;
use Statikbe\Surveyhero\Services\Factories\QuestionMapper\InputQuestionMapper;
use Statikbe\Surveyhero\Services\Factories\QuestionMapper\QuestionMapper;
use Statikbe\Surveyhero\Services\Factories\QuestionMapper\RatingScaleQuestionMapper;
use Statikbe\Surveyhero\Services\Factories\ResponseCreator\ChoicesResponseCreator;
use Statikbe\Surveyhero\Services\Factories\ResponseCreator\ChoiceTableResponseCreator;
use Statikbe\Surveyhero\Services\Factories\ResponseCreator\NumberResponseCreator;
use Statikbe\Surveyhero\Services\Factories\ResponseCreator\QuestionResponseCreator;
use Statikbe\Surveyhero\Services\Factories\ResponseCreator\TextResponseCreator;

class SurveyMappingService
{
Expand Down Expand Up @@ -56,22 +47,20 @@ public function map(Survey $survey): array
foreach ($questions as $question) {
$mapper = $this->getQuestionMapper($question->question->type);

if($mapper) {
if ($mapper) {
//a mapper can return one question or multiple.
$mappedQuestions = $mapper->mapQuestion($question, $questionCounter);
if(!empty($mappedQuestions)){
if(is_array(array_values($mappedQuestions)[0])){
if (! empty($mappedQuestions)) {
if (is_array(array_values($mappedQuestions)[0])) {
//multiple questions mapped:
$mapping['questions'] = array_merge($mapping['questions'], $mappedQuestions);
}
}
else {
} else {
//only one question mapped:
$mapping['questions'][] = $mappedQuestions;
}
$questionCounter++;
}
else {
} else {
throw QuestionMapperNotImplementedException::create($question->question->type);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Services/SurveyResponseImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ private function createOrUpdateSurveyResponse(\stdClass $surveyheroResponse, Sur
];

//map link parameters:
if(isset($surveyheroResponse->link_parameters)) {
if (isset($surveyheroResponse->link_parameters)) {
$linkParametersConfig = config('surveyhero.surveyhero_link_parameters_mapping', []);
foreach($linkParametersConfig as $surveyheroLinkParameter => $settings) {
if(isset($surveyheroResponse->link_parameters->{$surveyheroLinkParameter})) {
if(isset($settings['entity']) && isset($settings['value']) && isset($settings['field'])) {
foreach ($linkParametersConfig as $surveyheroLinkParameter => $settings) {
if (isset($surveyheroResponse->link_parameters->{$surveyheroLinkParameter})) {
if (isset($settings['entity']) && isset($settings['value']) && isset($settings['field'])) {
//Map parameter to value of associated model
$responseData[$settings['name']] = optional($settings['entity']::where($settings['value'], $surveyheroResponse->link_parameters->{$surveyheroLinkParameter})->first())->id;
} else {
Expand Down

0 comments on commit f5d0728

Please sign in to comment.