Example #1
0
 public function testAddError()
 {
     $Response = new Response();
     foreach ($this->_any_errors as $eachField => $eachErrors) {
         $Response->addError($eachField, $eachErrors);
     }
     $this->assertSame($this->_any_errors, $Response->getErrors());
 }
Example #2
0
 public function getResponse()
 {
     $headers = array('X-Shop: ' . $this->shopId, 'X-Key: ' . $this->apiKey);
     $content = json_encode($this->data);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $this->apiUrl . '/' . $this->resource . $this->getQueryString());
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
     curl_setopt($ch, CURLOPT_HEADER, true);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->type);
     if ($this->type === 'POST') {
         curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
     }
     $response = curl_exec($ch);
     $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
     $header = substr($response, 0, $header_size);
     $jsonBody = substr($response, $header_size);
     $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     $error = curl_error($ch);
     curl_close($ch);
     $response = new Response();
     if ($error) {
         $response->addError(new Error($code, $error));
         return $response;
     }
     if (!preg_match("/2../", $code)) {
         $body = json_decode($jsonBody, true);
         if (isset($body['errors'])) {
             foreach ($body['errors'] as $error) {
                 $response->addError(new Error($error['code'], $error['description']));
             }
             return $response;
         } else {
             $response->addError(new Error($code, 'Unknown error.'));
             return $response;
         }
     }
     $response->setHttpCode($code);
     $response->setHeader($header);
     $response->setJsonBody($jsonBody);
     return $response;
 }
Example #3
0
 private function secure()
 {
     $data = $this->request->getBasicAuth();
     if (!$data || !(isset($data['username']) && $data['username'] == $this->config->auth->username && (isset($data['password']) && $data['password'] == $this->config->auth->password))) {
         $response = new Response();
         $response->status = $response::STATUS_FORBIDDEN;
         $response->code = $response::CODE_ERROR;
         $response->addError('not right username and password');
         return $response->toArray();
     }
 }
 public static function findByName($name)
 {
     $response = new Response();
     if (!$name) {
         $response->status = $response::STATUS_BAD_REQUEST;
         $response->addError('параметр "name" не может быть пустым');
         return $response->toArray();
     }
     try {
         $response->data = Robots::find(['conditions' => 'name LIKE :name:', 'bind' => ['name' => '%' . $name . '%']])->toArray();
     } catch (Exception $e) {
         $response->setException($e);
     } finally {
         return $response->toArray();
     }
 }
Example #5
0
 public function attachErrorToResponse(Response $response)
 {
     foreach ($this->errors as $error) {
         $response->addError($error);
     }
 }
Example #6
0
 /**
  * Parse an XML response from the validator
  *
  * This function parses a SOAP 1.2 response xml string from the validator.
  *
  * @param string $xml The raw soap12 XML response from the validator.
  *
  * @return Response object Response if parsing soap12 response successfully,
  *
  * @throws Exception
  */
 protected function parseSOAP12Response($xml)
 {
     $doc = new \DOMDocument('1.0', 'UTF-8');
     if ($doc->loadXML($xml) === false) {
         throw new Exception('Failed load xml');
     }
     $response = new Response();
     // Get the standard CDATA elements
     foreach (array('uri', 'checkedby', 'csslevel', 'date') as $var) {
         $element = $doc->getElementsByTagName($var);
         if ($element->length) {
             $response->{'set' . ucfirst($var)}($element->item(0)->nodeValue);
         }
     }
     // Handle the bool element validity
     $element = $doc->getElementsByTagName('validity');
     if ($element->length && $element->item(0)->nodeValue === 'true') {
         $response->setValidity(true);
     } else {
         $response->setValidity(false);
     }
     if (!$response->isValidity()) {
         $errors = $doc->getElementsByTagName('error');
         foreach ($errors as $error) {
             $response->addError(new Error($error));
         }
     }
     $warnings = $doc->getElementsByTagName('warning');
     foreach ($warnings as $warning) {
         $response->addWarning(new Warning($warning));
     }
     return $response;
 }
Example #7
0
 public static function createResponseByOtherModelData($model, $external_id)
 {
     $ret = null;
     if ($model && $external_id) {
         $myCompanyId = Yii::app()->user->getProfile()->company_id;
         $Response = new Response();
         $Response->model = $model;
         $Response->external_id = $external_id;
         $Response->from_company_id = $myCompanyId;
         //$model = new $model;
         $modelObj = $model::model()->findByPk($external_id);
         if (is_null($modelObj)) {
             $Response->addError('external_id', "external_id=[{$external_id}] not found in [{$model}]");
         } else {
             // проверка, что предлождение не наше
             $modelCompanyId = $modelObj->user->company_id;
             if ($modelCompanyId != $myCompanyId) {
                 // что мы на него уже заказ не делали
                 //                    $criteria = new CDbCriteria;
                 //                    $criteria->addColumnCondition([
                 //                        'model' => $model,
                 //                        'external_id' => $external_id,
                 //                        'from_company_id' => $myCompanyId ,
                 //                        'to_company_id' => $modelCompanyId ,
                 //                    ]);
                 //                    $countAlready = Response::model()->count($criteria);
                 //                    if ( 0 == $countAlready ) {
                 if (!static::isResponseAlreadyExists($model, $external_id, $myCompanyId, $modelCompanyId)) {
                     $Response->to_company_id = $modelObj->user->company_id;
                     $text = trim(Yii::app()->request->getParam('text', ''));
                     if (0 == mb_strlen($text)) {
                         $text = Yii::t('default', 'first response message');
                     }
                     $Response->text = $text;
                     $res = $modelObj->user->sendCRMVirtual($model);
                 } else {
                     $Response->addError('external_id', "Already have response");
                 }
             } else {
                 $Response->addError('external_id', "Element belongs to us");
             }
         }
         $ret = $Response;
     }
     return $ret;
 }
Example #8
0
include '../vendor/autoload.php';
// set true for testing purposes
$linear = false;
$response = new Response();
try {
    if (!array_key_exists('start', $_GET)) {
        throw new \RuntimeException('No start time specified.');
    }
    if (!array_key_exists('end', $_GET)) {
        throw new \RuntimeException('No end time specified.');
    }
    if (!array_key_exists('step', $_GET)) {
        throw new \RuntimeException('No step specified.');
    }
    $start = (int) $_GET['start'];
    $end = (int) $_GET['end'];
    $step = (int) $_GET['step'];
    // 30m
    if ($linear) {
        $bandwidth = new Linear($start, $end, $step);
    } elseif ($_GET['weak']) {
        $bandwidth = new Bandwidth(0, 2400, 2000, 386, 0.05);
    } else {
        $bandwidth = new Bandwidth(0, 12000, 8000, 1024, 0.05);
    }
    $response->data('datapoints', $bandwidth->get($start, $end, $step));
} catch (\Exception $e) {
    $response->addError($e->getMessage());
}
echo $response->toJson();
Example #9
0
 public static function create($data)
 {
     $response = new Response();
     try {
         $data = json_decode($data, true);
         $category = Categories::findFirst($data->categoryId);
         if ($category !== FALSE) {
             /** @var Products $products */
             $product = new Products();
             $product->setAttributes($data);
             $response->data = $product->create();
             $response->status = $response::STATUS_CREATED;
         } else {
             $response->status = $response::STATUS_BAD_REQUEST;
             $response->addError('CATEGORIES_NOT_FOUND');
         }
     } catch (Exception $e) {
         $response->setException($e);
     } finally {
         return $response->toArray();
     }
 }