Skip to content

Commit 24a1c6d

Browse files
Change warning to logger for GSM-7 (#382)
* Swap out the trigger warning for a logger if one has been injected instead * Shims and deprecation warnings * Get rid of manchucks worst fear, namely dead commented code * fix interface tests * Update test/SMS/ClientTest.php Co-authored-by: Chris Tankersley <chris@ctankersley.com> * PR amendments * PR amendments --------- Co-authored-by: Chris Tankersley <chris@ctankersley.com>
1 parent 2c6a738 commit 24a1c6d

File tree

7 files changed

+105
-130
lines changed

7 files changed

+105
-130
lines changed

rector.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Client.php

Lines changed: 64 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
* Vonage API Client, allows access to the API from PHP.
7373
*
7474
* @method Account\Client account()
75-
* @method Message\Client message()
7675
* @method Messages\Client messages()
7776
* @method Application\Client applications()
7877
* @method Conversion\Client conversion()
@@ -291,7 +290,8 @@ public function getFactory(): ContainerInterface
291290
}
292291

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

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

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

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

382382
/**
383-
* Takes a URL and a key=>value array to generate a POST PSR-7 request object
384-
*
385-
* @throws ClientExceptionInterface
386-
* @throws ClientException
383+
* @deprecated Use a configured APIResource with a HandlerInterface
384+
* Request business logic is being removed from the User Client Layer.
387385
*/
388386
public function postUrlEncoded(string $url, array $params): ResponseInterface
389387
{
@@ -399,11 +397,10 @@ public function postUrlEncoded(string $url, array $params): ResponseInterface
399397
return $this->send($request);
400398
}
401399

400+
402401
/**
403-
* Takes a URL and a key=>value array to generate a PUT PSR-7 request object
404-
*
405-
* @throws ClientExceptionInterface
406-
* @throws ClientException
402+
* @deprecated Use a configured APIResource with a HandlerInterface
403+
* Request business logic is being removed from the User Client Layer.
407404
*/
408405
public function put(string $url, array $params): ResponseInterface
409406
{
@@ -420,10 +417,8 @@ public function put(string $url, array $params): ResponseInterface
420417
}
421418

422419
/**
423-
* Takes a URL and a key=>value array to generate a DELETE PSR-7 request object
424-
*
425-
* @throws ClientExceptionInterface
426-
* @throws ClientException
420+
* @deprecated Use a configured APIResource with a HandlerInterface
421+
* Request business logic is being removed from the User Client Layer.
427422
*/
428423
public function delete(string $url): ResponseInterface
429424
{
@@ -439,7 +434,6 @@ public function delete(string $url): ResponseInterface
439434
* Wraps the HTTP Client, creates a new PSR-7 request adding authentication, signatures, etc.
440435
*
441436
* @throws ClientExceptionInterface
442-
* @throws ClientException
443437
*/
444438
public function send(RequestInterface $request): ResponseInterface
445439
{
@@ -523,22 +517,13 @@ protected function validateAppOptions($app): void
523517
}
524518
}
525519

526-
public function serialize(EntityInterface $entity): string
527-
{
528-
if ($entity instanceof Verification) {
529-
return $this->verify()->serialize($entity);
530-
}
531-
532-
throw new RuntimeException('unknown class `' . $entity::class . '``');
533-
}
534-
535520
public function __call($name, $args)
536521
{
537-
if (!$this->factory->hasApi($name)) {
522+
if (!$this->factory->has($name)) {
538523
throw new RuntimeException('no api namespace found: ' . $name);
539524
}
540525

541-
$collection = $this->factory->getApi($name);
526+
$collection = $this->factory->get($name);
542527

543528
if (empty($args)) {
544529
return $collection;
@@ -552,13 +537,48 @@ public function __call($name, $args)
552537
*/
553538
public function __get($name)
554539
{
555-
if (!$this->factory->hasApi($name)) {
540+
if (!$this->factory->has($name)) {
556541
throw new RuntimeException('no api namespace found: ' . $name);
557542
}
558543

559-
return $this->factory->getApi($name);
544+
return $this->factory->get($name);
545+
}
546+
547+
/**
548+
* @deprecated Use the Verify Client, this shouldn't be here and will be removed.
549+
*/
550+
public function serialize(EntityInterface $entity): string
551+
{
552+
if ($entity instanceof Verification) {
553+
return $this->verify()->serialize($entity);
554+
}
555+
556+
throw new RuntimeException('unknown class `' . $entity::class . '``');
557+
}
558+
559+
protected function getVersion(): string
560+
{
561+
return InstalledVersions::getVersion('vonage/client-core');
562+
}
563+
564+
public function getLogger(): ?LoggerInterface
565+
{
566+
if (!$this->logger && $this->getFactory()->has(LoggerInterface::class)) {
567+
$this->setLogger($this->getFactory()->get(LoggerInterface::class));
568+
}
569+
570+
return $this->logger;
571+
}
572+
573+
public function getCredentials(): CredentialsInterface
574+
{
575+
return $this->credentials;
560576
}
561577

578+
/**
579+
* @deprecated Use a configured APIResource with a HandlerInterface
580+
* Request business logic is being removed from the User Client Layer.
581+
*/
562582
protected static function requiresBasicAuth(RequestInterface $request): bool
563583
{
564584
$path = $request->getUri()->getPath();
@@ -568,6 +588,10 @@ protected static function requiresBasicAuth(RequestInterface $request): bool
568588
return $isSecretManagementEndpoint || $isApplicationV2;
569589
}
570590

591+
/**
592+
* @deprecated Use a configured APIResource with a HandlerInterface
593+
* Request business logic is being removed from the User Client Layer.
594+
*/
571595
protected static function requiresAuthInUrlNotBody(RequestInterface $request): bool
572596
{
573597
$path = $request->getUri()->getPath();
@@ -578,6 +602,10 @@ protected static function requiresAuthInUrlNotBody(RequestInterface $request): b
578602
return $isRedact || $isMessages;
579603
}
580604

605+
/**
606+
* @deprecated Use a configured APIResource with a HandlerInterface
607+
* Request business logic is being removed from the User Client Layer.
608+
*/
581609
protected function needsKeypairAuthentication(RequestInterface $request): bool
582610
{
583611
$path = $request->getUri()->getPath();
@@ -588,23 +616,4 @@ protected function needsKeypairAuthentication(RequestInterface $request): bool
588616

589617
return $isCallEndpoint || $isRecordingUrl || $isStitchEndpoint || $isUserEndpoint;
590618
}
591-
592-
protected function getVersion(): string
593-
{
594-
return InstalledVersions::getVersion('vonage/client-core');
595-
}
596-
597-
public function getLogger(): ?LoggerInterface
598-
{
599-
if (!$this->logger && $this->getFactory()->has(LoggerInterface::class)) {
600-
$this->setLogger($this->getFactory()->get(LoggerInterface::class));
601-
}
602-
603-
return $this->logger;
604-
}
605-
606-
public function getCredentials(): CredentialsInterface
607-
{
608-
return $this->credentials;
609-
}
610619
}

src/SMS/Client.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@
1212
namespace Vonage\SMS;
1313

1414
use Psr\Http\Client\ClientExceptionInterface;
15+
use Psr\Log\LogLevel;
1516
use Vonage\Client\APIClient;
1617
use Vonage\Client\APIResource;
1718
use Vonage\Client\Exception\Exception as ClientException;
1819
use Vonage\Client\Exception\ThrottleException;
20+
use Vonage\Logger\LoggerTrait;
1921
use Vonage\SMS\Message\Message;
20-
use Vonage\SMS\Message\SMS;
2122

2223
use function sleep;
2324

2425
class Client implements APIClient
2526
{
27+
use LoggerTrait;
28+
2629
public function __construct(protected APIResource $api)
2730
{
2831
}
@@ -38,11 +41,8 @@ public function getAPIResource(): APIResource
3841
*/
3942
public function send(Message $message): Collection
4043
{
41-
if ($message->getErrorMessage()) {
42-
trigger_error(
43-
$message->getErrorMessage(),
44-
E_USER_WARNING
45-
);
44+
if ($warningMessage = $message->getWarningMessage()) {
45+
$this->log(LogLevel::WARNING, $warningMessage);
4646
}
4747

4848
try {

src/SMS/Message/Message.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ interface Message
1515
{
1616
public function toArray(): array;
1717
public function getErrorMessage(): ?string;
18-
public function setErrorMessage(?string $errorMessage): void;
18+
public function getWarningMessage(): ?string;
19+
public function setWarningMessage(?string $errorMessage): void;
1920
}

src/SMS/Message/OutboundMessage.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class OutboundMessage implements Message
3636
*/
3737
protected $ttl = 259200000;
3838

39-
protected ?string $errorMessage = null;
39+
protected ?string $warningMessage = null;
4040

4141
/**
4242
* Type of message, set by the child class
@@ -49,14 +49,22 @@ public function __construct(protected string $to, protected string $from)
4949
{
5050
}
5151

52+
/**
53+
* @deprecated Shim when correcting naming conventions, will be removed when it comes out the interface
54+
*/
5255
public function getErrorMessage(): ?string
5356
{
54-
return $this->errorMessage;
57+
return $this->getWarningMessage();
58+
}
59+
60+
public function getWarningMessage(): ?string
61+
{
62+
return $this->warningMessage;
5563
}
5664

57-
public function setErrorMessage(?string $errorMessage): void
65+
public function setWarningMessage(?string $errorMessage): void
5866
{
59-
$this->errorMessage = $errorMessage;
67+
$this->warningMessage = $errorMessage;
6068
}
6169

6270
abstract public function toArray(): array;

src/SMS/Message/SMS.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,16 @@ public function setEntityId(string $id): self
5858
return $this;
5959
}
6060

61-
public function getErrorMessage(): ?string
61+
public function getWarningMessage(): ?string
6262
{
63-
if ($this->getType() === 'unicode' && self::isGsm7($this->getMessage())) {
64-
$this->setErrorMessage("You are sending a message as `unicode` when it could be `text` or a
65-
`text` type with unicode-only characters. This could result in increased billing -
66-
See https://developer.vonage.com/messaging/sms for details, or email support@vonage.com if you have any
67-
questions.");
68-
}
69-
7063
if ($this->getType() === 'text' && ! self::isGsm7($this->getMessage())) {
71-
$this->setErrorMessage("You are sending a message as `text` when contains unicode only
64+
$this->setWarningMessage("You are sending a message as `text` which contains non-GSM7
7265
characters. This could result in encoding problems with the target device - See
7366
https://developer.vonage.com/messaging/sms for details, or email support@vonage.com if you have any
7467
questions.");
7568
}
7669

77-
return $this->errorMessage;
70+
return $this->warningMessage;
7871
}
7972

8073
/**

0 commit comments

Comments
 (0)