Skip to content

Commit

Permalink
Merge pull request #2 from d3no/master
Browse files Browse the repository at this point in the history
fix bug
  • Loading branch information
d3no committed Mar 2, 2021
2 parents ec9db94 + c63c7ee commit 262373b
Show file tree
Hide file tree
Showing 71 changed files with 1,882 additions and 26 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -2,7 +2,6 @@ language: php
dist: biotic

php:
- 7.1
- 7.2
- 7.3
- 7.4
Expand Down
10 changes: 6 additions & 4 deletions src/Client.php
Expand Up @@ -25,13 +25,14 @@
* @method \Mocean\Verify\Client verify()
* @method \Mocean\NumberLookup\Client numberLookup()
* @method \Mocean\Voice\Client voice()
* @method \Mocean\Command\Client command()
*/
class Client
{
public $version = '2';
public $baseUrl = 'https://rest.moceanapi.com';
public $baseUrl = 'https://rest.moceanapi.com';
const PL = 'PHP-SDK';
const SDK_VERSION = '2.1.0';
const SDK_VERSION = '2.2.0';
/**
* API Credentials.
*
Expand Down Expand Up @@ -66,7 +67,7 @@ class Client
public function __construct(CredentialsInterface $credentials, $options = [], HttpClient $client = null)
{
if ($client === null) {
$client = new \Http\Adapter\Guzzle6\Client(new GuzzleClient());
$client = new \Http\Adapter\Guzzle7\Client(new GuzzleClient());
}

$this->setHttpClient($client);
Expand Down Expand Up @@ -94,6 +95,7 @@ public function __construct(CredentialsInterface $credentials, $options = [], Ht
'verify' => 'Mocean\Verify\Client',
'numberLookup' => 'Mocean\NumberLookup\Client',
'voice' => 'Mocean\Voice\Client',
'command' => 'Mocean\Command\Client',
], $this));
}

Expand Down Expand Up @@ -210,7 +212,7 @@ public function __call($name, $args)
}

$collection = $this->factory->getApi($name);

if (empty($args)) {
return $collection;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Client/Factory/MapFactory.php
Expand Up @@ -8,6 +8,7 @@

namespace Mocean\Client\Factory;

use Composer\Autoload\ClassLoader;
use Mocean\Client;

class MapFactory implements FactoryInterface
Expand Down Expand Up @@ -56,9 +57,9 @@ public function getApi($api)
$api
));
}

$class = $this->map[$api];

$instance = new $class();
if ($instance instanceof Client\ClientAwareInterface) {
$instance->setClient($this->client);
Expand Down
92 changes: 92 additions & 0 deletions src/Command/Client.php
@@ -0,0 +1,92 @@
<?php

namespace Mocean\Command;

use GuzzleHttp\Psr7\Request;
use Mocean\Client\ClientAwareInterface;
use Mocean\Client\ClientAwareTrait;
use Mocean\Client\Exception\Exception;
use Mocean\Command\Mc\AbstractMc;

class Client implements ClientAwareInterface
{
use ClientAwareTrait;

private $action = McAction::AUTO;

/** @var Commander */
private $commander;

public function __construct()
{
$this->commander = new Commander();
}

/**
* @param $action action from \Mocean\Command\McAction
* @return self
*/
public function doAction($action)
{
$this->action = $action;
return $this;
}

public function execute($params)
{
if (isset($params["mocean-command"]) && !($params["mocean-command"] instanceof McBuilder)) {
throw new \InvalidArgumentException('commandBuilder must implement `'.McBuilder::class);
}

if (isset($params["mocean-event-url"])) {
$this->setEventUrl($params["mocean-event-url"]);
}
if (isset($params["mocean-resp-format"])) {
$this->setResponseFormat($params["mocean-resp-format"]);
}

$command = $params["mocean-command"]->build();

if (count($command) <= 0) {
throw new Exception('No command found in McBuilder.');
}

$this->commander->setCommand($command);

if ($this->action === McAction::AUTO || $this->action === McAction::SEND_TELEGRAM) {
$uri = "/send-message";
}

$this->setResponseFormat("json");

$params = $this->commander->getRequestData();

$request = new Request(
'POST',
$uri,
['content-type' => 'application/x-www-form-urlencoded']
);

$request->getBody()->write(http_build_query($params));
$response = $this->client->send($request);

$response->getBody()->rewind();
$data = $response->getBody()->getContents();
if (!isset($data) || $data === '') {
throw new Exception\Exception('unexpected response from API');
}

return Commander::createFromResponse($data, $this->client->version);
}

public function setEventUrl($url)
{
$this->commander->setEventUrl($url);
return $this;
}
public function setResponseFormat($type)
{
$this->commander->setResponseFormat($type);
return $this;
}
}
49 changes: 49 additions & 0 deletions src/Command/ClientTest.php
@@ -0,0 +1,49 @@
<?php

namespace MoceanTest\Command;

use Mocean\Command\Mc;
use Mocean\Command\McBuilder;
use MoceanTest\AbstractTesting;
use Psr\Http\Message\RequestInterface;

class ClientTest extends AbstractTesting
{
public function testExecute()
{
$inputParams = [
'mocean-event-url'=> 'https://moceanapi.com',
'mocean-command' => McBuilder::create()
->add(
Mc::tgSendText()
->setFrom("bot_username")
->setTo("123456789")
->setContent("Hello world")
),
'mocean-resp-format' => 'json'
];

$mockHttp = $this->makeMockHttpClient(function (RequestInterface $request) use ($inputParams) {
$this->assertEquals('POST', $request->getMethod());
$this->assertEquals($this->getTestUri('/send-message'), $request->getUri()->getPath());
$body = $this->getContentFromRequest($request);
$this->assertEquals($inputParams['mocean-event-url'], $body['mocean-event-url']);
$this->assertEquals(
Mc::tgSendText()
->setFrom("bot_username")
->setTo("123456789")
->setContent("Hello world")
->getRequestData(),

json_decode($body['mocean-command'], true)[0]
);

return $this->getResponse('command.xml');
});

$client = $this->makeMoceanClientWithMockHttpClient($mockHttp);

$commandRes = $client->command()->execute($inputParams);
$this->assertInstanceOf(\Mocean\Command\Commander::class, $commandRes);
}
}
54 changes: 54 additions & 0 deletions src/Command/Commander.php
@@ -0,0 +1,54 @@
<?php

namespace Mocean\Command;

use Mocean\Client\Exception\Exception;
use Mocean\Command\Mc\AbstractMc;
use Mocean\Model\ArrayAccessTrait;
use Mocean\Model\AsRequest;
use Mocean\Model\AsResponse;
use Mocean\Model\ModelInterface;
use Mocean\Model\ObjectAccessTrait;
use Mocean\Model\ResponseTrait;

class Commander implements ModelInterface, AsRequest, AsResponse
{
use ObjectAccessTrait, ResponseTrait, ArrayAccessTrait;

protected $requestData = [];

public function setEventUrl($eventUrl)
{
$this->requestData["mocean-event-url"] = $eventUrl;
return $this;
}

public function setCommand($command)
{
$this->requestData['mocean-command'] = json_encode($command);
return $this;
}

public function setResponseFormat($format)
{
$this->requestData['mocean-resp-format'] = $format;
}

public function getRequestData()
{
return $this->requestData;
}

public static function createFromResponse($responseData, $version)
{
$commander = new self(null, null);
$commander->setRawResponseData($responseData)
->processResponse($version);

if (isset($commander['status']) && $commander['status'] !== 0 && $commander['status'] !== '0') {
throw new Exception($commander['err_msg']);
}

return $commander;
}
}
27 changes: 27 additions & 0 deletions src/Command/CommanderTest.php
@@ -0,0 +1,27 @@
<?php

namespace MoceanTest\Command;

use MoceanTest\AbstractTesting;
use Mocean\Command\Commander;

class CommanderTest extends AbstractTesting
{
public function testRequestDataParams()
{
$params = [
'mocean-event-url' => 'testing event url',
'mocean-command' => '"testing mocean command"',
'mocean-resp-format' => 'json',
];


$setterReq = new Commander();
$setterReq->setEventUrl('testing event url');
$setterReq->setCommand('testing mocean command');
$setterReq->setResponseFormat('json');

$this->assertEquals($params, $setterReq->getRequestData());
}

}
57 changes: 57 additions & 0 deletions src/Command/Mc.php
@@ -0,0 +1,57 @@
<?php

namespace Mocean\Command;

use Mocean\Command\Mc\ContactType;
use Mocean\Command\Mc\SendSMS;
use Mocean\Command\Mc\TgSendPhoto;
use Mocean\Command\Mc\TgSendText;
use Mocean\Command\Mc\TgSendAudio;
use Mocean\Command\Mc\TgSendAnimation;
use Mocean\Command\Mc\TgSendDocument;
use Mocean\Command\Mc\TgSendVideo;
use Mocean\Command\Mc\TgRequestContact;


class Mc
{
public static function tgSendText()
{
return new TgSendText();
}

public static function tgSendAudio()
{
return new TgSendAudio();
}

public static function tgSendAnimation()
{
return new TgSendAnimation();
}

public static function tgSendDocument()
{
return new TgSendDocument();
}

public static function tgSendVideo()
{
return new TgSendVideo();
}

public static function tgSendPhoto()
{
return new TgSendPhoto();
}

public static function tgRequestContact()
{
return new TgRequestContact();
}

public static function sendSMS()
{
return new SendSMS();
}
}
28 changes: 28 additions & 0 deletions src/Command/Mc/AbstractMc.php
@@ -0,0 +1,28 @@
<?php

namespace Mocean\Command\Mc;

use Mocean\Model\AsRequest;
use Mocean\Client\Exception\Exception;

abstract class AbstractMc implements AsRequest
{
protected $requestData;

abstract public function action();

abstract protected function requiredKey();

public function getRequestData()
{
foreach ($this->requiredKey() as $param) {
if (!isset($this->requestData[$param])) {
throw new \InvalidArgumentException('missing expected key `'.$param.'` from '.static::class);
}
}

return array_merge($this->requestData, [
'action' => $this->action(),
]);
}
}

0 comments on commit 262373b

Please sign in to comment.