Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top