0

Namespace/Modulename/Model/WebService/ClientFactory.php

<?php

namespace Namespace\Modulename\Model\WebService;

use Magento\Framework\ObjectManagerInterface;
use Namespace\Modulename\Model\WebService\Config;


class ClientFactory
{

    protected $config;


    public function __construct(Config $config)
    {
        $this->config = $config;
    }

    /**
     * @param array $data
     * @return \Modulename\WebService\Client
     */
    public function create(array $data = [])
    {
        if (isset($data['options']) && is_string($data['options'])) {
            $data['options'] = ['host' => $data['options']];
        } elseif (empty($data['options'])) {
            $data['options'] = [];
        }

        $userId = !empty($data['userId']) ? $data['userId'] : $this->config->getUserId();
        $licenseKey = !empty($data['licenseKey']) ? $data['licenseKey'] : $this->config->getLicenseKey();
        $locales = !empty($data['locales']) ? $data['locales'] : ['en'];
        $options = $data['options'] + ['host' => $this->config->getHost()];

        $client = new \Modulename\WebService\Client($userId, $licenseKey, $locales, $options);
        return $client;
    }
}

Namespace/Modulename/Observer/Redirect.php

<?php

namespace Namespace\Modulename\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Namespace\Modulename\Model\WebService\Config;
use Namespace\Modulename\Model\WebService\ClientFactory;

class Redirect implements ObserverInterface {


    protected $clientFactory;


    public function __construct(Config $config, ClientFactory $clientFactory)
    {
        $this->config = $config;
        $this->clientFactory = $clientFactory;
    }

    public function execute(Observer $observer) 
    {

        $client = $this->clientFactory->create();
        $record = $client->city('128.101.101.101');
        print($record->country->isoCode . "\n");        
        return $this;
    }
}

Fatal error: Uncaught Error: Class 'Modulename\WebService\Client' not found in Namespace/Modulename/Model/WebService/ClientFactory.php

  • what is this line means $client = new \Modulename\WebService\Client($userId, $licenseKey, $locales, $options); – Learing_Coder Aug 8 '18 at 19:48
  • This will instantiate the object. For example ` $client = new Client(42, 'abcdef123456');`. This creates a Client object that can be reused across requests. – Slimshadddyyy Aug 8 '18 at 19:50
1

The factory classes are generated by magento itself, you don't need to create it manually.

Just rename your class to Client and magento would fetch ClientFactory by itself.

  • Does that mean if I change new \Modulename\WebService\Client to new \Modulename\WebService\ClientFactory, should it work? – Slimshadddyyy Aug 8 '18 at 20:08
  • yes, it should work. – Vivek Kumar Aug 8 '18 at 20:11
  • If I rename file back to ClientFactory.php and change code to Class ClientFactory and new \Modulename\WebService\ClientFactory it thorws exception Source class "\Modulename\WebService\Client" for "Modulename\WebService\ClientFactory" generation does not exist. – Slimshadddyyy Aug 8 '18 at 20:25
  • You should just keep a single class file that is client , ClientFactory is generated by magento.You should not worry about it. – Vivek Kumar Aug 8 '18 at 20:27
  • Okay but now it says Class Modulename\WebService\Client not found in Namespace/Modulename/Model/WebService/Client. Should there be change in Observer/Redirect.php as well? – Slimshadddyyy Aug 8 '18 at 22:43

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.