Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review: 1257-validate-existing-iati-response-validator #1260

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions app/Console/Commands/ValidateOldActivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace App\Console\Commands;

use App\IATI\Services\Workflow\BulkPublishingService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class ValidateOldActivity extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'validate:activity';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$activityId = $this->askValid(
'Please enter the activity ids which you want to validate separated by comma (Compulsory)',
'activityId',
['required']
);

// $activityId = [];
// DB::table('iati_validator_responses')
// ->whereJsonDoesntContainKey('response->errors->0->details')
// ->chunkById(10, function($validators) use (&$activityId) {
// foreach($validators as $validator)
// {
// $response = json_decode($validator->response,true);
//
// if((isset($response['errors']) && empty($response['errors'])) || isset($response['errors'][0]['details']))
// {
// continue;
// }
//
// $activityId[] = $validator->activity_id;
// }
// });
$activityId = explode(',', $activityId);
$activityWorkFlowController = app()->make(BulkPublishingService::class);
$activityWorkFlowController->validateActivitiesOnIATI($activityId);
}

/**
* Validates input given by user.
*
* @param $rules
* @param $fieldName
* @param $value
*
* @return string|null
*/
protected function validateInput($rules, $fieldName, $value): ?string
{
$validator = Validator::make([
$fieldName => $value,
], [
$fieldName => $rules,
]);

return $validator->fails()
? $validator->errors()->first($fieldName)
: null;
}

/**
* Ask input from user and return value.
*
* @param $question
* @param $field
* @param $rules
*
* @return string
*/
protected function askValid($question, $field, $rules): string
{
$value = $this->ask($question);
$message = $this->validateInput($rules, $field, $value);

if ($message) {
$this->error($message);

return $this->askValid($question, $field, $rules);
}

return $value;
}
}
Loading