0

I have a custom observer with different method definitions:

  1. Should the observer include only execute method?

  2. If i can include all the methods, how?

Vendor\Module\Observer\SyncObserver.php

<?php
namespace Vendor\Module\Observer;

use \Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class SyncObserver implements ObserverInterface
{
 protected $_queue;

/**
 * @var \Vendor\Module\Model\QueueFactory
 */
protected $syncQueueFactory;

/**
 * @var \Psr\Log\LoggerInterface
 */
protected $logger;

function __construct(
    \Vendor\Module\Model\QueueFactory $syncQueueFactory,
    \Psr\Log\LoggerInterface $logger
)
{
    $this->syncQueueFactory = $syncQueueFactory;
    $this->logger = $logger;
    $this->_queue = $this->syncQueueFactory->create();
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    //echo 'Sync obssrexe';die;
    $event = $observer->getEvent();
    //$this->tglssearchEngineFactory->getCatlogSearchResult();
}

//How to include these methods here... if i just put it here, they are no getting called

public function method1(\Magento\Framework\Event\Observer $observer)
{...}

public function method2(\Magento\Framework\Event\Observer $observer)
{...}

UPDATED

Can i use something like this?

function __construct(
    \Vendor\module\Model\QueueFactory $syncQueueFactory,
    \Psr\Log\LoggerInterface $logger
)
{
    $this->syncQueueFactory = $syncQueueFactory;
    $this->logger = $logger;
    $this->_queue = $this->syncQueueFactory->create();
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    //echo 'Sync obssrexe';die;
    $event = $observer->getEvent();
    $this->syncQueueFactory->productUpdate();
}

My QueueFactory:

public function productUpdate(\Magento\Framework\Event\Observer $observer)
{...}
1

Why do you need the other methods?
If you need to call them just inside your execute method you should make them protected.
If you are planning to use them outside the class also, maybe you need to rethink your architecture.
The scope of an observer is to so something when a certain event is dispatched.
If you want it to do something else, you are breaking the single responsibility principle.
In this case you may want to move your other methods in an other class.

  • Can i use like above,i updated the answer – Sushivam Jul 21 '16 at 8:18
  • I see that the method productUpdate expects a parameter. You can call it in your observer like this $this->syncQueueFactory->productUpdate($this); but maybe you should change the parameter type from \Magento\Framework\Event\Observer to \Magento\Framework\Event\ObserverInterface – Marius Jul 21 '16 at 8:20
  • what if there multiple methods in QueueFactory?, how to call it in observer using syncQueueFactory – Sushivam Jul 21 '16 at 8:22
  • You lost me here. Maybe you should explain in the question what you are trying to achieve. – Marius Jul 21 '16 at 8:24
  • Im just trying to add the product id to my custom table when any change is made to the product :P – Sushivam Jul 21 '16 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.