What is the best way to have one instance of some object in class? I'm using yii2-httpclient and use Client class. I have different methods thank make requests for differents web API but I think its bad idea to creating new object when calling method. I'm trying make instance in constructor but have error "Call to a member function createRequest() on a non-object". How can I do this another way?
My Controller:
<?php
namespace app\models;
use yii;
use yii\base\Model;
use yii\httpclient\Client;
class ExchangeRates extends Model
{
function __construct()
{
$client = new Client();
}
public function getPrivatRate()
{
// $client = new Client();
$privatResponse = $client->createRequest()
->setMethod('get')
->setUrl('https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5')
->send();
return json_decode($privatResponse->content, true);
}
public function getNationalRate()
{
// $client = new Client();
$nbuResponse = $client->createRequest()
->setMethod('get')
->setUrl('https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?valcode=USD&date=20161101&json')
->send();
return json_decode($nbuResponse->content, true);
}
public function getMejBankRate()
{
// $client = new Client();
$mejResponse = $client->createRequest()
->setMethod('get')
->setUrl('http://api.cashex.com.ua/api/v1/exchange/mejbank')
->send();
return json_decode($mejResponse->content, true);
}
public function getBlackMarketRate()
{
// $client = new Client();
$blackMarketResponse = $client->createRequest()
->setMethod('get')
->setUrl('http://api.cashex.com.ua/api/v1/exchange/black-market')
->send();
return json_decode($blackMarketResponse->content, true);
}
}
You can something like this:
class ExchangeRates extends Model
{
public $client;
public function init()
{
parent::init();
$this->client = new Client();
}
Now you can use it like this:
$privatResponse = $this->client->createRequest()->...
Related
I'm using Laravel v5.8 and guzzlehttp of v7.4 and tried to write this Controller for getting some information:
public function __construct()
{
$client = new Client(['base_uri' => 'https://jsonplaceholder.typicode.com/']);
}
public function getInfo(Request $request)
{
try {
$response = $this->client->request('GET', 'posts');
dd($response->getContents());
} catch (ClientException $e) {
dd($e);
}
}
But now when I call the method getInfo, I get this error message:
Undefined property: App\Http\Controllers\Tavanmand\AppResultController::$client
However the docs says calling the uri's like this.
So what's going wrong here? How can I solve this issue?
The scope of your $client variable is limited to inside your constructor. You want to assign it to some sort of class property if you want to access it elsewhere;
private $client;
public function __construct()
{
$this->client = new Client(['base_uri' => 'https://jsonplaceholder.typicode.com/']);
}
public function getInfo(Request $request)
{
try {
$response = $this->client->request('GET', 'posts');
//...
}
Make $client as global variable of this class.
then set value at construction like:
public $client
public function __construct()
{
$this->client = new Client(['base_uri' => 'https://jsonplaceholder.typicode.com/']);
}
Happy coding...
I am using symfony 3 and trying to get access to the class I declared in
src/AppBundle/Service/ApiEngine.php
namespace AppBundle\Service;
use DateTime;
class ApiEngine {
private $api_handler;
public function __construct($username, bool $repos) {
$client = new \GuzzleHttp\Client();
$request = 'https://api.github.com/users/' . $username;
$request .= ($repos) ? '/repos' : "";
$res = $client->request('GET', $request);
$this->api_handler = json_decode($res->getBody()->getContents(), true);
}
public function getProfileData() {
return [ /*some data*/ ];
}
}
I declared this file in
config/service.yml
service:
*
*
*
api:
class: AppBundle\Service\ApiEngine
arguments: ["#username", "#repos"]
In controller I am trying to use some of the ApiEngine methods like this:
src/AppBundle/Controller/GitApiController.php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class GitApiController extends Controller {
/**
* #Route("/{username}", name ="gitapi", defaults={"username": "symfony"})
*/
public function gitApiAction($username) {
$api = $this->get('api')->__construct($username, false)->getProfileData();
return $this->render('gitapi/index.html.twig', $api);
} }
But it gives me this error:
(1/1) ServiceNotFoundException The service "api" has a dependency on a
non-existent service "username".
I advise you to change your class into this for example:
private function __construct($username, bool $repos) {
$client = new \GuzzleHttp\Client();
$request = 'https://api.github.com/users/' . $username;
$request .= ($repos) ? '/repos' : "";
$res = $client->request('GET', $request);
$this->api_handler = json_decode($res->getBody()->getContents(), true);
}
public static function createApiEngine($username, bool $repos)
{
return new self($username, $bool);
}
After inside your controller you can do this:
$api = ApiEngine::createApiEngine($username, false);
$api->getProfileData();
Into your controller you need to insert a use for ApiEngine, in this use case you don't need dependency injection so inside your services.yml remove arguments please
I'm using Guzzle's asynchronous request and have them implemented in a service that I would like to test now.
My method looks like this (pseudo, so if it's not 100% valid, please excuse)
public function getPlayer(string $uiid, array &$player = [])
{
$options['query'] = ['id' => $uiid];
$promise = $this->requestAsync('GET', $this->endpoint, $options);
$promise->then(function (ResponseInterface $response) use (&$player) {
$player = $response->getBody()->getContents();
});
return $players;
}
Now I want to test it, but I don't really know how to mock the callable, because I'm always getting the error
1) tzfrs\PlayerBundle\Tests\Api\Player\PlayerServiceTest::testGetPlayer
Prophecy\Exception\InvalidArgumentException: Expected callable or instance of PromiseInterface, but got object.
This is how I have it implemented currently
/** #var ObjectProphecy|PromiseInterface $response */
$promise = $this->prophesize(PromiseInterface::class);
$promise->then()->will($this->returnCallback(function (ResponseInterface $response) use (&$player){}));
Didn't work. And this
$this->returnCallback(function (ResponseInterface $response) use (&$player){})
didn't work either. Same error. And when simply trying a dummy callback
$promise->then(function(){});
I get the error Error: Call to a member function then() on string, even after ->reveal()ing the promise first. Any ideas?
I had another idea.
Make a dependency that will make what you make now in requestAsync();
And then create it's mock that will return another mock of promise.
class PromiseMock
{
private $response;
public function __construct(ResponseInterface $response)
{
$this->response = $response;
}
public function then($callable)
{
$callable($this->response);
}
}
test looks like
public function testGetPlayer()
{
$response = new Response(200, [], "Your test response");
$promiseMock = new PromiseMock($response);
$mockDependency = $this->getMockBuilder('YourDependencyClass')
->getMock()
->expects("requestAsync")->willReturn($promiseMock);
$service = new YouServiceClass($mockDependency);
$service->getPlayer("76245914-d56d-4bac-8419-9e409f43e777");
}
And in your class changes only
$promise = $this->someNameService->requestAsync('GET', $this->endpoint, $options);
I would inject a processor to your class and call it's callable. Check it out, the rest is quite obvious:
public function __construct(Processor $processor) {
$this->processor = $processor;
}
public function getPlayer(string $uiid, array &$player = [])
{
$options['query'] = ['id' => $uiid];
$promise = $this->requestAsync('GET', $this->endpoint, $options);
$promise->then([$this->processor, "processResponse"]);
$player = $this->processor->getPlayer();
return $players;
}
And processor:
class Processor {
private $player;
public function processResponse (ResponseInterface $response) {
$this->player = $response->getBody()->getContents();
}
public function getPlayer() { return $this->player;}
}
How to get access to $app inside a controller as the Slim 3.3 injects only the ContainerInterface?
Code to illustrate the question:
$app = new \Slim\App;
$app->get('/home', 'HomeController:get');
$app->run();
class HomeController {
private $ci;
public function _construct($ci) {
$this->ci = $ci;
}
public function get($request, $response) {
$this->ci->get(...);
// How to access $app and dependencies like $app->jwt?
}
}
This was a tough one.
Slim 3 heavily uses dependency injection, so you might want to use it too.
First inside your dependencies.php you need to grab the $app and throw it in a container to inject it to the Controller later.
$container['slim'] = function ($c) {
global $app;
return $app;
};
Then you got to inject it:
// Generic Controller
$container['App\Controllers\_Controller'] = function ($c) {
return new _Controller($c->get('slim'));
};
Now on your controller.php:
private $slim;
/**
* #param \Psr\Log\LoggerInterface $logger
* #param \App\DataAccess $dataaccess
* #param \App\$app $slim
*/
public function __construct(LoggerInterface $logger, _DataAccess $dataaccess, $slim)
{
$this->logger = $logger;
$this->dataaccess = $dataaccess;
$this->slim = $slim;
}
Now you just got call it like this:
$this->slim->doSomething();
You can make your own 'singleton' to mimic Slim::getInstance(); ;)
class Anorexic extends \Slim\App {
private static $_instance;
public static function getInstance(){
if(empty(self::$_instance){
self::$_instance = new self();
}
return self::$_instance;
}
}
Then change your initialization like this:
// $app = new \Slim\App;
$app = Anorexic::getInstance();
Now you can get your \Slim\App instance anywhere in your code by calling Anorexic::getInstance(); Ofcourse you should never try this at home :P
in zf1, we can get controller and action name using
$controller = $this->getRequest()->getControllerName();
$action = $this->getRequest()->getActionName();
How we can achieve this in zf2?
UPDATE:
I tried to get them using
echo $this->getEvent()->getRouteMatch()->getParam('action', 'NA');
echo $this->getEvent()->getRouteMatch()->getParam('controller', 'NA');
But I am getting error
Fatal error: Call to a member function getParam() on a non-object
I like to get them in __construct() method;
Ideally I would like to check if there is no Action is defined it will execute noaction() method. I would check using php method method_exists.
Even simpler:
$controllerName =$this->params('controller');
$actionName = $this->params('action');
you can't access these variables in controller __construct() method, but you can access them in dispatch method and onDispatch method.
but if you would like to check whether action exist or not, in zf2 there is already a built in function for that notFoundAction as below
public function notFoundAction()
{
parent::notFoundAction();
$response = $this->getResponse();
$response->setStatusCode(200);
$response->setContent("Action not found");
return $response;
}
but if you still like to do it manually you can do this using dispatch methods as follow
namespace Mynamespace\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\Mvc\MvcEvent;
class IndexController extends AbstractActionController
{
public function __construct()
{
}
public function notFoundAction()
{
parent::notFoundAction();
$response = $this->getResponse();
$response->setStatusCode(200);
$response->setContent("Action not found");
return $response;
}
public function dispatch(Request $request, Response $response = null)
{
/*
* any customize code here
*/
return parent::dispatch($request, $response);
}
public function onDispatch(MvcEvent $e)
{
$action = $this->params('action');
//alertnatively
//$routeMatch = $e->getRouteMatch();
//$action = $routeMatch->getParam('action', 'not-found');
if(!method_exists(__Class__, $action."Action")){
$this->noaction();
}
return parent::onDispatch($e);
}
public function noaction()
{
echo 'action does not exits';
}
}
You will get module , controller and action name like this in Zf2 inside your controller...
$controllerClass = get_class($this);
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
$tmp = substr($controllerClass, strrpos($controllerClass, '\\')+1 );
$controllerName = str_replace('Controller', "", $tmp);
//set 'variable' into layout...
$this->layout()->currentModuleName = strtolower($moduleNamespace);
$this->layout()->currentControllerName = strtolower($controllerName);
$this->layout()->currentActionName = $this->params('action');
$controllerName = strtolower(Zend_Controller_Front::getInstance()->getRequest()->getControllerName());