skip to Main Content

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


  1. If you wish to use both methods from parent and child class, I would suggest to use helper class.

    ImportQuizHelper

    namespace {namespace path here}
    ...
    class ImportQuizHelper {
    
        public function info() {
    
        }
        ... 
        // move all methods here
        // you may add more methods here that can be used on both api and command
    }
    

    ImportQuizApiData

    ...
    use {namespace path here}ImportQuizHelper;
    use AppLibraryImportQuizApiData;
    class ImportQuizApiDataCommand extends Command
    {
        private $helper;
        public __construct(ImportQuizHelper $helper) {
            $this->helper = $helper;
        }
    
        protected $signature = 'app:import-quiz-api-data-command  {categoryName?}';
    
        public function handle()
        {
            ...
            $importQuizApiData = new ImportQuizApiData($this->helper); // pass helper here
            $importResult = $importQuizApiData->import();
            ...
    
            $this->helper->info(); // use info from helper
    
        }
    }
    

    ImportQuizApiData

    ...
    class ImportQuizApiData
    {
        private $helper;
        public __construct(ImportQuizHelper $helper) {
            $this->helper = $helper;
        }
    
        public function import()
        {
            ... // do your imports
            $this->helper->info(); // use info from helper
        }
    }
    

    There might be bugs or typo here since I just wrote it on the fly so just edit if needed.

    Login or Signup to reply.
  2. what you can do is :

    -have ImportQuizApiData() injected as a dependency

    -remove the setter (setParentCommand($this)) and have the import to do that (optional)

    use AppLibraryImportQuizApiData;
    ...
    class ImportQuizApiDataCommand extends Command
    {
      protected $signature = 'app:import-quiz-api-data-command  {categoryName?}';
    
      public function handle(ImportQuizApiData $importQuizApiData)
      {
        ...
        $importResult = $importQuizApiData->import($this);
        ...
      }
    }
    
    
    class ImportQuizApiData
    {
    
      protected ImportQuizApiDataCommand $parentCommand;
      ...
    
    
      public function import($parentCommand): bool
      {
        //if(! $this->parentCommand){//optional
        //  $this->parentCommand = $parentCommand;
        //}
        ...
        $currentRow = 0;
        foreach ($this->data) {
            ...
            $currentRow++;
            $parentCommand->info($currentRow . ' / ' . count($this->data) . ' - ' . (100 / count($this->data) * $currentRow) . '% ');
            //$this->parentCommand->info($currentRow . ' / ' . count($this->data) . ' - ' . (100 / count($this->data) * $currentRow) . '% ');
        } // foreach ($sourceQuizzesArray as $quiz) {  // Loop all quizzes
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search