0

Trying to override authenticate method of \Magento\Customer\Model\AccountManagement. Here is the code:

namespace namespace\module\Model\Customer;

class AccountManagement extends \Magento\Customer\Model\AccountManagement
{
    private $customerFactory;
    private $validationResultsDataFactory;
    private $eventManager;
    private $storeManager;
    private $mathRandom;
    private $validator;
    private $addressRepository;
    private $customerMetadataService;
    protected $logger;
    private $encryptor;
    private $customerRegistry;
    private $configShare;
    protected $stringHelper;
    private $customerRepository;
    private $scopeConfig;
    private $transportBuilder;
    protected $dataProcessor;
    protected $registry;
    protected $customerViewHelper;
    protected $dateTime;
    protected $objectFactory;
    protected $extensibleDataObjectConverter;
    protected $customerModel;
    protected $authentication;
    private $emailNotification;
    private $eavValidator;

    public function __construct(
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        ManagerInterface $eventManager,
        StoreManagerInterface $storeManager,
        Random $mathRandom,
        Validator $validator,
        ValidationResultsInterfaceFactory $validationResultsDataFactory,
        AddressRepositoryInterface $addressRepository,
        CustomerMetadataInterface $customerMetadataService,
        CustomerRegistry $customerRegistry,
        PsrLogger $logger,
        Encryptor $encryptor,
        ConfigShare $configShare,
        StringHelper $stringHelper,
        CustomerRepositoryInterface $customerRepository,
        ScopeConfigInterface $scopeConfig,
        TransportBuilder $transportBuilder,
        DataObjectProcessor $dataProcessor,
        Registry $registry,
        CustomerViewHelper $customerViewHelper,
        DateTime $dateTime,
        CustomerModel $customerModel,
        ObjectFactory $objectFactory,
        ExtensibleDataObjectConverter $extensibleDataObjectConverter
    ) {
        $this->customerFactory = $customerFactory;
        $this->eventManager = $eventManager;
        $this->storeManager = $storeManager;
        $this->mathRandom = $mathRandom;
        $this->validator = $validator;
        $this->validationResultsDataFactory = $validationResultsDataFactory;
        $this->addressRepository = $addressRepository;
        $this->customerMetadataService = $customerMetadataService;
        $this->customerRegistry = $customerRegistry;
        $this->logger = $logger;
        $this->encryptor = $encryptor;
        $this->configShare = $configShare;
        $this->stringHelper = $stringHelper;
        $this->customerRepository = $customerRepository;
        $this->scopeConfig = $scopeConfig;
        $this->transportBuilder = $transportBuilder;
        $this->dataProcessor = $dataProcessor;
        $this->registry = $registry;
        $this->customerViewHelper = $customerViewHelper;
        $this->dateTime = $dateTime;
        $this->customerModel = $customerModel;
        $this->objectFactory = $objectFactory;
        $this->extensibleDataObjectConverter = $extensibleDataObjectConverter;
        parent::__construct($customerFactory,$eventManager,$storeManager, $mathRandom,$validator,$validationResultsDataFactory,
            $addressRepository,$customerMetadataService ,$customerRegistry,$logger,$encryptor,$configShare,$stringHelper,$customerRepository,
            $scopeConfig,$transportBuilder,$dataProcessor,$registry,$customerViewHelper,$dateTime,$customerModel,$objectFactory,$extensibleDataObjectConverter);
    }

But i am getting this error

Fatal error: Uncaught TypeError: Argument 1 passed to namespace\module\Model\Customer\AccountManagement::__construct() must be an instance of namespace\module\Model\Customer\CustomerFactory, instance of Magento\Framework\Event\Manager\Proxy given.

I think the reason is its a factory class or private. Any ideas?

7
  • Why you try overwrite whole class instead of plugin?
    – Sohel Rana
    Jan 20, 2017 at 16:00
  • I need to do something inside that function which cant be done using before or after or around. Jan 20, 2017 at 16:05
  • Can you share your need?
    – Sohel Rana
    Jan 20, 2017 at 16:06
  • Thats the not important here i guess. Right now its a curiosity too you can say. It has to work. It work for other controllers. But why not here. Jan 20, 2017 at 16:10
  • You didn't specify full class name for CustomerFactory. It must be \Magento\Customer\Model\CustomerFactory but ObjectManager looking for namespace\module\Model\Customer\CustomerFactory instead. Not sure about why it passes Proxy class instead but you definitely need to fix class name.
    – yaronish
    Jan 20, 2017 at 16:22

1 Answer 1

3

Try following way:

<?php
namespace Vendor\Module\Model;

use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;

class AccountManagement extends \Magento\Customer\Model\AccountManagement
{
    /**
     * AccountManagement constructor.
     *
     * @param \Magento\Customer\Model\CustomerFactory                      $customerFactory
     * @param \Magento\Framework\Event\ManagerInterface                    $eventManager
     * @param \Magento\Store\Model\StoreManagerInterface                   $storeManager
     * @param \Magento\Framework\Math\Random                               $mathRandom
     * @param \Magento\Customer\Model\Metadata\Validator                   $validator
     * @param \Magento\Customer\Api\Data\ValidationResultsInterfaceFactory $validationResultsDataFactory
     * @param \Magento\Customer\Api\AddressRepositoryInterface             $addressRepository
     * @param \Magento\Customer\Api\CustomerMetadataInterface              $customerMetadataService
     * @param \Magento\Customer\Model\CustomerRegistry                     $customerRegistry
     * @param \Psr\Log\LoggerInterface                                     $logger
     * @param \Magento\Framework\Encryption\EncryptorInterface             $encryptor
     * @param \Magento\Customer\Model\Config\Share                         $configShare
     * @param \Magento\Framework\Stdlib\StringUtils                        $stringHelper
     * @param \Magento\Customer\Api\CustomerRepositoryInterface            $customerRepository
     * @param \Magento\Framework\App\Config\ScopeConfigInterface           $scopeConfig
     * @param \Magento\Framework\Mail\Template\TransportBuilder            $transportBuilder
     * @param \Magento\Framework\Reflection\DataObjectProcessor            $dataProcessor
     * @param \Magento\Framework\Registry                                  $registry
     * @param \Magento\Customer\Helper\View                                $customerViewHelper
     * @param \Magento\Framework\Stdlib\DateTime                           $dateTime
     * @param \Magento\Customer\Model\Customer                             $customerModel
     * @param \Magento\Framework\DataObjectFactory                         $objectFactory
     * @param \Magento\Framework\Api\ExtensibleDataObjectConverter         $extensibleDataObjectConverter
     * @param SearchCriteriaBuilder                                        $searchCriteriaBuilder
     * @param FilterBuilder                                                $filterBuilder
     */
    public function __construct(
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Math\Random $mathRandom,
        \Magento\Customer\Model\Metadata\Validator $validator,
        \Magento\Customer\Api\Data\ValidationResultsInterfaceFactory $validationResultsDataFactory,
        \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
        \Magento\Customer\Api\CustomerMetadataInterface $customerMetadataService,
        \Magento\Customer\Model\CustomerRegistry $customerRegistry,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Encryption\EncryptorInterface $encryptor,
        \Magento\Customer\Model\Config\Share $configShare,
        \Magento\Framework\Stdlib\StringUtils $stringHelper,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Framework\Reflection\DataObjectProcessor $dataProcessor,
        \Magento\Framework\Registry $registry,
        \Magento\Customer\Helper\View $customerViewHelper,
        \Magento\Framework\Stdlib\DateTime $dateTime,
        \Magento\Customer\Model\Customer $customerModel,
        \Magento\Framework\DataObjectFactory $objectFactory,
        \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        FilterBuilder $filterBuilder
    ) {
        parent::__construct(
            $customerFactory,
            $eventManager,
            $storeManager,
            $mathRandom,
            $validator,
            $validationResultsDataFactory,
            $addressRepository,
            $customerMetadataService,
            $customerRegistry,
            $logger,
            $encryptor,
            $configShare,
            $stringHelper,
            $customerRepository,
            $scopeConfig,
            $transportBuilder,
            $dataProcessor,
            $registry,
            $customerViewHelper,
            $dateTime,
            $customerModel,
            $objectFactory,
            $extensibleDataObjectConverter
        );

        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->filterBuilder = $filterBuilder;
    }
}

You can use $this->customerFactory, for customer model.

Delete 'var/generation/*' and flush cache.

5
  • Check the updated question. Its still giving me same error. Jan 20, 2017 at 16:28
  • Delete 'var/generation/*'
    – Sohel Rana
    Jan 20, 2017 at 16:37
  • kudos !! It worked after deleting var/generation. But kindly tell me one thing. How do you generate all the class paths like this \Magento\Customer\Model\CustomerFactory for all those injection. Do you know all the names or you can generate it somehow. However it works and really thanks for that. Now i can check how the plugin can be implemented into it. Thanks a lot. Jan 20, 2017 at 17:09
  • Please read namespace for php, it will help you. It's simple, when you used to it, it will be easy for you too. Don't worry, read more :)
    – Sohel Rana
    Jan 20, 2017 at 17:25
  • Ok. Sure. I will try it. Jan 20, 2017 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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