Skip to content

Commit

Permalink
exceptions and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rohsyl committed Nov 21, 2022
1 parent ef3dcae commit 3d20166
Show file tree
Hide file tree
Showing 19 changed files with 308 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/Commands/SaltoClientCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace rohsyl\Salto\Commands;

use Illuminate\Console\Command;
use rohsyl\Salto\Message\EncodeMobileMessage;
use rohsyl\Salto\Messages\EncodeMobileMessage;
use rohsyl\Salto\SaltoClient;
use Carbon\Carbon;

Expand Down
13 changes: 13 additions & 0 deletions src/Exceptions/ConnectionFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace rohsyl\Salto\Exceptions;

use Exception;

class ConnectionFailedException extends Exception
{
public function __construct($endpoint, $port)
{
parent::__construct("Failed to connect to '.$endpoint.':'.$port.'", 5);
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/NakException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace rohsyl\Salto\Exceptions;

use rohsyl\Salto\Response\Response;

class NakException extends SaltoException
{
public function __construct(Response $response)
{
parent::__construct($response, 'Nak. Server refused the command', 11);
}
}
25 changes: 25 additions & 0 deletions src/Exceptions/SaltoErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace rohsyl\Salto\Exceptions;

use rohsyl\Salto\Response\Response;

class SaltoErrorException extends SaltoException
{
public function __construct(Response $response)
{
parent::__construct(
$response,
'Error : ' . $response->getCommandName() . ' : ' . $response->getErrorDescription(),
12
);
}

public function getErrorName() : string {
return $this->getResponse()->getCommandName();
}

public function getErrorDescription() : ?string {
return $this->getResponse()->getErrorDescription();
}
}
27 changes: 27 additions & 0 deletions src/Exceptions/SaltoException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace rohsyl\Salto\Exceptions;

use rohsyl\Salto\Messages\Message;
use rohsyl\Salto\Response\Response;

class SaltoException extends \Exception
{
private Response $response;

public function __construct(Response $response, string $message = "", int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->response = $response;
}

public function getResponse(): Response
{
return $this->response;
}

public function getRequest() : ?Message
{
return $this->getResponse()->getRequest();
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/WrongChecksumException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace rohsyl\Salto\Exceptions;

use rohsyl\Salto\Response\Response;

class WrongChecksumException extends SaltoException
{
public function __construct(Response $response)
{
parent::__construct($response, "Wrong checksum exception", 10);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace rohsyl\Salto\Message;
namespace rohsyl\Salto\Messages;

class CheckoutMessage extends Message
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace rohsyl\Salto\Message;
namespace rohsyl\Salto\Messages;

class CopyMobileMessage extends EncodeMobileMessage
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace rohsyl\Salto\Message;
namespace rohsyl\Salto\Messages;

use rohsyl\Salto\SaltoClient;
use Carbon\Carbon;
Expand Down
2 changes: 1 addition & 1 deletion src/Message/Message.php → src/Messages/Message.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace rohsyl\Salto\Message;
namespace rohsyl\Salto\Messages;

use rohsyl\Salto\SaltoClient;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace rohsyl\Salto\Message;
namespace rohsyl\Salto\Messages;

use Carbon\Carbon;
use rohsyl\Salto\SaltoClient;
Expand Down
68 changes: 67 additions & 1 deletion src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,40 @@

namespace rohsyl\Salto\Response;

use rohsyl\Salto\Salto;
use rohsyl\Salto\Messages\Message;
use rohsyl\Salto\SaltoClient;

/**
*
*/
class Response
{
/**
* Instance an Ack response
* @return Response
*/
public static function Ack() {
return new self(SaltoClient::ACK);
}

/**
* Instance a Nak response
* @return Response
*/
public static function Nak() {
return new self(SaltoClient::NAK);
}

protected $request;

/**
* @var array The raw response sent by the server. Each row contains a field.
*/
protected $rawResponse;

/**
* @var string|null The checksum of the response
*/
protected $checksum;

public function __construct($rawResponse, $checksum = null)
Expand All @@ -24,41 +44,87 @@ public function __construct($rawResponse, $checksum = null)
$this->checksum = $checksum;
}

/**
* Get the command name that should be included in the response
* @return mixed
*/
public function getCommandName() {
return $this->rawResponse[1];
}

/**
* Is Ack
* @return bool
*/
public function isAck() {
return $this->rawResponse === SaltoClient::ACK;
}

/**
* Is Nak
* @return bool
*/
public function isNak() {
return $this->rawResponse === SaltoClient::NAK;
}

/**
* Is a response to a command/message sent.
* @return bool
*/
public function isMessage() {
return $this->rawResponse[0] === SaltoClient::STX;
}

/**
* Get the raw message array
* @return array
*/
public function getRawMessageArray() {
return array_slice($this->rawResponse, 1, sizeof($this->rawResponse) - 2);
}

/**
* Is this response an error response
* @return bool
*/
public function isError() {
return in_array($this->rawResponse[1], SaltoClient::getErrors());
}

/**
* Get the text decription of the error (if there is one)
* @return string|null
*/
public function getErrorDescription() {
return SaltoClient::getErrorDescription($this->rawResponse[0]);
}

/**
* Get the raw response
* @return array
*/
public function raw() {
return $this->rawResponse;
}

/**
* Return true if the checksum is valid
* @return bool
*/
public function check() {
if($this->checksum == SaltoClient::LRC_SKIP) return true;

return $this->checksum == SaltoClient::computeLrc($this->getRawMessageArray());
}

public function setRequest(Message $request): void
{
$this->request = $request;
}

public function getRequest() : Message
{
return $this->request;
}
}
12 changes: 12 additions & 0 deletions src/Salto.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
namespace rohsyl\Salto;

use Illuminate\Support\Facades\Facade;
use rohsyl\Salto\Testing\FakeSaltoManager;

/**
* @method static Socket getSocket(SaltoClient $client)
* @method static SaltoClient getClient(string $endpoint, int $port)
*
* @see SaltoManager
*/
class Salto extends Facade
Expand All @@ -18,4 +22,12 @@ protected static function getFacadeAccessor()
{
return SaltoManager::class;
}


public static function fake()
{
$fake = new FakeSaltoManager();
static::swap($fake);
return $fake;
}
}
Loading

0 comments on commit 3d20166

Please sign in to comment.