1
0
Fork 0
mirror of synced 2025-04-10 04:21:01 +00:00
This commit is contained in:
Alex Lushpai 2019-04-01 02:15:27 +03:00
parent 9290228aca
commit acf5ee4e9c
18 changed files with 4187 additions and 1 deletions

185
.gitignore vendored Normal file
View file

@ -0,0 +1,185 @@
# Created by .ignore support plugin (hsz.mobi)
### macOS template
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/dictionaries
.idea/**/shelf
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# CMake
cmake-build-debug/
cmake-build-release/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
### NetBeans template
nbproject/private/
nbbuild/
dist/
nbdist/
.nb-gradle/
### Eclipse template
.metadata
bin/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# PyDev specific (Python IDE for Eclipse)
*.pydevproject
# CDT-specific (C/C++ Development Tooling)
.cproject
# CDT- autotools
.autotools
# Java annotation processor (APT)
.factorypath
# PDT-specific (PHP Development Tools)
.buildpath
# sbteclipse plugin
.target
# Tern plugin
.tern-project
# TeXlipse plugin
.texlipse
# STS (Spring Tool Suite)
.springBeans
# Code Recommenders
.recommenders/
# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet
### Composer template
composer.phar
/vendor/
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
### Linux template
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*
### Windows template
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk

16
.travis.yml Normal file
View file

@ -0,0 +1,16 @@
language: php
cache:
directories:
- $HOME/.composer/cache
php:
- '7.0'
- '7.1'
- '7.2'
before_script:
- flags="-o"
- composer install $flags
script: php ./vendor/phpunit/phpunit/phpunit -c phpunit.xml.dist

View file

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2018 retailCRM
Copyright (c) 2018-2019 retailCRM
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

32
apigen.neon Normal file
View file

@ -0,0 +1,32 @@
extensions:
- php
source:
- src
exclude:
- tests/
- vendor/
- bin/
- docs/
charset:
- auto
- UTF-8
- Windows-1251
title: retailCRM PHP MG Bot API client
templateTheme: bootstrap
groups: auto
accessLevels:
- public
- protected
internal: true
php: false
tree: true
deprecated: true
todo: true
destination: ../mg-bot-api-client-php.pages/
download: false

40
composer.json Normal file
View file

@ -0,0 +1,40 @@
{
"name": "retailcrm/mg-bot-api-client-php",
"description": "PHP client for retailCRM MG Bot API",
"type": "library",
"keywords": ["API", "retailCRM", "REST", "bot"],
"homepage": "http://www.retailcrm.ru/",
"license": "MIT",
"authors": [
{
"name": "retailCRM",
"email": "support@retailcrm.ru"
}
],
"require": {
"php": ">=7.0",
"ext-curl": "*",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "6.5.*",
"phpmd/phpmd": "2.6.*",
"phpstan/phpstan": "0.9.*",
"squizlabs/php_codesniffer": "3.4.*"
},
"support": {
"email": "support@retailcrm.ru"
},
"autoload": {
"psr-4": { "RetailCrm\\": "src/" }
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"config": {
"bin-dir": "bin",
"process-timeout": 600
}
}

2918
composer.lock generated Normal file

File diff suppressed because it is too large Load diff

22
phpunit.xml.dist Normal file
View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="phpunit.xsd"
bootstrap="./tests/bootstrap.php"
colors="true"
verbose="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="RetailCrm">
<directory>tests/RetailCrm/Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src/RetailCrm</directory>
</whitelist>
</filter>
</phpunit>

251
phpunit.xsd Normal file
View file

@ -0,0 +1,251 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:annotation>
<xs:documentation source="http://www.phpunit.de/manual/3.7/en/appendixes.configuration.html">
This Schema file defines the rules by which the XML configuration file of PHPUnit 3.7 may be structured.
</xs:documentation>
<xs:appinfo source="http://www.phpunit.de/manual/current/en/appendixes.configuration.html"/>
</xs:annotation>
<xs:element name="phpunit" type="phpUnitType">
<xs:annotation>
<xs:documentation>Root Element</xs:documentation>
</xs:annotation>
</xs:element>
<xs:complexType name="filtersType">
<xs:choice>
<xs:sequence>
<xs:element name="blacklist" type="filterType"/>
<xs:element name="whitelist" type="whiteListType" minOccurs="0"/>
</xs:sequence>
<xs:sequence>
<xs:element name="whitelist" type="whiteListType"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
<xs:complexType name="filterType">
<xs:sequence>
<xs:group ref="pathGroup"/>
<xs:element name="exclude" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:group ref="pathGroup"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="whiteListType">
<xs:complexContent>
<xs:extension base="filterType">
<xs:attribute name="processUncoveredFilesFromWhitelist" default="true" type="xs:boolean"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="groupsType">
<xs:choice>
<xs:sequence>
<xs:element name="include" type="groupType"/>
<xs:element name="exclude" type="groupType" minOccurs="0"/>
</xs:sequence>
<xs:sequence>
<xs:element name="exclude" type="groupType"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
<xs:complexType name="groupType">
<xs:sequence>
<xs:element name="group" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="listenersType">
<xs:sequence>
<xs:element name="listener" type="objectType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="objectType">
<xs:sequence>
<xs:element name="arguments" minOccurs="0">
<xs:complexType>
<xs:group ref="argumentsGroup"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="class" type="xs:string" use="required"/>
<xs:attribute name="file" type="xs:anyURI"/>
</xs:complexType>
<xs:complexType name="arrayType">
<xs:sequence>
<xs:element name="element" type="argumentType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="argumentType">
<xs:group ref="argumentChoice"/>
<xs:attribute name="key" use="required"/>
</xs:complexType>
<xs:group name="argumentsGroup">
<xs:sequence>
<xs:element name="array" type="arrayType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="integer" type="xs:integer" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="string" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="double" type="xs:double" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="null" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="object" type="objectType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="file" type="xs:anyURI" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="directory" type="xs:anyURI" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:group>
<xs:group name="argumentChoice">
<xs:choice>
<xs:element name="array" type="arrayType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="integer" type="xs:integer" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="string" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="double" type="xs:double" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="null" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="object" type="objectType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="file" type="xs:anyURI" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="directory" type="xs:anyURI" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
</xs:group>
<xs:complexType name="loggersType">
<xs:sequence>
<xs:element name="log" type="loggerType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="loggerType">
<xs:attribute name="type">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="coverage-html"/>
<xs:enumeration value="coverage-clover"/>
<xs:enumeration value="json"/>
<xs:enumeration value="plain"/>
<xs:enumeration value="tap"/>
<xs:enumeration value="junit"/>
<xs:enumeration value="testdox-html"/>
<xs:enumeration value="testdox-text"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="target" type="xs:anyURI"/>
<xs:attribute name="title" type="xs:string"/>
<xs:attribute name="charset" type="xs:string" default="UTF-8"/>
<xs:attribute name="yui" type="xs:boolean" default="true"/>
<xs:attribute name="highlight" type="xs:boolean" default="false"/>
<xs:attribute name="lowUpperBound" type="xs:nonNegativeInteger" default="35"/>
<xs:attribute name="highLowerBound" type="xs:nonNegativeInteger" default="70"/>
<xs:attribute name="logIncompleteSkipped" type="xs:boolean" default="false"/>
</xs:complexType>
<xs:group name="pathGroup">
<xs:sequence>
<xs:element name="directory" type="directoryFilterType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="file" type="fileFilterType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:group>
<xs:complexType name="directoryFilterType">
<xs:simpleContent>
<xs:extension base="xs:anyURI">
<xs:attribute type="xs:string" name="suffix" default="Test.php"/>
<xs:attributeGroup ref="phpVersionGroup"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="fileFilterType">
<xs:simpleContent>
<xs:extension base="xs:anyURI">
<xs:attributeGroup ref="phpVersionGroup"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:attributeGroup name="phpVersionGroup">
<xs:attribute name="phpVersion" type="xs:string" default="5.3.0"/>
<xs:attribute name="phpVersionOperator" type="xs:string" default="&gt;="/>
</xs:attributeGroup>
<xs:complexType name="phpType">
<xs:sequence>
<xs:element name="includePath" type="xs:anyURI" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="ini" type="namedValueType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="const" type="namedValueType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="var" type="namedValueType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="env" type="namedValueType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="post" type="namedValueType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="get" type="namedValueType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="cookie" type="namedValueType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="server" type="namedValueType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="files" type="namedValueType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="request" type="namedValueType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="namedValueType">
<xs:attribute name="name" use="required" type="xs:string"/>
<xs:attribute name="value" use="required" type="xs:anySimpleType"/>
</xs:complexType>
<xs:complexType name="phpUnitType">
<xs:annotation>
<xs:documentation>The main type specifying the document structure</xs:documentation>
</xs:annotation>
<xs:group ref="configGroup"/>
<xs:attributeGroup ref="configAttributeGroup"/>
</xs:complexType>
<xs:attributeGroup name="configAttributeGroup">
<xs:attribute name="backupGlobals" type="xs:boolean" default="true"/>
<xs:attribute name="backupStaticAttributes" type="xs:boolean" default="false"/>
<xs:attribute name="bootstrap" type="xs:anyURI"/>
<xs:attribute name="cacheTokens" type="xs:boolean"/>
<xs:attribute name="colors" type="xs:boolean" default="false"/>
<xs:attribute name="convertErrorsToExceptions" type="xs:boolean" default="true"/>
<xs:attribute name="convertNoticesToExceptions" type="xs:boolean" default="true"/>
<xs:attribute name="convertWarningsToExceptions" type="xs:boolean" default="true"/>
<xs:attribute name="forceCoversAnnotation" type="xs:boolean" default="false"/>
<xs:attribute name="mapTestClassNameToCoveredClassName" type="xs:boolean" default="false"/>
<xs:attribute name="printerClass" type="xs:string" default="PHPUnit_TextUI_ResultPrinter"/>
<xs:attribute name="printerFile" type="xs:anyURI"/>
<xs:attribute name="processIsolation" type="xs:boolean" default="false"/>
<xs:attribute name="stopOnError" type="xs:boolean" default="false"/>
<xs:attribute name="stopOnFailure" type="xs:boolean" default="false"/>
<xs:attribute name="stopOnIncomplete" type="xs:boolean" default="false"/>
<xs:attribute name="stopOnSkipped" type="xs:boolean" default="false"/>
<xs:attribute name="strict" type="xs:boolean" default="false"/>
<xs:attribute name="testSuiteLoaderClass" type="xs:string" default="PHPUnit_Runner_StandardTestSuiteLoader"/>
<xs:attribute name="testSuiteLoaderFile" type="xs:anyURI"/>
<xs:attribute name="timeoutForSmallTests" type="xs:integer" default="1"/>
<xs:attribute name="timeoutForMediumTests" type="xs:integer" default="10"/>
<xs:attribute name="timeoutForLargeTests" type="xs:integer" default="60"/>
<xs:attribute name="verbose" type="xs:boolean" default="false"/>
</xs:attributeGroup>
<xs:group name="configGroup">
<xs:all>
<xs:element ref="testSuiteFacet" minOccurs="0"/>
<xs:element name="groups" type="groupsType" minOccurs="0"/>
<xs:element name="filter" type="filtersType" minOccurs="0"/>
<xs:element name="logging" type="loggersType" minOccurs="0"/>
<xs:element name="listeners" type="listenersType" minOccurs="0"/>
<xs:element name="php" type="phpType" minOccurs="0"/>
<xs:element name="selenium" type="seleniumType" minOccurs="0"/>
</xs:all>
</xs:group>
<xs:complexType name="seleniumType">
<xs:sequence>
<xs:element name="browser" type="browserType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="browserType">
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="browser" type="xs:string"/>
<xs:attribute name="host" type="xs:anyURI"/>
<xs:attribute name="port" type="xs:nonNegativeInteger"/>
<xs:attribute name="timeout" type="xs:nonNegativeInteger"/>
</xs:complexType>
<xs:element name="testSuiteFacet" abstract="true"/>
<xs:element name="testsuite" type="testSuiteType" substitutionGroup="testSuiteFacet"/>
<xs:element name="testsuites" type="testSuitesType" substitutionGroup="testSuiteFacet"/>
<xs:complexType name="testSuitesType">
<xs:sequence>
<xs:element name="testsuite" type="testSuiteType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="testSuiteType">
<xs:sequence>
<xs:group ref="pathGroup"/>
<xs:element name="exclude" type="xs:anyURI" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:schema>

View file

@ -0,0 +1,123 @@
<?php
/**
* PHP version 7.0
*
* Client
*
* @category RetailCrm
* @package Bot
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
namespace RetailCrm\Bot;
use RetailCrm\Bot\Exception\CurlException;
use RetailCrm\Bot\Exception\InvalidJsonException;
use Exception;
use InvalidArgumentException;
/**
* PHP version 7.0
*
* Client class
*
* @category RetailCrm
* @package Bot
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
class Client
{
const VERSION = 'v1';
protected $client;
/**
* Init
*
* @param string $url api url
* @param string $token api key
* @param bool $debug debug flag
*/
public function __construct($url, $token, $debug = false)
{
if ('/' !== $url[strlen($url) - 1]) {
$url .= '/';
}
$url = $url . 'api/bot/' . self::VERSION;
$this->client = new Request($url, $token, $debug);
}
/**
* Returns filtered bots list
*
* @param array $parameters (default: array())
*
* @throws InvalidArgumentException
* @throws CurlException
* @throws InvalidJsonException
* @throws Exception
*
* @return Response
*/
public function bots(array $parameters = [])
{
return $this->client->makeRequest('/bots', 'GET', $parameters);
}
/**
* Returns filtered channels list
*
* @param array $parameters (default: array())
*
* @throws InvalidArgumentException
* @throws CurlException
* @throws InvalidJsonException
* @throws Exception
*
* @return Response
*/
public function channels(array $parameters = [])
{
return $this->client->makeRequest('/channels', 'GET', $parameters);
}
/**
* Returns filtered chats list
*
* @param array $parameters (default: array())
*
* @throws InvalidArgumentException
* @throws CurlException
* @throws InvalidJsonException
* @throws Exception
*
* @return Response
*/
public function chats(array $parameters = [])
{
return $this->client->makeRequest('/chats', 'GET', $parameters);
}
/**
* Returns filtered customers list
*
* @param array $parameters (default: array())
*
* @throws InvalidArgumentException
* @throws CurlException
* @throws InvalidJsonException
* @throws Exception
*
* @return Response
*/
public function customers(array $parameters = [])
{
return $this->client->makeRequest('/customers', 'GET', $parameters);
}
}

View file

@ -0,0 +1,32 @@
<?php
/**
* PHP version 7.0
*
* CurlException
*
* @category RetailCrm
* @package Bot
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
namespace RetailCrm\Bot\Exception;
use RuntimeException;
/**
* PHP version 7.0
*
* Class CurlException
*
* @category RetailCrm
* @package Bot
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
class CurlException extends RuntimeException
{
}

View file

@ -0,0 +1,32 @@
<?php
/**
* PHP version 7.0
*
* CurlException
*
* @category RetailCrm
* @package Bot
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
namespace RetailCrm\Bot\Exception;
use DomainException;
/**
* PHP version 7.0
*
* Class InvalidJsonException
*
* @category RetailCrm
* @package Bot
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
class InvalidJsonException extends DomainException
{
}

View file

@ -0,0 +1,32 @@
<?php
/**
* PHP version 7.0
*
* CurlException
*
* @category RetailCrm
* @package Bot
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
namespace RetailCrm\Bot\Exception;
use DomainException;
/**
* PHP version 7.0
*
* Class CurlException
*
* @category RetailCrm
* @package Bot
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
class LimitException extends DomainException
{
}

View file

@ -0,0 +1,151 @@
<?php
/**
* PHP version 7.0
*
* Request
*
* @category RetailCrm
* @package Bot
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
namespace RetailCrm\Bot;
use RetailCrm\Bot\Exception\CurlException;
use RetailCrm\Bot\Exception\InvalidJsonException;
use RetailCrm\Bot\Exception\LimitException;
use Exception;
use InvalidArgumentException;
/**
* PHP version 7.0
*
* Request class
*
* @category RetailCrm
* @package Bot
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
class Request
{
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
const METHOD_PUT = 'PUT';
const METHOD_DELETE = 'DELETE';
protected $url;
protected $token;
private $debug;
/**
* Client constructor.
*
* @param string $url api url
* @param string $token api token
* @param bool $debug make request verbose
*/
public function __construct($url, $token, $debug)
{
if (false === stripos($url, 'https://')) {
throw new InvalidArgumentException('API schema requires HTTPS protocol');
}
$this->url = $url;
$this->token = $token;
$this->debug = $debug;
}
/**
* Make HTTP request
*
* @param string $path request url
* @param string $method (default: 'GET')
* @param array $parameters (default: array())
*
* @throws \InvalidArgumentException
* @throws \Exception
* @throws CurlException
* @throws InvalidJsonException
*
* @return Response
*/
public function makeRequest(
$path,
$method,
array $parameters = []
) {
$allowedMethods = [self::METHOD_GET, self::METHOD_POST, self::METHOD_PUT, self::METHOD_DELETE];
if (!in_array($method, $allowedMethods, false)) {
throw new InvalidArgumentException(
sprintf(
'Method "%s" is not valid. Allowed methods are %s',
$method,
implode(', ', $allowedMethods)
)
);
}
$url = $this->url . $path;
if (self::METHOD_GET === $method && count($parameters)) {
$url .= '?' . http_build_query($parameters, '', '&');
}
$curlHandler = curl_init();
curl_setopt($curlHandler, CURLOPT_URL, $url);
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curlHandler, CURLOPT_FAILONERROR, false);
curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curlHandler, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curlHandler, CURLOPT_TIMEOUT, 60);
curl_setopt($curlHandler, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($curlHandler, CURLOPT_VERBOSE, (int)$this->debug);
curl_setopt($curlHandler, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
sprintf("X-Bot-Token: %s", $this->token)
]);
if (in_array($method, [self::METHOD_POST, self::METHOD_PUT, self::METHOD_DELETE])) {
curl_setopt($curlHandler, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, json_encode($parameters));
}
$responseBody = curl_exec($curlHandler);
$statusCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE);
$response = Response::parseJSON($responseBody);
$errorMessage = !empty($response['errorMsg']) ? $response['errorMsg'] : '';
/**
* responses with 400 & 460 http codes contains extended error data
* therefore they are not handled as exceptions
*/
if (in_array($statusCode, [403, 404, 500])) {
throw new Exception($errorMessage);
}
if ($statusCode == 503) {
throw new LimitException($errorMessage);
}
$errno = curl_errno($curlHandler);
$error = curl_error($curlHandler);
curl_close($curlHandler);
if ($errno) {
throw new CurlException($error, $errno);
}
return new Response($statusCode, $responseBody);
}
}

View file

@ -0,0 +1,230 @@
<?php
/**
* PHP version 7.0
*
* Request
*
* @category RetailCrm
* @package Bot
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
namespace RetailCrm\Bot;
use BadMethodCallException;
use InvalidArgumentException;
use RetailCrm\Bot\Exception\InvalidJsonException;
/**
* PHP version 7.0
*
* Request class
*
* @category RetailCrm
* @package Bot
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
class Response implements \ArrayAccess
{
// HTTP response status code
protected $statusCode;
// response assoc array
protected $response;
// response body
protected $raw;
/**
* ApiResponse constructor.
*
* @param int $statusCode HTTP status code
* @param mixed $responseBody HTTP body
*
* @throws InvalidJsonException
*/
public function __construct($statusCode, $responseBody = null)
{
$this->statusCode = (int) $statusCode;
$this->raw = $responseBody;
$this->response = self::parseJSON($responseBody);
}
/**
* Return raw HTTP response
*
* @return string|null
*/
public function getRawResponse()
{
return $this->raw;
}
/**
* Return HTTP response
*
* @return array
*/
public function getResponse()
{
return $this->response;
}
/**
* Return HTTP response status code
*
* @return int
*/
public function getStatusCode()
{
return $this->statusCode;
}
/**
* HTTP request was successful
*
* @return bool
*/
public function isSuccessful()
{
return $this->statusCode < 400;
}
/**
* Allow to access for the property throw class method
*
* @param string $name method name
* @param mixed $arguments method parameters
*
* @throws \InvalidArgumentException
*
* @return mixed
*/
public function __call($name, $arguments)
{
// convert getSomeProperty to someProperty
$propertyName = strtolower(substr($name, 3, 1)) . substr($name, 4);
if (!isset($this->response[$propertyName])) {
throw new InvalidArgumentException("Method \"$name\" not found");
}
return $this->response[$propertyName];
}
/**
* Allow to access for the property throw object property
*
* @param string $name property name
*
* @throws \InvalidArgumentException
*
* @return mixed
*/
public function __get($name)
{
if (!isset($this->response[$name])) {
throw new InvalidArgumentException("Property \"$name\" not found");
}
return $this->response[$name];
}
/**
* Allow to check if the property exists through object property
*
* @param string $name property name
*
* @return bool
*/
public function __isset($name)
{
return isset($this->response[$name]);
}
/**
* Offset set
*
* @param mixed $offset offset
* @param mixed $value value
*
* @throws BadMethodCallException
* @return void
*/
public function offsetSet($offset, $value)
{
$message = sprintf("This call not allowed. Offset given: %s. Value given: %s", $offset, $value);
throw new BadMethodCallException($message);
}
/**
* Offset unset
*
* @param mixed $offset offset
*
* @throws BadMethodCallException
* @return void
*/
public function offsetUnset($offset)
{
$message = sprintf("This call not allowed. Offset given: %s", $offset);
throw new BadMethodCallException($message);
}
/**
* Check offset
*
* @param mixed $offset offset
*
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->response[$offset]);
}
/**
* Get offset
*
* @param mixed $offset offset
*
* @throws \InvalidArgumentException
*
* @return mixed
*/
public function offsetGet($offset)
{
if (!isset($this->response[$offset])) {
throw new InvalidArgumentException("Property \"$offset\" not found");
}
return $this->response[$offset];
}
/**
* @param string $responseBody
*
* @return array
*/
public static function parseJSON($responseBody): array
{
$result = [];
if (!empty($responseBody)) {
$response = json_decode($responseBody, true);
if (!$response && JSON_ERROR_NONE !== ($error = json_last_error())) {
throw new InvalidJsonException("Invalid JSON in the API response body. Error code #$error", $error);
}
$result = $response;
}
return $result;
}
}

View file

@ -0,0 +1,57 @@
<?php
/**
* PHP version 7.0
*
* RequestHelper
*
* @category RetailCrm
* @package Helper
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
namespace RetailCrm\Helper;
/**
* PHP version 7.0
*
* RequestHelper class
*
* @category RetailCrm
* @package Helper
* @author retailCRM <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://help.retailcrm.pro/docs/Developers
*/
class RequestHelper
{
/**
* Prepare module data
*
* @param array $config
* @param string $clientId
*
* @return array
*/
public static function moduleRequest(array $config, string $clientId): array
{
$config['host'] = str_replace("https://", '', $config['host']);
$moduleConfiguration = [
'code' => $config['code'],
'integrationCode' => $config['code'],
'active' => true,
'name' => $config['name'],
'clientId' => $clientId,
'logo' => sprintf("https://%s%s", $config['host'], $config['logo']),
'baseUrl' => sprintf("https://%s", $config['host']),
'accountURL' => sprintf("https://%s/settings/%s", $config['host'], $clientId),
'actions' => ['activity' => '/actions/activity'],
'integrations' => ['mgBot' => []]
];
return $moduleConfiguration;
}
}

View file

@ -0,0 +1,55 @@
<?php
/**
* PHP version 5.4
*
* Test case class
*
* @category RetailCrm
* @package Test
* @author RetailCrm <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://www.retailcrm.ru/docs/Developers/ApiVersion5
*/
namespace RetailCrm\Test;
use PHPUnit\Framework\TestCase as BaseCase;
use RetailCrm\Bot\Client;
/**
* Class TestCase
*
* @category RetailCrm
* @package Test
* @author RetailCrm <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://www.retailcrm.ru/docs/Developers/ApiVersion5
*/
class TestCase extends BaseCase
{
/**
* Return ApiClient object
*
* @param string $url (default: null)
* @param string $key (default: null)
* @param bool $debug (default: false)
*
* @return Client
*/
public static function getApiClient(
$url = null,
$key = null,
$debug = false
) {
$configUrl = getenv('MG_BOT_URL') ?: $_SERVER['MG_BOT_URL'];
$configKey = getenv('MG_BOT_KEY') ?: $_SERVER['MG_BOT_KEY'];
$configDbg = getenv('MG_BOT_DBG') ?: $_SERVER['MG_BOT_DBG'];
return new Client(
$url ?: $configUrl,
$key ?: $configKey,
$debug ?: $configDbg
);
}
}

View file

10
tests/bootstrap.php Normal file
View file

@ -0,0 +1,10 @@
<?php
if (function_exists('date_default_timezone_set')
&& function_exists('date_default_timezone_get')
) {
date_default_timezone_set(date_default_timezone_get());
}
$loader = include dirname(__DIR__) . '/vendor/autoload.php';
$loader->add('RetailCrm\\Test', __DIR__);