Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change warning to logger for GSM-7 #382

Merged
merged 7 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 0 additions & 22 deletions rector.php

This file was deleted.

132 changes: 61 additions & 71 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
* Vonage API Client, allows access to the API from PHP.
*
* @method Account\Client account()
* @method Message\Client message()
SecondeJK marked this conversation as resolved.
Show resolved Hide resolved
* @method Messages\Client messages()
* @method Application\Client applications()
* @method Conversion\Client conversion()
Expand Down Expand Up @@ -291,7 +290,8 @@ public function getFactory(): ContainerInterface
}

/**
* @throws ClientException
* @deprecated Use a configured APIResource with a HandlerInterface
* Request business logic is being removed from the User Client Layer.
*/
public static function signRequest(RequestInterface $request, SignatureSecret $credentials): RequestInterface
{
Expand All @@ -304,6 +304,10 @@ public static function signRequest(RequestInterface $request, SignatureSecret $c
return $handler($request, $credentials);
}

/**
* @deprecated Use a configured APIResource with a HandlerInterface
* Request business logic is being removed from the User Client Layer.
*/
public static function authRequest(RequestInterface $request, Basic $credentials): RequestInterface
{
switch ($request->getHeaderLine('content-type')) {
Expand Down Expand Up @@ -344,10 +348,8 @@ public function generateJwt($claims = []): Token
}

/**
* Takes a URL and a key=>value array to generate a GET PSR-7 request object
*
* @throws ClientExceptionInterface
* @throws ClientException
* @deprecated Use a configured APIResource with a HandlerInterface
* Request business logic is being removed from the User Client Layer.
*/
public function get(string $url, array $params = []): ResponseInterface
{
Expand All @@ -360,10 +362,8 @@ public function get(string $url, array $params = []): ResponseInterface
}

/**
* Takes a URL and a key=>value array to generate a POST PSR-7 request object
*
* @throws ClientExceptionInterface
* @throws ClientException
* @deprecated Use a configured APIResource with a HandlerInterface
* Request business logic is being removed from the User Client Layer.
*/
public function post(string $url, array $params): ResponseInterface
{
Expand All @@ -380,30 +380,8 @@ public function post(string $url, array $params): ResponseInterface
}

/**
* Takes a URL and a key=>value array to generate a POST PSR-7 request object
*
* @throws ClientExceptionInterface
* @throws ClientException
*/
public function postUrlEncoded(string $url, array $params): ResponseInterface
SecondeJK marked this conversation as resolved.
Show resolved Hide resolved
{
$request = new Request(
$url,
'POST',
'php://temp',
['content-type' => 'application/x-www-form-urlencoded']
);

$request->getBody()->write(http_build_query($params));

return $this->send($request);
}

/**
* Takes a URL and a key=>value array to generate a PUT PSR-7 request object
*
* @throws ClientExceptionInterface
* @throws ClientException
* @deprecated Use a configured APIResource with a HandlerInterface
* Request business logic is being removed from the User Client Layer.
*/
public function put(string $url, array $params): ResponseInterface
{
Expand All @@ -420,10 +398,8 @@ public function put(string $url, array $params): ResponseInterface
}

/**
* Takes a URL and a key=>value array to generate a DELETE PSR-7 request object
*
* @throws ClientExceptionInterface
* @throws ClientException
* @deprecated Use a configured APIResource with a HandlerInterface
* Request business logic is being removed from the User Client Layer.
*/
public function delete(string $url): ResponseInterface
{
Expand All @@ -439,7 +415,6 @@ public function delete(string $url): ResponseInterface
* Wraps the HTTP Client, creates a new PSR-7 request adding authentication, signatures, etc.
*
* @throws ClientExceptionInterface
* @throws ClientException
*/
public function send(RequestInterface $request): ResponseInterface
{
Expand Down Expand Up @@ -523,22 +498,13 @@ protected function validateAppOptions($app): void
}
}

public function serialize(EntityInterface $entity): string
{
if ($entity instanceof Verification) {
return $this->verify()->serialize($entity);
}

throw new RuntimeException('unknown class `' . $entity::class . '``');
}

public function __call($name, $args)
{
if (!$this->factory->hasApi($name)) {
if (!$this->factory->has($name)) {
throw new RuntimeException('no api namespace found: ' . $name);
}

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

if (empty($args)) {
return $collection;
Expand All @@ -552,13 +518,48 @@ public function __call($name, $args)
*/
public function __get($name)
{
if (!$this->factory->hasApi($name)) {
if (!$this->factory->has($name)) {
throw new RuntimeException('no api namespace found: ' . $name);
}

return $this->factory->getApi($name);
return $this->factory->get($name);
}

/**
* @deprecated Use the Verify Client, this shouldn't be here and will be removed.
*/
public function serialize(EntityInterface $entity): string
{
if ($entity instanceof Verification) {
return $this->verify()->serialize($entity);
}

throw new RuntimeException('unknown class `' . $entity::class . '``');
}

protected function getVersion(): string
{
return InstalledVersions::getVersion('vonage/client-core');
}

public function getLogger(): ?LoggerInterface
{
if (!$this->logger && $this->getFactory()->has(LoggerInterface::class)) {
$this->setLogger($this->getFactory()->get(LoggerInterface::class));
}

return $this->logger;
}

public function getCredentials(): CredentialsInterface
{
return $this->credentials;
}

/**
* @deprecated Use a configured APIResource with a HandlerInterface
* Request business logic is being removed from the User Client Layer.
*/
protected static function requiresBasicAuth(RequestInterface $request): bool
{
$path = $request->getUri()->getPath();
Expand All @@ -568,6 +569,10 @@ protected static function requiresBasicAuth(RequestInterface $request): bool
return $isSecretManagementEndpoint || $isApplicationV2;
}

/**
* @deprecated Use a configured APIResource with a HandlerInterface
* Request business logic is being removed from the User Client Layer.
*/
protected static function requiresAuthInUrlNotBody(RequestInterface $request): bool
{
$path = $request->getUri()->getPath();
Expand All @@ -578,6 +583,10 @@ protected static function requiresAuthInUrlNotBody(RequestInterface $request): b
return $isRedact || $isMessages;
}

/**
* @deprecated Use a configured APIResource with a HandlerInterface
* Request business logic is being removed from the User Client Layer.
*/
protected function needsKeypairAuthentication(RequestInterface $request): bool
{
$path = $request->getUri()->getPath();
Expand All @@ -588,23 +597,4 @@ protected function needsKeypairAuthentication(RequestInterface $request): bool

return $isCallEndpoint || $isRecordingUrl || $isStitchEndpoint || $isUserEndpoint;
}

protected function getVersion(): string
{
return InstalledVersions::getVersion('vonage/client-core');
}

public function getLogger(): ?LoggerInterface
{
if (!$this->logger && $this->getFactory()->has(LoggerInterface::class)) {
$this->setLogger($this->getFactory()->get(LoggerInterface::class));
}

return $this->logger;
}

public function getCredentials(): CredentialsInterface
{
return $this->credentials;
}
}
12 changes: 6 additions & 6 deletions src/SMS/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@
namespace Vonage\SMS;

use Psr\Http\Client\ClientExceptionInterface;
use Psr\Log\LogLevel;
use Vonage\Client\APIClient;
use Vonage\Client\APIResource;
use Vonage\Client\Exception\Exception as ClientException;
use Vonage\Client\Exception\ThrottleException;
use Vonage\Logger\LoggerTrait;
use Vonage\SMS\Message\Message;
use Vonage\SMS\Message\SMS;
SecondeJK marked this conversation as resolved.
Show resolved Hide resolved

use function sleep;

class Client implements APIClient
{
use LoggerTrait;

public function __construct(protected APIResource $api)
{
}
Expand All @@ -38,11 +41,8 @@ public function getAPIResource(): APIResource
*/
public function send(Message $message): Collection
{
if ($message->getErrorMessage()) {
trigger_error(
$message->getErrorMessage(),
E_USER_WARNING
);
if ($warningMessage = $message->getWarningMessage()) {
$this->log(LogLevel::WARNING, $warningMessage);
}

try {
Expand Down
8 changes: 8 additions & 0 deletions src/SMS/Message/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,12 @@ public function getProtocolId(): ?int
{
return $this->protocolId;
}

/**
* @deprecated This will be removed once it comes out of the interface
*/
public function getErrorMessage(): ?string
{
return $this->getWarningMessage();
}
}
3 changes: 2 additions & 1 deletion src/SMS/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ interface Message
{
public function toArray(): array;
public function getErrorMessage(): ?string;
public function setErrorMessage(?string $errorMessage): void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to come back as well, and just wrap setWarningMessage()

public function getWarningMessage(): ?string;
public function setWarningMessage(?string $errorMessage): void;
}
10 changes: 5 additions & 5 deletions src/SMS/Message/OutboundMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class OutboundMessage implements Message
*/
protected $ttl = 259200000;

protected ?string $errorMessage = null;
protected ?string $warningMessage = null;

/**
* Type of message, set by the child class
Expand All @@ -49,14 +49,14 @@ public function __construct(protected string $to, protected string $from)
{
}

public function getErrorMessage(): ?string
SecondeJK marked this conversation as resolved.
Show resolved Hide resolved
public function getWarningMessage(): ?string
{
return $this->errorMessage;
return $this->warningMessage;
}

public function setErrorMessage(?string $errorMessage): void
public function setWarningMessage(?string $errorMessage): void
{
$this->errorMessage = $errorMessage;
$this->warningMessage = $errorMessage;
}

abstract public function toArray(): array;
Expand Down
19 changes: 11 additions & 8 deletions src/SMS/Message/SMS.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,26 @@ public function setEntityId(string $id): self
return $this;
}

/**
* @deprecated We don't throw errors, only warnings. This will be removed.
*
* @return string|null
*/
public function getErrorMessage(): ?string
{
if ($this->getType() === 'unicode' && self::isGsm7($this->getMessage())) {
$this->setErrorMessage("You are sending a message as `unicode` when it could be `text` or a
`text` type with unicode-only characters. This could result in increased billing -
See https://developer.vonage.com/messaging/sms for details, or email support@vonage.com if you have any
questions.");
}
return $this->getWarningMessage();
}

public function getWarningMessage(): ?string
{
if ($this->getType() === 'text' && ! self::isGsm7($this->getMessage())) {
$this->setErrorMessage("You are sending a message as `text` when contains unicode only
$this->setWarningMessage("You are sending a message as `text` when contains unicode only
characters. This could result in encoding problems with the target device - See
https://developer.vonage.com/messaging/sms for details, or email support@vonage.com if you have any
questions.");
}

return $this->errorMessage;
return $this->warningMessage;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/SMS/Message/Vcal.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ public function getEvent(): string
{
return $this->event;
}

SecondeJK marked this conversation as resolved.
Show resolved Hide resolved
/**
* @deprecated This will be removed once it comes out of the interface
*/
public function getErrorMessage(): ?string
{
return $this->getWarningMessage();
}
}
8 changes: 8 additions & 0 deletions src/SMS/Message/Vcard.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ public function getCard(): string
{
return $this->card;
}

SecondeJK marked this conversation as resolved.
Show resolved Hide resolved
/**
* @deprecated This will be removed once it comes out of the interface
*/
public function getErrorMessage(): ?string
{
return $this->getWarningMessage();
}
}