Skip to content

Commit 5679857

Browse files
author
Artem Shteltser
committed
🚧 Rethinking structure of the lib
1 parent ff2ed65 commit 5679857

File tree

11 files changed

+164
-235
lines changed

11 files changed

+164
-235
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/vendor/
2+
index.php

‎composer.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@
1717
"require": {
1818
"ext-json": "*",
1919
"guzzlehttp/guzzle": "^7.0"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"PowerLink\\": "src"
24+
}
2025
}
2126
}

‎src/PowerLinkAPI.php‎ renamed to ‎src/Client.php‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,12 @@
44

55
use GuzzleHttp\Client as HTTPClient;
66

7-
abstract class Client
7+
class Client
88
{
99
/**
1010
* Host
1111
*/
12-
const BASE_URL = 'https://api.powerlink.co.il/api/record/';
13-
14-
/**
15-
* Error messages
16-
*/
17-
const ERROR_INVALID_API_KEY = 'Invalid TokenID.';
18-
19-
/**
20-
* @var string
21-
*/
22-
protected $token_id;
12+
const BASE_URL = 'https://api.powerlink.co.il/api/';
2313

2414
/**
2515
* @var HTTPClient
@@ -31,16 +21,25 @@ abstract class Client
3121
*
3222
* @param string $token_id Token id for PowerLink account
3323
* @param string $base_uri (optional) Base URL for PowerLink API
24+
*
3425
*/
35-
public function __construct($token_id, $base_uri = self::BASE_URL)
26+
public function __construct($token_id, $base_url = self::BASE_URL)
3627
{
37-
$this->token_id = $token_id;
3828
$this->client = new HTTPClient([
39-
'base_uri' => $base_uri,
29+
'base_uri' => $base_url,
4030
'headers' => [
31+
'tokenid' => $token_id,
4132
'User-Agent' => "stelzer/php-powerlink/" . PowerLink::VERSION,
4233
'Accept' => 'application/json'
4334
]
4435
]);
4536
}
37+
38+
/**
39+
* Get Initialized Client
40+
*/
41+
public function getClient()
42+
{
43+
return $this->client;
44+
}
4645
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Powerlink\Exceptions;
4+
5+
/**
6+
* Class PowerLinkException
7+
*/
8+
class PowerLinkException extends \Exception
9+
{
10+
}

‎src/Model/BaseModel.php‎

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

‎src/PowerLink.php‎

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,113 @@
22

33
namespace PowerLink;
44

5+
use PowerLink\Exceptions\PowerLinkException;
6+
7+
use GuzzleHttp\Psr7;
8+
use GuzzleHttp\Exception\RequestException;
9+
510
class PowerLink
611
{
12+
/** @var string Package version */
13+
const VERSION = '1.0.0';
14+
15+
/** @var string The API access token */
16+
protected static $token = null;
17+
718
/**
8-
* @var string
19+
* @param string|null $token Token ID for authorization
20+
* @param Client|null $client Custom Client
21+
* @throws PowerLinkException When no token is provided
922
*/
10-
protected $token_id;
23+
public function __construct($token = null, $client = null)
24+
{
25+
if ($token === null) {
26+
if (self::$token === null) {
27+
$msg = 'No token provided, and none is globally set. ';
28+
$msg .= 'Use PowerLink::setToken, or instantiate the Diffbot class with a $token parameter.';
29+
throw new PowerLinkException($msg);
30+
}
31+
} else {
32+
self::setToken($token);
33+
$this->setHttpClient($client);
34+
}
35+
}
1136

1237
/**
13-
* @var string
38+
* Sets the token for all future new instances
39+
* @param string $token The API access token, as obtained on diffbot.com/dev
40+
* @return void
1441
*/
15-
const VERSION = '1.0.0';
42+
public static function setToken($token)
43+
{
44+
self::validateToken($token);
45+
self::$token = $token;
46+
}
47+
48+
private static function validateToken($token)
49+
{
50+
if (!is_string($token)) {
51+
throw new \InvalidArgumentException('Token is not a string.');
52+
}
53+
54+
if (strlen($token) < 36) {
55+
throw new \InvalidArgumentException('Token "' . $token . '" is too short, and thus invalid.');
56+
}
57+
58+
return true;
59+
}
60+
61+
/**
62+
* Returns the token that has been defined.
63+
* @return null|string
64+
*/
65+
public function getToken()
66+
{
67+
return self::$token;
68+
}
69+
1670

1771
/**
18-
* Constructor for PowerLink.
72+
* Sets the client to be used for querying the API endpoints
1973
*
20-
* @param string $token_id Token ID for authorization
74+
* @param Client|null $client
75+
* @return $this
2176
*/
22-
public function __construct($token_id)
77+
public function setHttpClient(Client $client = null)
2378
{
24-
$this->token_id = $token_id;
79+
if ($client === null) {
80+
$client = new Client(self::$token);
81+
}
82+
83+
$this->client = $client->getClient();
84+
return $this;
2585
}
2686

2787
/**
28-
* @return string
88+
* Returns either the instance of the Guzzle client that has been defined, or null
89+
* @return Client|null
2990
*/
30-
public function getTokenID()
91+
public function getHttpClient()
92+
{
93+
return $this->client;
94+
}
95+
96+
public function query()
3197
{
32-
return $this->token_id;
98+
try {
99+
$response = $this->client->post('/query', [
100+
'json' => [
101+
'objecttype' => 'asdsdasdfgasddf',
102+
'page_size' => 50,
103+
'page_number' => 1,
104+
'fields' => '*',
105+
]
106+
]);
107+
} catch (RequestException $e) {
108+
echo Psr7\Message::toString($e->getRequest());
109+
if ($e->hasResponse()) {
110+
echo Psr7\Message::toString($e->getResponse());
111+
}
112+
}
33113
}
34114
}
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,52 @@
11
<?php
22

3-
namespace PowerLink\Model;
3+
namespace PowerLink;
44

55
/**
66
* Class defining a query request in PowerLink with methods to set
77
* query settings
88
*/
9-
class QueryModel
9+
class Query
1010
{
11-
public const ASC = 'asc';
12-
public const DESC = 'DESC';
13-
1411
/**
1512
* Object Type
1613
* @var string
1714
*/
18-
public $object_type;
15+
protected $object_type;
1916

2017
/**
2118
* Number of items returned per page
2219
* @var string
2320
*/
24-
public $page_size;
21+
protected $page_size;
2522

2623
/**
2724
* Number of requested page
2825
* @var string
2926
*/
30-
public $page_number;
27+
protected $page_number;
3128

3229
/**
3330
* Array of requested fields
3431
* @var array
3532
*/
36-
public $fields;
33+
protected $fields;
3734

3835
/**
3936
* SQL query for selecting ites
4037
* @var string
4138
*/
42-
public $query;
39+
protected $query;
4340

4441
/**
4542
* Name of field to sort by
4643
* @var string
4744
*/
48-
public $sort_by;
45+
protected $sort_by;
4946

5047
/**
5148
* Name of field to sort by
5249
* @var ASC|DESC
5350
*/
54-
public $sort_type;
51+
protected $sort_type;
5552
}

‎src/Query/Syntax/OrderBy.php‎ renamed to ‎src/Query/OrderBy.php‎

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

3-
namespace PowerLink\Query\Syntax;
3+
namespace PowerLink\Query;
44

55
/**
66
* Class OrderBy.
@@ -11,7 +11,7 @@ class OrderBy
1111
const DESC = 'desc';
1212

1313
/**
14-
* @var Field
14+
* @var string
1515
*/
1616
protected $field;
1717

@@ -20,14 +20,9 @@ class OrderBy
2020
*/
2121
protected $direction;
2222

23-
/**
24-
* @var bool
25-
*/
26-
protected $useAlias;
27-
2823
/**
2924
* @param string $field
30-
* @param string $direction
25+
* @param ASC|DESC|string $direction
3126
*/
3227
public function __construct(string $field, $direction)
3328
{
@@ -44,7 +39,7 @@ public function getField()
4439
}
4540

4641
/**
47-
* @param Field $field
42+
* @param string $field
4843
*
4944
* @return $this
5045
*/

‎src/Query/Syntax/Field.php‎

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

0 commit comments

Comments
 (0)