可重用代码 soap 客户端 laravel

Reusable code soap client laravel

我在 laravel 中有使用相同 soap 客户端的不同功能: 这些函数中 soapclient 的重复代码似乎是错误的。 我尝试使用特征文件,但我似乎无法以这种方式工作。

 public function getStaff()
    {
        // INITIALISEER DE SOAP CLIENT
        $webservicesPwd = "apipwd";      
        $soap = new SoapClient('https://apiserver.com');

        // HAAL STUDENTEN OP VIA API CALL
        $result = $soap->apiFunction($webservicesPwd,2,1);

        // INDIEN RESULTAAT NIET CORRECT
        if(is_int($result)) {

           throw new \Exception($errorMessage);
        }

public function getStudents()
    {
          // INITIALISEER DE SOAP CLIENT
        $webservicesPwd = "apipwd";      
        $soap = new SoapClient('https://apiserver.com');

        // HAAL STUDENTEN OP VIA API CALL     
        $result = $this->soap-someOtherApiFunction($this->webservicesPwd,3,1);

        // INDIEN RESULTAAT NIET CORRECT
      if(is_int($result)) {

           throw new \Exception($errorMessage);
        }


        dd($result);
    }

新建 class:

class ApiNameClient {
    protected $soapClient;

    protected $webservicesPwd;

    public function __construct() {
    // initialize $soapClient and $webservicesPwd here
    }

    public function getStudents() {
        $result = $this->soapClient->myFirstAction($this->webservicesPwd,3,1);

        return $this->defendInteger($result);
    }

    protected function defendInteger($value) {
        if(is_int($value)) {
            throw new \Exception($errorMessage);
        }
        return $value;
    }
}