Skip to content

Commit 9c68bfb

Browse files
committed
refactor: implements more abstraction
1 parent 236781c commit 9c68bfb

21 files changed

+324
-155
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*****************************************************************************
4+
*
5+
* PROJECT: MTA PHP SDK
6+
* LICENSE: See LICENSE in the top level directory
7+
* FILE: AccessDeniedException.php
8+
*
9+
* Multi Theft Auto is available from http://www.multitheftauto.com/
10+
*
11+
*****************************************************************************/
12+
13+
declare(strict_types=1);
14+
15+
namespace MultiTheftAuto\Sdk\Exception;
16+
17+
use Exception;
18+
19+
class AccessDeniedException extends Exception
20+
{
21+
protected const EXCEPTION_MESSAGE = 'Access Denied. This server requires authentication. Please ensure that a valid username and password combination is provided.';
22+
23+
public function __construct()
24+
{
25+
parent::__construct(self::EXCEPTION_MESSAGE);
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*****************************************************************************
4+
*
5+
* PROJECT: MTA PHP SDK
6+
* LICENSE: See LICENSE in the top level directory
7+
* FILE: NotFoundBodyException.php
8+
*
9+
* Multi Theft Auto is available from http://www.multitheftauto.com/
10+
*
11+
*****************************************************************************/
12+
13+
declare(strict_types=1);
14+
15+
namespace MultiTheftAuto\Sdk\Exception;
16+
17+
use Exception;
18+
19+
class FunctionNotFoundException extends Exception
20+
{
21+
protected const EXCEPTION_MESSAGE = 'Attempted function call was not found';
22+
23+
public function __construct()
24+
{
25+
parent::__construct(self::EXCEPTION_MESSAGE);
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*****************************************************************************
4+
*
5+
* PROJECT: MTA PHP SDK
6+
* LICENSE: See LICENSE in the top level directory
7+
* FILE: NotFoundStatusException.php
8+
*
9+
* Multi Theft Auto is available from http://www.multitheftauto.com/
10+
*
11+
*****************************************************************************/
12+
13+
declare(strict_types=1);
14+
15+
namespace MultiTheftAuto\Sdk\Exception;
16+
17+
use Exception;
18+
19+
class NotFoundStatusException extends Exception
20+
{
21+
protected const EXCEPTION_MESSAGE = 'There was a problem with the request. Ensure that the resource exists and that the name is spelled correctly.';
22+
23+
public function __construct()
24+
{
25+
parent::__construct(self::EXCEPTION_MESSAGE);
26+
}
27+
}

src/Utils/ElementFactory.php renamed to src/Factory/ElementFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
declare(strict_types=1);
1414

15-
namespace MultiTheftAuto\Sdk\Utils;
15+
namespace MultiTheftAuto\Sdk\Factory;
1616

1717
use MultiTheftAuto\Sdk\Model\Element;
1818
use MultiTheftAuto\Sdk\Model\Resource;

src/Model/Resource.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
use Exception;
1818
use JsonSerializable;
19-
use MultiTheftAuto\Sdk\Mta;
19+
use MultiTheftAuto\Sdk\Service\MtaService;
2020

2121
class Resource implements JsonSerializable
2222
{
@@ -26,16 +26,17 @@ class Resource implements JsonSerializable
2626
private $name;
2727

2828
/**
29-
* @var Mta|null
29+
* @var MtaService|null
3030
*/
31-
private $mta;
31+
private $mtaService;
3232

3333
public const SERVER_PREFIX = '^R^';
34+
protected const UNDEFINED_SERVICE_EXCEPTION = 'Resource %s can not be called because Mta service is not defined';
3435

35-
public function __construct(string $name, Mta $mta = null)
36+
public function __construct(string $name, MtaService $mtaService = null)
3637
{
3738
$this->name = $name;
38-
$this->mta = $mta;
39+
$this->mtaService = $mtaService;
3940
}
4041

4142
public static function fromServer(string $value): self
@@ -49,16 +50,20 @@ public function getName()
4950
return $this->name;
5051
}
5152

52-
public function call($function, ...$arguments)
53+
/**
54+
* @throws \Http\Client\Exception
55+
* @throws Exception
56+
*/
57+
public function call(string $function, array ...$arguments)
5358
{
54-
if (!$this->mta) {
55-
throw new Exception(sprintf('Resource %s can not be called because Mta manager is not defined', $this->name));
59+
if (!$this->mtaService) {
60+
throw new Exception(sprintf(self::UNDEFINED_SERVICE_EXCEPTION, $this->name));
5661
}
5762

58-
return $this->mta->callFunction($this->name, $function, $arguments);
63+
return $this->mtaService->callFunction($this->name, $function, $arguments);
5964
}
6065

61-
public function __call($name, $arguments)
66+
public function __call(string $name, array $arguments)
6267
{
6368
array_unshift($arguments, $name);
6469
return call_user_func_array([$this, 'call'], $arguments);

src/Model/Server.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public function getPort(): int
4848
return $this->httpPort;
4949
}
5050

51-
public function getBaseUri(): string
51+
public function getEndpoint(): string
5252
{
53-
return sprintf('http://%s:%s', $this->host, $this->httpPort);
53+
return sprintf('%s:%s', $this->host, $this->httpPort);
5454
}
5555
}

src/Mta.php

Lines changed: 22 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,76 +14,60 @@
1414

1515
namespace MultiTheftAuto\Sdk;
1616

17-
use GuzzleHttp\Psr7\Stream;
18-
use Http\Client\HttpClient;
1917
use Http\Discovery\HttpClientDiscovery;
2018
use Http\Discovery\Psr17FactoryDiscovery;
21-
use Http\Message\Authentication\BasicAuth;
2219
use MultiTheftAuto\Sdk\Model\Authentication;
2320
use MultiTheftAuto\Sdk\Model\Resource as MtaResource;
2421
use MultiTheftAuto\Sdk\Model\Resources;
2522
use MultiTheftAuto\Sdk\Model\Server;
26-
use MultiTheftAuto\Sdk\Response\HandleResponse;
27-
use MultiTheftAuto\Sdk\Response\HttpStatusVerification;
28-
use MultiTheftAuto\Sdk\Utils\ElementTransformer;
29-
use MultiTheftAuto\Sdk\Utils\Input;
23+
use MultiTheftAuto\Sdk\Service\MtaService;
24+
use MultiTheftAuto\Sdk\Transformer\ElementTransformer;
25+
use MultiTheftAuto\Sdk\Util\Input;
26+
use Psr\Http\Client\ClientInterface;
3027
use Psr\Http\Message\RequestFactoryInterface;
3128
use Psr\Http\Message\StreamFactoryInterface;
3229

3330
class Mta
3431
{
3532
/**
36-
* @var Server
33+
* @var MtaService
3734
*/
38-
protected $server;
39-
40-
/**
41-
* @var Authentication
42-
*/
43-
protected $auth;
35+
protected $mtaService;
4436

4537
/**
4638
* @var Resources
4739
*/
4840
protected $resources;
4941

50-
/**
51-
* @var HttpClient
52-
*/
53-
protected $httpClient;
54-
55-
/**
56-
* @var RequestFactoryInterface
57-
*/
58-
protected $requestFactory;
59-
60-
/**
61-
* @var StreamFactoryInterface
62-
*/
63-
protected $streamFactory;
64-
6542
public function __construct(
6643
Server $server,
6744
Authentication $auth,
68-
HttpClient $httpClient = null,
45+
ClientInterface $httpClient = null,
6946
RequestFactoryInterface $requestFactory = null,
7047
StreamFactoryInterface $streamFactory = null
7148
) {
72-
$this->server = $server;
73-
$this->auth = $auth;
74-
$this->httpClient = $httpClient ?? HttpClientDiscovery::find();
75-
$this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory();
76-
$this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory();
49+
$this->mtaService = new MtaService(
50+
$server,
51+
$auth,
52+
$httpClient ?? HttpClientDiscovery::find(),
53+
$requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(),
54+
$streamFactory ?? Psr17FactoryDiscovery::findStreamFactory()
55+
);
7756

7857
$this->resources = new Resources();
7958
}
8059

60+
public function getService(): MtaService
61+
{
62+
return $this->mtaService;
63+
}
64+
8165
public function getResource(string $resourceName): MtaResource
8266
{
8367
$resource = $this->resources->findByName($resourceName);
8468

8569
if (empty($resource)) {
86-
$resource = new MtaResource($resourceName, $this);
70+
$resource = new MtaResource($resourceName, $this->mtaService);
8771
$this->resources->add($resource);
8872
}
8973

@@ -103,38 +87,8 @@ public static function doReturn(...$arguments): void
10387
echo ElementTransformer::toServer($arguments);
10488
}
10589

106-
public function callFunction(string $resourceName, string $functionName, array $arguments = null): ?array
107-
{
108-
$requestData = $arguments ? ElementTransformer::toServer($arguments) : '';
109-
$path = sprintf('%s/call/%s', $resourceName, $functionName);
110-
111-
$responseBody = $this->executeRequest($path, $requestData);
112-
$convertedResponse = ElementTransformer::fromServer($responseBody);
113-
114-
return $convertedResponse ?? null;
115-
}
116-
117-
public function __get(string $name): MtaResource
118-
{
119-
return $this->getResource($name);
120-
}
121-
122-
protected function executeRequest(string $path, string $body): string
90+
public function __get(string $resourceName): MtaResource
12391
{
124-
$request = $this->requestFactory->createRequest(
125-
'POST',
126-
sprintf('%s/%s', $this->server->getBaseUri(), $path)
127-
);
128-
129-
$streamBody = $this->streamFactory->createStream($body);
130-
$request->withBody($streamBody);
131-
132-
$auth = new BasicAuth($this->auth->getUser(), $this->auth->getPassword());
133-
$request = $auth->authenticate($request);
134-
135-
$response = $this->httpClient->sendRequest($request);
136-
HttpStatusVerification::validateStatus($response);
137-
138-
return HandleResponse::getBody($response);
92+
return $this->getResource($resourceName);
13993
}
14094
}

src/Response/HttpStatusValidator.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/*****************************************************************************
4+
*
5+
* PROJECT: MTA PHP SDK
6+
* LICENSE: See LICENSE in the top level directory
7+
* FILE: HttpStatusVerification.php
8+
*
9+
* Multi Theft Auto is available from http://www.multitheftauto.com/
10+
*
11+
*****************************************************************************/
12+
13+
declare(strict_types=1);
14+
15+
namespace MultiTheftAuto\Sdk\Response;
16+
17+
use Exception;
18+
use MultiTheftAuto\Sdk\Exception\AccessDeniedException;
19+
use MultiTheftAuto\Sdk\Exception\FunctionNotFoundException;
20+
use MultiTheftAuto\Sdk\Exception\NotFoundStatusException;
21+
use Psr\Http\Message\ResponseInterface;
22+
23+
class HttpStatusValidator
24+
{
25+
/**
26+
* @var ResponseInterface
27+
*/
28+
protected $response;
29+
30+
protected const ERROR_NOT_FOUND_BODY = 'error: not found';
31+
protected const SOMETHING_WENT_WRONG = 'Something went wrong. HTTP Status Code: %s | Body: %s';
32+
33+
public function __construct(ResponseInterface $response)
34+
{
35+
$this->response = $response;
36+
}
37+
38+
/**
39+
* @throws Exception
40+
*/
41+
public function validate(): void
42+
{
43+
$statusCode = $this->response->getStatusCode();
44+
45+
switch ($statusCode) {
46+
case 401:
47+
{
48+
throw new AccessDeniedException();
49+
break;
50+
}
51+
case 404:
52+
{
53+
throw new NotFoundStatusException();
54+
break;
55+
}
56+
case 200:
57+
{
58+
if (HandleResponse::getBody($this->response) == self::ERROR_NOT_FOUND_BODY) {
59+
throw new FunctionNotFoundException();
60+
}
61+
break;
62+
}
63+
default:
64+
{
65+
throw new Exception(
66+
sprintf(
67+
self::SOMETHING_WENT_WRONG,
68+
$statusCode,
69+
HandleResponse::getBody($this->response)
70+
)
71+
);
72+
}
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)