In my console command I call ImportQuizApiData method :
use AppLibraryImportQuizApiData;
...
class ImportQuizApiDataCommand extends Command
{
protected $signature = 'app:import-quiz-api-data-command {categoryName?}';
public function handle()
{
...
$importQuizApiData = new ImportQuizApiData();
$importQuizApiData->setParentCommand($this);
$importResult = $importQuizApiData->import();
...
Inside of import method of ImportQuizApiData object I show current process in console :
class ImportQuizApiData
{
protected ImportQuizApiDataCommand $parentCommand;
...
public function setParentCommand(ImportQuizApiDataCommand $value): self
{
$this->parentCommand = $value;
return $this;
}
public function import(): bool
{
...
$currentRow = 0;
foreach ($this->data) {
...
$currentRow++;
$this->parentCommand->info($currentRow . ' / ' . count($this->data) . ' - ' . (100 / count($this->data) * $currentRow) . '% ');
} // foreach ($sourceQuizzesArray as $quiz) { // Loop all quizzes
I dislike a way as I passed $parentCommand var into ImportQuizApiData object, but how can I do it in a better way ?
I know how to use events / listeners – but looks like it is not helpfull here…
2
Answers
If you wish to use both methods from parent and child class, I would suggest to use helper class.
ImportQuizHelper
ImportQuizApiData
ImportQuizApiData
There might be bugs or typo here since I just wrote it on the fly so just edit if needed.
what you can do is :
-have ImportQuizApiData() injected as a dependency
-remove the setter (setParentCommand($this)) and have the import to do that (optional)