Skip to content

Commit 0184e72

Browse files
committed
refactor: some design changes
1 parent ee9748b commit 0184e72

17 files changed

+217
-99
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ require_once('vendor/autoload.php');
3434

3535
use MultiTheftAuto\Sdk\Mta;
3636
use MultiTheftAuto\Sdk\Model\Server;
37-
use MultiTheftAuto\Sdk\Authentication\Credential;
37+
use MultiTheftAuto\Sdk\Model\Authentication;
3838

3939
$server = new Server('127.0.0.1', 22005);
40-
$credential = new Credential('myUser', 'myPassword');
41-
$mta = new Mta($server, $credential);
40+
$auth = new Authentication('myUser', 'myPassword');
41+
$mta = new Mta($server, $auth);
4242

4343
$response = $mta->getResource('someResource')->call('callableFunction', $arg1, $arg2, $arg3, ...);
4444
//or

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
"require": {
99
"php": "^7.1",
1010
"ext-json": "^1.5",
11+
"php-http/client-implementation": "^1.0",
1112
"php-http/httplug": "^2.0",
12-
"php-http/client-implementation": "^1.0"
13+
"psr/http-factory": "^1.0"
1314
},
1415
"require-dev": {
1516
"friendsofphp/php-cs-fixer": "^2.13",
1617
"guzzlehttp/psr7": "^1.5",
18+
"http-interop/http-factory-guzzle": "^1.0",
1719
"php-http/mock-client": "^1.1",
20+
"phpspec/prophecy": "~1.0",
1821
"phpstan/phpstan": "^0.10.7",
1922
"phpunit/phpunit": "^7"
2023
},
@@ -30,8 +33,8 @@
3033
}
3134
},
3235
"scripts": {
33-
"fixer": "php-cs-fixer fix --verbose --show-progress=estimating",
34-
"fixer:check": [
36+
"lint": "php-cs-fixer fix --verbose --show-progress=estimating",
37+
"lint:check": [
3538
"php-cs-fixer fix --dry-run --verbose --show-progress=estimating"
3639
],
3740
"phpunit": "phpunit tests",

examples/ajax/ajax_interface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
use MultiTheftAuto\Sdk\Mta;
4-
use MultiTheftAuto\Sdk\Authentication\CredentialTest;
4+
use MultiTheftAuto\Sdk\Model\Authentication;
55
use MultiTheftAuto\Sdk\Model\Server;
66

77
// =============================
@@ -27,8 +27,8 @@
2727
}
2828

2929
$server = new Server($host, $port);
30-
$credential = new CredentialTest($http_username, $http_password);
31-
$mta = new Mta($server, $credential);
30+
$auth = new Authentication($http_username, $http_password);
31+
$mta = new Mta($server, $auth);
3232

3333
$val = explode(",", $val);
3434
$json_data = json_encode($val);

src/Authentication/Credential.php renamed to src/Model/Authentication.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
declare(strict_types=1);
1414

15-
namespace MultiTheftAuto\Sdk\Authentication;
15+
namespace MultiTheftAuto\Sdk\Model;
1616

17-
class Credential
17+
class Authentication
1818
{
1919
/**
2020
* @var string

src/Model/Element.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,36 @@
1414

1515
namespace MultiTheftAuto\Sdk\Model;
1616

17-
class Element
17+
use JsonSerializable;
18+
19+
class Element implements JsonSerializable
1820
{
21+
/**
22+
* @var string
23+
*/
1924
protected $id;
2025

26+
public const SERVER_PREFIX = '^E^';
27+
2128
public function __construct(string $id)
2229
{
2330
$this->id = $id;
2431
}
2532

33+
public static function fromServer(string $value): self
34+
{
35+
$id = substr($value, 3);
36+
37+
return new static($id);
38+
}
39+
2640
public function getId(): string
2741
{
2842
return $this->id;
2943
}
3044

31-
public function __toString()
45+
public function jsonSerialize(): string
3246
{
33-
return '^E^' . $this->id;
47+
return self::SERVER_PREFIX . $this->id;
3448
}
3549
}

src/Model/Resource.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
namespace MultiTheftAuto\Sdk\Model;
1616

1717
use Exception;
18+
use JsonSerializable;
1819
use MultiTheftAuto\Sdk\Mta;
1920

20-
class Resource
21+
class Resource implements JsonSerializable
2122
{
2223
/**
2324
* @var string
@@ -29,12 +30,20 @@ class Resource
2930
*/
3031
private $mta;
3132

33+
public const SERVER_PREFIX = '^R^';
34+
3235
public function __construct(string $name, Mta $mta = null)
3336
{
3437
$this->name = $name;
3538
$this->mta = $mta;
3639
}
3740

41+
public static function fromServer(string $value): self
42+
{
43+
$name = substr($value, 3);
44+
return new static($name);
45+
}
46+
3847
public function getName()
3948
{
4049
return $this->name;
@@ -55,8 +64,8 @@ public function __call($name, $arguments)
5564
return call_user_func_array([$this, 'call'], $arguments);
5665
}
5766

58-
public function __toString()
67+
public function jsonSerialize(): string
5968
{
60-
return '^R^' . $this->name;
69+
return self::SERVER_PREFIX . $this->name;
6170
}
6271
}

src/Model/Server.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function __construct(string $host, int $httpPort)
3333
if (!filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
3434
throw new InvalidArgumentException('Invalid IP');
3535
}
36+
3637
$this->host = $host;
3738
$this->httpPort = $httpPort;
3839
}

src/Mta.php

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@
1414

1515
namespace MultiTheftAuto\Sdk;
1616

17+
use GuzzleHttp\Psr7\Stream;
1718
use Http\Client\HttpClient;
1819
use Http\Discovery\HttpClientDiscovery;
19-
use Http\Discovery\MessageFactoryDiscovery;
20+
use Http\Discovery\Psr17FactoryDiscovery;
2021
use Http\Message\Authentication\BasicAuth;
21-
use Http\Message\MessageFactory;
22-
use MultiTheftAuto\Sdk\Authentication\Credential;
22+
use MultiTheftAuto\Sdk\Model\Authentication;
2323
use MultiTheftAuto\Sdk\Model\Resource as MtaResource;
2424
use MultiTheftAuto\Sdk\Model\Resources;
2525
use MultiTheftAuto\Sdk\Model\Server;
2626
use MultiTheftAuto\Sdk\Response\HandleResponse;
2727
use MultiTheftAuto\Sdk\Response\HttpStatusVerification;
28+
use MultiTheftAuto\Sdk\Utils\ElementTransformer;
2829
use MultiTheftAuto\Sdk\Utils\Input;
29-
use MultiTheftAuto\Sdk\Utils\Translator;
30+
use Psr\Http\Message\RequestFactoryInterface;
31+
use Psr\Http\Message\StreamFactoryInterface;
3032

3133
class Mta
3234
{
@@ -36,9 +38,9 @@ class Mta
3638
protected $server;
3739

3840
/**
39-
* @var Credential
41+
* @var Authentication
4042
*/
41-
protected $credential;
43+
protected $auth;
4244

4345
/**
4446
* @var Resources
@@ -51,20 +53,27 @@ class Mta
5153
protected $httpClient;
5254

5355
/**
54-
* @var MessageFactory
56+
* @var RequestFactoryInterface
5557
*/
56-
protected $messageFactory;
58+
protected $requestFactory;
59+
60+
/**
61+
* @var StreamFactoryInterface
62+
*/
63+
protected $streamFactory;
5764

5865
public function __construct(
5966
Server $server,
60-
Credential $credential,
67+
Authentication $auth,
6168
HttpClient $httpClient = null,
62-
MessageFactory $messageFactory = null
69+
RequestFactoryInterface $requestFactory = null,
70+
StreamFactoryInterface $streamFactory = null
6371
) {
6472
$this->server = $server;
65-
$this->credential = $credential;
66-
$this->httpClient = $httpClient?? HttpClientDiscovery::find();
67-
$this->messageFactory = $messageFactory?? MessageFactoryDiscovery::find();
73+
$this->auth = $auth;
74+
$this->httpClient = $httpClient ?? HttpClientDiscovery::find();
75+
$this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory();
76+
$this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory();
6877

6978
$this->resources = new Resources();
7079
}
@@ -86,33 +95,41 @@ public function getResource(string $resourceName): MtaResource
8695
*/
8796
public static function getInput(): ?array
8897
{
89-
return Translator::fromServer(Input::get())?? null;
98+
return ElementTransformer::fromServer(Input::get()) ?? null;
9099
}
91100

92101
public static function doReturn(...$arguments): void
93102
{
94-
echo Translator::toServer($arguments);
103+
echo ElementTransformer::toServer($arguments);
95104
}
96105

97-
public function callFunction(string $resourceName, string $function, array $arguments = null): ?array
106+
public function callFunction(string $resourceName, string $functionName, array $arguments = null): ?array
98107
{
99-
$json_output = $arguments? Translator::toServer($arguments) : '';
100-
$path = sprintf('%s/call/%s', $resourceName, $function);
101-
$result = $this->do_post_request($path, $json_output);
102-
$out = Translator::fromServer($result);
108+
$requestData = $arguments ? ElementTransformer::toServer($arguments) : '';
109+
$path = sprintf('%s/call/%s', $resourceName, $functionName);
103110

104-
return $out?? null;
111+
$responseBody = $this->executeRequest($path, $requestData);
112+
$convertedResponse = ElementTransformer::fromServer($responseBody);
113+
114+
return $convertedResponse ?? null;
105115
}
106116

107-
public function __get($name)
117+
public function __get(string $name): MtaResource
108118
{
109119
return $this->getResource($name);
110120
}
111121

112-
protected function do_post_request($path, $json_data): string
122+
protected function executeRequest(string $path, string $body): string
113123
{
114-
$request = $this->messageFactory->createRequest('POST', sprintf('%s/%s', $this->server->getBaseUri(), $path), [], $json_data);
115-
$auth = new BasicAuth($this->credential->getUser(), $this->credential->getPassword());
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());
116133
$request = $auth->authenticate($request);
117134

118135
$response = $this->httpClient->sendRequest($request);

src/Response/HttpStatusVerification.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,27 @@ class HttpStatusVerification
2323

2424
public static function validateStatus(ResponseInterface $response): void
2525
{
26-
if (HandleResponse::getBody($response) == self::ERROR_NOT_FOUND) {
27-
throw new Exception('Attempted function call was not found');
28-
}
29-
3026
$statusCode = $response->getStatusCode();
3127

3228
switch ($statusCode) {
3329
case 401:
34-
throw new Exception('Access Denied. This server requires authentication. Please ensure that a valid username and password combination is provided.');
35-
break;
30+
{
31+
throw new Exception('Access Denied. This server requires authentication. Please ensure that a valid username and password combination is provided.');
32+
break;
33+
}
3634
case 404:
37-
throw new Exception('There was a problem with the request. Ensure that the resource exists and that the name is spelled correctly.');
38-
break;
35+
{
36+
throw new Exception('There was a problem with the request. Ensure that the resource exists and that the name is spelled correctly.');
37+
break;
38+
}
3939
}
4040

4141
if ($statusCode != 200) {
4242
throw new Exception(sprintf('Something went wrong. HTTP Status Code: %s | Body: %s', $statusCode, HandleResponse::getBody($response)));
4343
}
44+
45+
if (HandleResponse::getBody($response) == self::ERROR_NOT_FOUND) {
46+
throw new Exception('Attempted function call was not found');
47+
}
4448
}
4549
}

src/Utils/ElementFactory.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*****************************************************************************
4+
*
5+
* PROJECT: MTA PHP SDK
6+
* LICENSE: See LICENSE in the top level directory
7+
* FILE: ElementFactory.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\Utils;
16+
17+
use MultiTheftAuto\Sdk\Model\Element;
18+
use MultiTheftAuto\Sdk\Model\Resource;
19+
20+
class ElementFactory
21+
{
22+
public static function fromServer(string $value)
23+
{
24+
$valuePrefix = substr($value, 0, 3);
25+
26+
switch ($valuePrefix) {
27+
case Element::SERVER_PREFIX: {
28+
return Element::fromServer($value);
29+
}
30+
case Resource::SERVER_PREFIX: {
31+
return Resource::fromServer($value);
32+
}
33+
}
34+
35+
return $value;
36+
}
37+
}

0 commit comments

Comments
 (0)