Skip to content

Commit fbe3b00

Browse files
Alfredo De la calleAlfredo De la calle
Alfredo De la calle
authored and
Alfredo De la calle
committed
Add Offer API
1 parent 69a18eb commit fbe3b00

File tree

6 files changed

+266
-4
lines changed

6 files changed

+266
-4
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

composer.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
"php": "^7.0",
88
"guzzlehttp/guzzle": "^6.1"
99
},
10-
1110
"autoload": {
1211
"psr-4": {
13-
"Bytelovers\\Affiser\\": "src"
12+
"Bytelovers\\Affiser\\": "src/"
1413
}
1514
},
1615
"minimum-stability": "dev"

src/Api/Affiliate/Partner.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Bytelovers\Affise\Api\Affiliate;
4+
5+
use Bytelovers\Affise\Base;
6+
7+
class Partner extends Base {
8+
protected $endpointBase = 'partner';
9+
10+
public function getOfferList($params = []) {
11+
return $this->get($this->endpointBase . '/offers', $params);
12+
}
13+
}

src/Api/Offer/Offer.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: alfredodelacalle
5+
* Date: 22/12/17
6+
* Time: 0:41
7+
*/
8+
9+
namespace Bytelovers\Affise\Api\Offer;
10+
11+
use Bytelovers\Affise\Base;
12+
13+
class Offer extends Base {
14+
protected $endpointBase = 'offer';
15+
16+
public function getEndpointBase(): string {
17+
return $this->endpointBase;
18+
}
19+
20+
private function getEndpointBasePlural(): string {
21+
return $this->endpointBase . 's';
22+
}
23+
24+
public function getOfferList($params = []) {
25+
return $this->get($this->getEndpointBasePlural(), $params);
26+
}
27+
28+
public function getOfferById($id = null) {
29+
if (!$id) {
30+
throw new \Exception('The param ID is required');
31+
}
32+
33+
return $this->get(implode('/', [
34+
$this->getEndpointBase(),
35+
$id
36+
]));
37+
}
38+
39+
public function getOfferCategories($params = []) {
40+
return $this->get(implode('/', [
41+
$this->getEndpointBase(),
42+
'categories'
43+
]), $params);
44+
}
45+
46+
}

src/Base.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Bytelovers\Affise;
4+
5+
class Base {
6+
7+
private $httpClient;
8+
9+
protected $endpointBase;
10+
11+
public function __construct($httpClient) {
12+
$this->setHttpClient($httpClient);
13+
}
14+
15+
public function get($path, $params = []) {
16+
return $this->getHttpClient()->get($path, $params);
17+
}
18+
19+
//TODO: Implement
20+
public function post($path, $params = []) {
21+
throw new \Exception('Method not implemented');
22+
}
23+
24+
/**
25+
* @return mixed
26+
*/
27+
public function getHttpClient() {
28+
return $this->httpClient;
29+
}
30+
31+
/**
32+
* @param mixed $httpClient
33+
*/
34+
public function setHttpClient($httpClient) {
35+
$this->httpClient = $httpClient;
36+
}
37+
38+
}

src/Client.php

+153-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,161 @@
22

33
namespace Bytelovers\Affise;
44

5+
use GuzzleHttp\Client as GuzzleClient;
6+
57
class Client {
6-
private $apiUrl = 'http://';
8+
private $apiUrl = 'http://%s.affise.com/%s/%s';
9+
10+
private $apiVersion = '3.0';
11+
12+
private $apiDomain;
13+
14+
private $apiKey;
15+
16+
private $headers = ['User-Agent' => 'Affise-php-client'];
17+
18+
private $httpClient;
19+
20+
public function url() {
21+
return sprintf($this->apiUrl, $this->apiDomain, $this->apiVersion);
22+
}
23+
24+
public function __construct($apiDomain, $aplKey) {
25+
$this->setApiDomain($apiDomain);
26+
$this->setHeaders(['API-Key' => $aplKey]);
27+
28+
$this->setHttpClient(new GuzzleClient([
29+
['headers' => $this->getHeaders()],
30+
]));
31+
}
32+
33+
public function api($class) {
34+
$class = 'Bytelovers\\Affise\\Api\\' . $class;
35+
return new $class($this);
36+
}
37+
38+
public function get($endpoint, $params = []) {
39+
$req = $this->buildUrl($endpoint, $params);
40+
$req = urldecode($req);
41+
42+
$res = $this->getHttpClient()->get($req);
43+
return $this->handleResponse($res);
44+
}
45+
46+
private function buildUrl($endpoint, $params) {
47+
$url = sprintf($this->apiUrl, $this->getApiDomain(), $this->getApiVersion(), $endpoint);
48+
49+
if ($params) {
50+
$url .= '?' . http_build_query($params);
51+
}
52+
53+
return $url;
54+
}
55+
56+
private function handleResponse($res) {
57+
$statusCode = $res->getStatusCode();
58+
$body = json_decode($res->getbody());
59+
60+
if ($statusCode >= 200 && $statusCode < 300) {
61+
return $body;
62+
}
63+
dd($res->getHeaders());
64+
throw new \Exception($body->message, $statusCode);
65+
}
766

8-
public function __construct() {
67+
/**
68+
* @return string
69+
*/
70+
public function getApiUrl(): string
71+
{
972
return $this->apiUrl;
1073
}
74+
75+
/**
76+
* @param string $apiUrl
77+
*/
78+
public function setApiUrl(string $apiUrl)
79+
{
80+
$this->apiUrl = $apiUrl;
81+
}
82+
83+
/**
84+
* @return string
85+
*/
86+
public function getApiVersion(): string
87+
{
88+
return $this->apiVersion;
89+
}
90+
91+
/**
92+
* @param string $apiVersion
93+
*/
94+
public function setApiVersion(string $apiVersion)
95+
{
96+
$this->apiVersion = $apiVersion;
97+
}
98+
99+
/**
100+
* @return mixed
101+
*/
102+
public function getApiDomain()
103+
{
104+
return $this->apiDomain;
105+
}
106+
107+
/**
108+
* @param mixed $apiDomain
109+
*/
110+
public function setApiDomain($apiDomain)
111+
{
112+
$this->apiDomain = $apiDomain;
113+
}
114+
115+
/**
116+
* @return mixed
117+
*/
118+
public function getApiKey()
119+
{
120+
return $this->apiKey;
121+
}
122+
123+
/**
124+
* @param mixed $apiKey
125+
*/
126+
public function setApiKey($apiKey)
127+
{
128+
$this->apiKey = $apiKey;
129+
}
130+
131+
/**
132+
* @return array
133+
*/
134+
public function getHeaders(): array
135+
{
136+
return $this->headers;
137+
}
138+
139+
/**
140+
* @param array $headers
141+
*/
142+
public function setHeaders(array $headers)
143+
{
144+
$this->headers = $headers;
145+
}
146+
147+
/**
148+
* @return mixed
149+
*/
150+
public function getHttpClient()
151+
{
152+
return $this->httpClient;
153+
}
154+
155+
/**
156+
* @param mixed $httpClient
157+
*/
158+
public function setHttpClient($httpClient)
159+
{
160+
$this->httpClient = $httpClient;
161+
}
11162
}

0 commit comments

Comments
 (0)