1
0
Fork 0
mirror of synced 2025-04-10 04:21:01 +00:00

move validator call to Request

This commit is contained in:
Alex Lushpai 2019-04-04 19:30:12 +03:00
parent fbbfaa5b0e
commit b8db7faf0d
16 changed files with 134 additions and 93 deletions

View file

@ -0,0 +1,3 @@
MG_BOT_URL=""
MG_BOT_KEY=""
MG_BOT_DBG=""

2
.gitignore vendored
View file

@ -4,7 +4,7 @@
.DS_Store
.AppleDouble
.LSOverride
.env
# Icon must end with two \r
Icon

View file

@ -1,13 +1,13 @@
language: php
cache:
directories:
- $HOME/.composer/cache
directories:
- $HOME/.composer/cache
php:
- '7.0'
- '7.1'
- '7.2'
- '7.0'
- '7.1'
- '7.2'
before_script:
- flags="-o"

View file

@ -24,7 +24,8 @@
"phpunit/phpunit": "6.5.*",
"phpmd/phpmd": "2.6.*",
"phpstan/phpstan": "0.9.*",
"squizlabs/php_codesniffer": "3.4.*"
"squizlabs/php_codesniffer": "3.4.*",
"symfony/dotenv": "^4.2"
},
"support": {
"email": "support@retailcrm.ru"

59
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "48599ce720e1f6fdb177855a1da35720",
"content-hash": "36d096c81a3aa192b431d6237766fca4",
"packages": [
{
"name": "doctrine/annotations",
@ -3272,6 +3272,63 @@
"homepage": "https://symfony.com",
"time": "2019-03-30T15:58:42+00:00"
},
{
"name": "symfony/dotenv",
"version": "v4.2.5",
"source": {
"type": "git",
"url": "https://github.com/symfony/dotenv.git",
"reference": "b541d63b83532be55a020db8ed2e50598385a583"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/dotenv/zipball/b541d63b83532be55a020db8ed2e50598385a583",
"reference": "b541d63b83532be55a020db8ed2e50598385a583",
"shasum": ""
},
"require": {
"php": "^7.1.3"
},
"require-dev": {
"symfony/process": "~3.4|~4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.2-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Dotenv\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Registers environment variables from a .env file",
"homepage": "https://symfony.com",
"keywords": [
"dotenv",
"env",
"environment"
],
"time": "2019-04-01T07:32:59+00:00"
},
{
"name": "symfony/filesystem",
"version": "v4.2.5",

View file

@ -10,13 +10,13 @@
<testsuites>
<testsuite name="RetailCrm">
<directory>tests/RetailCrm/Tests</directory>
<directory>tests/Bot/Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src/RetailCrm</directory>
<directory suffix=".php">./src/Bot</directory>
</whitelist>
</filter>
</phpunit>

View file

@ -126,12 +126,6 @@ class Client
*/
public function commandEdit(Model\Request\CommandEditRequest $request)
{
$validation = Component\Validator::validate($request);
if (!empty($validation)) {
throw new InvalidArgumentException($validation);
}
return $this->client->makeRequest(
sprintf("/my/commands/%s", $request->getName()),
Request::METHOD_PUT,

View file

@ -1,53 +0,0 @@
<?php
/**
* PHP version 7.0
*
* Validator
*
* @package RetailCrm\Mg\Bot\Component
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
namespace RetailCrm\Mg\Bot\Component;
use Symfony\Component\Validator\Validation;
/**
* PHP version 7.0
*
* Validator class
*
* @package RetailCrm\Mg\Bot\Component
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
class Validator
{
/**
* Validate given class
*
* @param $class
*
* @return string
*/
public static function validate($class)
{
$errorsString = '';
$validator = Validation::createValidatorBuilder()
->addMethodMapping('loadValidatorMetadata')
->getValidator();
$errors = $validator->validate($class);
if (count($errors) > 0) {
$errorsString = (string) $errors;
}
return $errorsString;
}
}

View file

@ -33,7 +33,7 @@ class IntegrationHelper
*
* @return array
*/
public static function moduleRequest(array $config, string $clientId): array
public static function moduleRequest(array $config, string $clientId)
{
$config['host'] = str_replace("https://", '', $config['host']);

View file

@ -52,7 +52,7 @@ class CommandEditRequest
/**
* @return string
*/
public function getName(): string
public function getName()
{
return $this->name;
}
@ -68,7 +68,7 @@ class CommandEditRequest
/**
* @return string
*/
public function getDescription(): string
public function getDescription()
{
return $this->description;
}

View file

@ -79,7 +79,7 @@ class DialogsRequest
/**
* @return int
*/
public function getChatId(): int
public function getChatId()
{
return $this->chatId;
}
@ -95,7 +95,7 @@ class DialogsRequest
/**
* @return int
*/
public function getUserId(): int
public function getUserId()
{
return $this->userId;
}
@ -111,7 +111,7 @@ class DialogsRequest
/**
* @return int
*/
public function getBotId(): int
public function getBotId()
{
return $this->botId;
}
@ -127,7 +127,7 @@ class DialogsRequest
/**
* @return int
*/
public function getActive(): int
public function getActive()
{
return $this->active;
}
@ -143,7 +143,7 @@ class DialogsRequest
/**
* @return int
*/
public function getAssign(): int
public function getAssign()
{
return $this->assign;
}

View file

@ -77,7 +77,7 @@ class MembersRequest
/**
* @return int
*/
public function getChatId(): int
public function getChatId()
{
return $this->chatId;
}
@ -93,7 +93,7 @@ class MembersRequest
/**
* @return int
*/
public function getUserId(): int
public function getUserId()
{
return $this->userId;
}
@ -109,7 +109,7 @@ class MembersRequest
/**
* @return string
*/
public function getState(): string
public function getState()
{
return $this->state;
}
@ -125,7 +125,7 @@ class MembersRequest
/**
* @return string
*/
public function getSince(): string
public function getSince()
{
return $this->since;
}
@ -141,7 +141,7 @@ class MembersRequest
/**
* @return string
*/
public function getUntil(): string
public function getUntil()
{
return $this->until;
}

View file

@ -115,7 +115,7 @@ class MessagesRequest
/**
* @return int
*/
public function getChatId(): int
public function getChatId()
{
return $this->chatId;
}
@ -131,7 +131,7 @@ class MessagesRequest
/**
* @return int
*/
public function getDialogId(): int
public function getDialogId()
{
return $this->dialogId;
}
@ -147,7 +147,7 @@ class MessagesRequest
/**
* @return int
*/
public function getUserId(): int
public function getUserId()
{
return $this->userId;
}
@ -163,7 +163,7 @@ class MessagesRequest
/**
* @return int
*/
public function getCustomerId(): int
public function getCustomerId()
{
return $this->customerId;
}
@ -179,7 +179,7 @@ class MessagesRequest
/**
* @return int
*/
public function getBotId(): int
public function getBotId()
{
return $this->botId;
}
@ -195,7 +195,7 @@ class MessagesRequest
/**
* @return int
*/
public function getChannelId(): int
public function getChannelId()
{
return $this->channelId;
}
@ -211,7 +211,7 @@ class MessagesRequest
/**
* @return string
*/
public function getChannelType(): string
public function getChannelType()
{
return $this->channelType;
}
@ -227,7 +227,7 @@ class MessagesRequest
/**
* @return string
*/
public function getScope(): string
public function getScope()
{
return $this->scope;
}
@ -243,7 +243,7 @@ class MessagesRequest
/**
* @return string
*/
public function getType(): string
public function getType()
{
return $this->type;
}

View file

@ -18,6 +18,7 @@ use RetailCrm\Common\Exception\CurlException;
use RetailCrm\Common\Exception\LimitException;
use Exception;
use InvalidArgumentException;
use Symfony\Component\Validator\Validation;
/**
* PHP version 7.0
@ -77,10 +78,13 @@ class Request
public function makeRequest($path, $method, $request = null, $serializeTo = self::S_JSON)
{
$this->validateMethod($method);
$this->validateRequest($request);
$parameters = $this->serialize($request, $serializeTo);
$url = $this->buildUrl($path, $method, $parameters);
var_dump($url);
$curlHandler = curl_init();
curl_setopt($curlHandler, CURLOPT_URL, $url);
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1);
@ -167,6 +171,27 @@ class Request
}
}
/**
* Validate given class
*
* @param string $class
*
* @return void
*/
private function validateRequest($class)
{
$validator = Validation::createValidatorBuilder()
->addMethodMapping('loadValidatorMetadata')
->getValidator();
$errors = $validator->validate($class);
if ($errors->count() > 0) {
$message = (string) $errors;
throw new InvalidArgumentException($message);
}
}
/**
* Serialize given object to JSON or Array
*

View file

@ -14,7 +14,7 @@
namespace RetailCrm\Mg\Bot\Tests;
use InvalidArgumentException;
use RetailCrm\Mg\Bot\Client;
use RetailCrm\Mg\Bot\Model\Request\ChannelsRequest;
use RetailCrm\Mg\Bot\Model\Request\CommandEditRequest;
use RetailCrm\Mg\Bot\Test\TestCase;
@ -33,10 +33,19 @@ class ClientTest extends TestCase
/**
* @group("list")
* @throws \Exception
*/
public function testChannels()
{
self::assertTrue(1 == 1);
$client = self::getApiClient();
$request = new ChannelsRequest();
$request->setActive(true);
$request->setTypes(['viber']);
$channels = $client->channels($request);
var_dump($channels->getResponse());
self::assertTrue($channels->isSuccessful() == true);
}
public function testChats()

View file

@ -8,3 +8,8 @@ if (function_exists('date_default_timezone_set')
$loader = include dirname(__DIR__) . '/vendor/autoload.php';
$loader->add('RetailCrm\\Mg\\Bot\\Test', __DIR__);
use Symfony\Component\Dotenv\Dotenv;
$dotenv = new Dotenv();
$dotenv->load(__DIR__ . '/../.env');