Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.php_cs.cache
/vendor
composer.lock
.DS_Store
.idea
23 changes: 23 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude([
'runtime',
'vendor',
])
->in(__DIR__ . '/src');

$rules = [
'@Symfony' => true,
'phpdoc_inline_tag' => false,
'single_import_per_statement' => false,
'concat_space' => ['spacing' => 'one'],
'array_syntax' => ['syntax' => 'short'],
'phpdoc_no_package' => false,
];

if (class_exists('\\PhpCsFixer\\Fixer\\ControlStructure\\YodaStyleFixer')) {
$rules['yoda_style'] = false;
}

return PhpCsFixer\Config::create()->setRules($rules)->setFinder($finder);
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.2"
"php": ">=5.4"
},
"autoload": {
"psr-4": {
Expand All @@ -28,5 +28,8 @@
"type": "git",
"url": "https://github.com/dumkaaa/ExtService"
}
]
}
],
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16@dev"
}
}
42 changes: 27 additions & 15 deletions src/BaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
use ExtService\Traits\BaseGetter;

/**
* Объект с запросом к сервису
* Объект с запросом к сервису.
*/
class BaseRequest extends Web\HttpClient implements IRequest
{
use BaseGetter;

/**
* @var array
*/
protected $_params = [];


/**
* Конструктор
*
* @param array $params
*/
public function __construct(array $params = null)
Expand All @@ -30,36 +31,43 @@ public function __construct(array $params = null)
$this->responseCookies = new Web\HttpCookies();
$this->setParams($params);
$this->setCompress(true);
$this->setVersion("1.1");
$this->setVersion('1.1');
}

/**
* Задает параметры запроса из массива
* Задает параметры запроса из массива.
*
* @param array $params
*
* @return BaseRequest
*/
public function setParams(array $params = null, $boolClean = true)
{
$this->requestHeaders = new Web\HTTPHeaders;
$this->requestHeaders = new Web\HTTPHeaders();

if ($boolClean)
if ($boolClean) {
$this->cleanParams();
}

if (isset($params['cookies']))
if (isset($params['cookies'])) {
$this->setCookies($params['cookies']);
}

if (isset($params['headers']))
if (isset($params['headers'])) {
$this->setHeaders($params['headers']);
}

foreach ($params as $name => $value) {
$this->setParam($name, $value);
}

return $this;
}

public function cleanParams()
{
$this->_params = null;

return $this;
}

Expand All @@ -69,28 +77,34 @@ public function setCookies(array $cookies)
}

/**
* Задает заголовки запроса из массива
* Задает заголовки запроса из массива.
*
* @param array $headers
*
* @return $this
*/
public function setHeaders(array $headers)
{
foreach ($headers as $name => $value) {
$this->setHeader($name, $value, true);
}

return $this;
}

/**
* override
* Задает параметр запроса
* Задает параметр запроса.
*
* @param string $name
* @param mixed $value
* @param mixed $value
*
* @return BaseRequest
*/
public function setParam($name, $value)
{
$this->_params[trim($name)] = $value;

return $this;
}

Expand All @@ -111,7 +125,7 @@ public function getBody()

public function setAuthorization($user, $pass)
{
$this->setHeader("Authorization", "Basic " . base64_encode($user . ":" . $pass));
$this->setHeader('Authorization', 'Basic ' . base64_encode($user . ':' . $pass));
}

public function getHeaders()
Expand All @@ -123,6 +137,4 @@ public function getCookies()
{
return $this->responseCookies;
}


}
}
36 changes: 26 additions & 10 deletions src/BaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use ExtService\Interfaces\Response as IResponse;

/**
* Объект с ответом от сервиса
* Объект с ответом от сервиса.
*/
class BaseResponse implements IResponse
{
Expand All @@ -29,7 +29,8 @@ class BaseResponse implements IResponse
protected $error = false;

/**
* Возвращает данные ответа
* Возвращает данные ответа.
*
* @return mixed
*/
public function getData()
Expand All @@ -38,29 +39,36 @@ public function getData()
}

/**
* Задает данные ответа
* Задает данные ответа.
*
* @param mixed $data
*
* @return Response
*/
public function setData($data)
{
$this->data = $data;

return $this;
}

/**
* Задает куки ответа
* Задает куки ответа.
*
* @param mixed $data
*
* @return Response
*/
public function setCookies($cookie)
{
$this->cookie = $cookie;

return $this;
}

/**
* Возвращает куки ответа
* Возвращает куки ответа.
*
* @return \Bitrix\Main\Web\HttpCookies
*/
public function getCookies()
Expand All @@ -69,18 +77,22 @@ public function getCookies()
}

/**
* Задает код ошибки
* Задает код ошибки.
*
* @param string $code
*
* @return Response
*/
public function setStatus($code)
{
$this->error_code = trim($code);

return $this;
}

/**
* Возвращает код ошибки
* Возвращает код ошибки.
*
* @return string
*/
public function getStatus()
Expand All @@ -89,7 +101,8 @@ public function getStatus()
}

/**
* Возвращает сообщение об ошибке
* Возвращает сообщение об ошибке.
*
* @return string
*/
public function getError()
Expand All @@ -98,13 +111,16 @@ public function getError()
}

/**
* Задает сообщение об ошибке
* Задает сообщение об ошибке.
*
* @param string $message
*
* @return Response
*/
public function setError($message)
{
$this->error = $message;

return $this;
}
}
}
37 changes: 24 additions & 13 deletions src/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
use ExtService\Interfaces\Service as IService;

/**
* Базовый класс для сервисовю Реализует обращение к методам и к стороннему апи
* Базовый класс для сервисовю Реализует обращение к методам и к стороннему апи.
*/
class BaseService implements IService
{
use Traits\BaseSetter, Traits\BaseGetter;
use Traits\BaseSetter;
use Traits\BaseGetter;

/** @var array */
protected $params = [];

/**
* Осуществляет HTTP запрос с сервису
* @param IRequest $request Объект запроса
* Осуществляет HTTP запрос с сервису.
*
* @param IRequest $request Объект запроса
* @param IResponse $response Объект ответа
*
* @return IResponse
*/
public function query(IRequest $request, IResponse $response)
Expand All @@ -39,9 +42,11 @@ public function query(IRequest $request, IResponse $response)
}

/**
* Вызывает методы получения данных по имени
* @param string $name Имя методы, например для getCatalog это будет Catalog
* Вызывает методы получения данных по имени.
*
* @param string $name Имя методы, например для getCatalog это будет Catalog
* @param IRequest $request
*
* @return IResponse | false
*/
public function get($name, IRequest $request = null, $cacheTime = 43200)
Expand All @@ -53,27 +58,31 @@ public function get($name, IRequest $request = null, $cacheTime = 43200)
if ($cache->initCache($cacheTime, get_class($this) . $method)) {
$return = $cache->getVars();
} elseif ($cache->startDataCache()) {
if (!is_callable(array($this, $method))) {
if (!is_callable([$this, $method])) {
$cache->abortDataCache();

return $return;
} else {
$return = $this->$method();

if(is_null($return)) {
if (is_null($return)) {
$cache->abortDataCache();
} elseif (is_object($return) && $return->getError()) {
$cache->abortDataCache();
}
}
$cache->endDataCache($return);
}

return $return;
}

/**
* Вызывает методы обработки данных по имени
* @param string $name Имя методы, например для actionSave это будет Save
* Вызывает методы обработки данных по имени.
*
* @param string $name Имя методы, например для actionSave это будет Save
* @param IRequest $request
*
* @return IResponse | false
*/
public function action($name, IRequest $request = null, $cacheTime = 0)
Expand All @@ -85,20 +94,22 @@ public function action($name, IRequest $request = null, $cacheTime = 0)
if ($cache->initCache($cacheTime, get_class($this) . $method)) {
$return = $cache->getVars();
} elseif ($cache->startDataCache()) {
if (!is_callable(array($this, $method))) {
if (!is_callable([$this, $method])) {
$cache->abortDataCache();

return $return;
} else {
$return = $this->$method($request);

if(is_null($return)) {
if (is_null($return)) {
$cache->abortDataCache();
} elseif (is_object($return) && $return->getError()) {
$cache->abortDataCache();
}
}
$cache->endDataCache($return);
}

return $return;
}
}
}
Loading