php - Argument 1 passed to __construct() must be an instance of GuzzleHttpClient

726

I'm trying to "use" a vendor script to connect to feefo api (an online reviews service) but when I try and use the script it gives me this error:

Type error: Argument 1 passed to 
BlueBayTravel\Feefo\Feefo::__construct() must be an instance of 
GuzzleHttp\Client, null given, called in/Users/webuser1/Projects/_websites/domain.co.uk/plugins/gavinfoster/feefo/components/Feedback.php on line 47

Here is the vendor code I'm using:

/*
 * This file is part of Feefo.
 *
 * (c) Blue Bay Travel <[email protected]>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace BlueBayTravel\Feefo;

use ArrayAccess;
use Countable;
use Exception;
use GuzzleHttp\Client;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Support\Arrayable;
use SimpleXMLElement;

/**
 * This is the feefo class.
 *
 * @author James Brooks <[email protected]>
 */
class Feefo implements Arrayable, ArrayAccess, Countable
{
    /**
     * The guzzle client.
     *
     * @var \GuzzleHttp\Client
     */
    protected $client;

    /**
     * The config repository.
     *
     * @var \Illuminate\Contracts\Config\Repository
     */
    protected $config;

    /**
     * The review items.
     *
     * @var array
     */
    protected $data;

    /**
     * Create a new feefo instance.
     *
     * @param \GuzzleHttp\Client                      $client
     * @param \Illuminate\Contracts\Config\Repository $config
     *
     * @return void
     */
    public function __construct(Client $client, Repository $config)
    {
        $this->client = $client;
        $this->config = $config;
    }

    /**
     * Fetch feedback.
     *
     * @param array|null $params
     *
     * @return \BlueBayTravel\Feefo\Feefo
     */
    public function fetch($params = null)
    {
        if ($params === null) {
            $params['json'] = true;
            $params['mode'] = 'both';
        }

        $params['logon'] = $this->config->get('feefo.logon');
        $params['password'] = $this->config->get('feefo.password');

        try {
            $body = $this->client->get($this->getRequestUrl($params));

            return $this->parse((string) $body->getBody());
        } catch (Exception $e) {
            throw $e; // Re-throw the exception
        }
    }

    /**
     * Parses the response.
     *
     * @param string $data
     *
     * @return \Illuminate\Support\Collection
     */
    protected function parse($data)
    {
        $xml = new SimpleXMLElement($data);

        foreach ((array) $xml as $items) {
            if (isset($items->TOTALRESPONSES)) {
                continue;
            }

            foreach ($items as $item) {
                $this->data[] = new FeefoItem((array) $item);
            }
        }

        return $this;
    }

    /**
     * Assigns a value to the specified offset.
     *
     * @param mixed $offset
     * @param mixed $value
     *
     * @return void
     */
    public function offsetSet($offset, $value)
    {
        if (is_null($offset)) {
            $this->data[] = $value;
        } else {
            $this->data[$offset] = $value;
        }
    }

    /**
     * Whether or not an offset exists.
     *
     * @param mixed $offset
     *
     * @return bool
     */
    public function offsetExists($offset)
    {
        return isset($this->data[$offset]);
    }

    /**
     * Unsets an offset.
     *
     * @param mixed $offset
     *
     * @return void
     */
    public function offsetUnset($offset)
    {
        if ($this->offsetExists($offset)) {
            unset($this->data[$offset]);
        }
    }

    /**
     * Returns the value at specified offset.
     *
     * @param mixed $offset
     *
     * @return mixed
     */
    public function offsetGet($offset)
    {
        return $this->offsetExists($offset) ? $this->data[$offset] : null;
    }

    /**
     * Count the number of items in the dataset.
     *
     * @return int
     */
    public function count()
    {
        return count($this->data);
    }

    /**
     * Get the instance as an array.
     *
     * @return array
     */
    public function toArray()
    {
        return $this->data;
    }

    /**
     * Returns the Feefo API endpoint.
     *
     * @param array $params
     *
     * @return string
     */
    protected function getRequestUrl(array $params)
    {
        $query = http_build_query($params);

        return sprintf('%s?%s', $this->config->get('feefo.baseuri'), $query);
    }
}

And here is the code I'm using to try and use the fetch() method from the vendor class:

use Cms\Classes\ComponentBase;
use ArrayAccess;
use Countable;
use Exception;
use GuzzleHttp\Client;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Support\Arrayable;
use SimpleXMLElement;
use BlueBayTravel\Feefo\Feefo;

class Feedback extends ComponentBase
{

    public $client;

    public $config;

    /**
     * Container used for display
     * @var BlueBayTravel\Feefo
     */
    public $feedback;

    public function componentDetails()
    {
        return [
            'name'        => 'Feedback Component',
            'description' => 'Adds Feefo feedback to the website'
        ];
    }

    public function defineProperties()
    {
        return [];
    }

    public function onRun()
    {
        $this->feedback = $this->page['feedback'] = $this->loadFeedback($this->client, $this->config);   
    }

    public function loadFeedback($client, $config)
    {

        $feefo = new Feefo($client, $config);
        $feedback = $feefo->fetch();

        return $feedback;

    }

}

Won't allow me to call the Fetch() method statically so trying to instantiate and then use:

    public function loadFeedback($client, $config)
    {

        $feefo = new Feefo($client, $config);
        $feedback = $feefo->fetch();

        return $feedback;

    }

I've also tried type hinting the args like this:

    public function loadFeedback(Client $client, Repository $config)
    {

        $feefo = new Feefo($client, $config);
        $feedback = $feefo->fetch();

        return $feedback;

    }

But still I get the exception error above. I'm struggling to understand how to get past this. Any help for a newbie much appreciated :)

223

Answer

Solution:

Just type hinting the function won't cast it to that object type. You need to properly pass the Guzzle\Client object to your function call.

// Make sure you 'use' the GuzzleClient on top of the class 
// or use the Fully Qualified Class Name of the Client
$client = new Client(); 

$feedback = new Feedback();

// Now we passed the Client object to the function of the feedback class 
// which will lead to the constructor of the Feefo class which is 
// where your error is coming from.
$loadedFeedback = $feedback->loadFeedback($client);

Don't forget to do the same for theRepository $config from Laravel/Lumen

People are also looking for solutions to the problem: php - Sending a file via a form larger than 34 MB

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.