php - Can I call a service inside a service in zf3? -
i new frameworks , zf3, need make api, following logic. receive parameters, store them in bd , if call action made @ point(call manager), send (provider caller). able first part, , second, now, don't know how last one... have following code:
indexcontroller.php
use application\service\callmanager; use application\service\tropocaller; use application\service\nexmocaller; class indexcontroller extends abstractactioncontroller { /** * entity manager. * @var doctrine\orm\entitymanager */ private $entitymanager; /** * call manager. * @var application\service\callmanager */ private $callmanager; /** * call manager. * @var application\service\nexmocaller */ private $nexmocaller; /** * call manager. * @var application\service\tropocaller */ private $tropocaller; /** * constructor used injecting dependencies controller. */ public function __construct($entitymanager, $callmanager, $nexmocaller, $tropocaller) { $this->entitymanager = $entitymanager; $this->callmanager = $callmanager; $this->nexmocaller = $nexmocaller; $this->tropocaller = $tropocaller; } public function contactusaction() { $form= new buttoncallform(); // check whether post post request. if ($this->getrequest()->ispost()) { // post data. $data = $this->params()->frompost(); // fill form data. $form->setdata($data); if ($form->isvalid()) { // validated form data. $data = $form->getdata(); // use post manager service add new call database $this->callmanager->callmanager($data); return $this->redirect()->toroute('application', ['action'=>'thankyou']); } } return new viewmodel([ 'form' => $form, ]); } public function thankyouaction() { return new viewmodel(); }
}
indexcontrollerfactory.php
<?php namespace application\controller\factory; use interop\container\containerinterface; use zend\servicemanager\factory\factoryinterface; use application\service\callmanager; use application\service\tropocaller; use application\service\nexmocaller; use application\controller\indexcontroller; /** * factory indexcontroller. purpose instantiate * controller. */ class indexcontrollerfactory implements factoryinterface { public function __invoke(containerinterface $container, $requestedname, array $options = null) { $entitymanager = $container->get('doctrine.entitymanager.orm_default'); $callmanager = $container->get(callmanager::class); $nexmocaller = $container->get(nexmocaller::class); $tropocaller = $container->get(tropocaller::class); // instantiate controller , inject dependencies return new indexcontroller($entitymanager, $callmanager, $nexmocaller, $tropocaller); } }
my callmanager.php
<?php namespace application\service; use zend\servicemanager\servicemanager; use zend\servicemanager\servicemanagerawareinterface; use zend\mvc\controller\abstractactioncontroller; use zend\view\model\viewmodel; use application\entity\callrequested; use doctrine\orm\entitymanager; use stomp\client; use stomp\statefulstomp; use stomp\network\connection; use stomp\transport\message; use application\service\nexmocaller; use application\service\tropocaller; class callmanager { /** * doctrine entity manager. * @var doctrine\orm\entitymanager */ private $entitymanager; /** * @var application\service\nexmocaller */ private $nexmocaller; /** * @var application\service\tropocaller */ private $tropocaller; // constructor method used inject dependencies controller. public function __construct($entitymanager) { $this->entitymanager = $entitymanager; } public function callmanager($data) { $callrequested= new callrequested; $callrequested-> setclientcontact($data['clientcontact']); $callrequested-> setprovider($data['provider']); $callrequested-> setcallcenter($data['callcentercontact']); $whencall= $data['schedule']; $language= $data['language']; $date = new \datetime(); $callrequested-> setrequesttime($date); $provider=$data['provider']; $tropo='tropo'; //var_dump($data); $this->entitymanager->persist($callrequested); $this->entitymanager->flush(); $id=$callrequested->getid(); //var_dump($callrequested->getid()); $data=array($id,$data); $data=json_encode($data, true); //confirmar onde usar esta lógica if($whencall==='1') { echo "vamos establecer ligaçao \n"; if (stripos($provider, $tropo) !== false) { $destination = '/queue/tropozend'; $messages = 1; $size = 256; $data = "calls"; $body = $data; try { $connection = new connection('tcp://192.168.64.3:61613'); $con1 = new statefulstomp(new client($connection)); $con1->send($destination, new message($body)); //echo "message sent $body \n" ; } catch(stompexception $e) { echo $e->getmessage(); // tropocall(); } } else{ $destination = '/queue/nexmozend'; $messages = 1; $size = 256; $data = "calls"; $body = $data; try { $connection = new connection('tcp://192.168.64.3:61613'); $con1 = new statefulstomp(new client($connection)); $con1->send($destination, new message($body)); //echo "message sent $body \n" ; } catch(stompexception $e) { echo $e->getmessage(); } } // nexmocall(); } else {echo "vamos agendar sua chamada \n"; } } }
i created nexmocaller , tropocaller, don't know how called them. here 1 example of logic: class nexmocaller
{ // public function __construct($nexmocaller) // { // $this->nexmocaller = $nexmocaller; // } public function nexmocall() { $service= new nexmocaller(); $servicemanager->set(nexmocaller::class, $service); $key = file_get_contents('scripts/application.key'); $basic = new \nexmo\client\credentials\basic($keynexmo, $secret); $keypair = new \nexmo\client\credentials\keypair($key, $application_id); $client = new \nexmo\client(new \nexmo\client\credentials\container($basic, $keypair)); $jwt = generate_jwt($application_id, $key); header('content-type: application/json', 'authorization: bearer'.$jwt); $destination = '/queue/nexmozend'; $connection = new connection('tcp://192.168.64.3:61613'); $stomp = new statefulstomp(new client($connection)); $stomp->subscribe($destination); echo "waiting messages...\n"; while(true) { $frame = $stomp->read(); $body = $frame->getbody(); //echo($frame); echo "message received $body \n"; //echo $stomp->read()->body, php_eol; //print_r($frame = $stomp->read()); //print_r($stomp->read()->body); break; } //codificar o json como é necessário $json = json_decode($body, true); var_dump($json); } }
my config.module.php
'controllers' => [ 'factories' => [ controller\indexcontroller::class => controller\factory\indexcontrollerfactory::class, ], ], 'service_manager' => [ 'factories' => [ service\callmanager::class => service\factory\callmanagerfactory::class, service\nexmocaller::class => invokablefactory::class, service\tropocaller::class => invokablefactory::class, ], ],
i have tried several aproaches none of them creates manager or calls it... plus, haven't found case of using service inside service, not sure possible... 1 of solutions i've tried was: -constructing in call manager:
/** * doctrine entity manager. * @var doctrine\orm\entitymanager */ private $entitymanager; /** * @var application\service\nexmocaller */ private $nexmocaller; /** * @var application\service\tropocaller */ private $tropocaller; public function __construct($entitymanager, $nexmocaller, $tropocaller) { $this->entitymanager = $entitymanager; $this->nexmocaller = $nexmocaller; $this->tropocaller = $tropocaller; }
and error:
too few arguments function application\service\callmanager::__construct(), 1 passed in /opt/lampp/htdocs/buttoncall/skeleton-application/module/application/src/service/factory/callmanagerfactory.php on line 15 , 3 expected
or calling:
$this->nexmocaller->nexmocaller();
and this: call member function nexmocaller() on null
i used answer gaven, in factory:
<?php namespace application\service\factory; use interop\container\containerinterface; use zend\servicemanager\factory\factoryinterface; use application\service\callmanager; use application\service\nexmocaller; use application\service\tropocaller; class callmanagerfactory implements factoryinterface { public function __invoke(containerinterface $container, $requestedname, array $options = null) { $entitymanager = $container->get('doctrine.entitymanager.orm_default'); $nexmocaller = $container->get(nexmocaller::class); $tropocaller = $container->get(tropocaller::class); return new $requestedname( $entitymanager, $nexmocaller, $tropocaller ); } }
and this: call member function nexmocaller() on null
what should do?
from understand trying use nexmocaller , tropocaller inside of callmanager haven't injected them.
you on right path creating constructor:
public function __construct($entitymanager, $nexmocaller, $tropocaller) { $this->entitymanager = $entitymanager; $this->nexmocaller = $nexmocaller; $this->tropocaller = $tropocaller; }
but while expecting 3 parameter error tells callmanagerfactory sending 1.
to solve modify callmanagerfactory looks this:
public function __invoke(containerinterface $container, $requestedname, array $options = null) { $entitymanager = $container->get('doctrine.entitymanager.orm_default'); $nexmocaller = $container->get(nexmocaller::class); $tropocaller = $container->get(tropocaller::class); return new $requestedname( $entitymanager, $nexmocaller, $tropocaller ); }
Comments
Post a Comment