Reputation: 2046
I'm going through a refactoring stage in the application I helped build and I haven't ever run into a similar situation, so I don't quite know if there is a way to simplify this.
The scenario: we connect our internal database to Reverb.com via their API to update our listings' inventory quantity and prices as well as to pull orders into our internal database. Reverb's API requires an authentication token for each call. The token is created by first sending the email and password and receiving the token in the response. We have three classes. The first class is mainly to setup the authentication token. The other two classes are for orders and inventory respectively. Our current setup instantiates separate objects for each class. This creates three different calls to Reverb to create an authentication token. I'm trying to remove this redundancy.
Here's the first class (revclass.php):
<?php
namespace rev;
class reverbclass
{
protected $reverbEmail;
protected $reverbPassword;
protected $reverbAuth;
public function __construct(){
//Retrieve email and password from database
$reverbinfo = $this->getReverbAppId();
$this->reverbEmail = $reverbinfo['reverb_email'];
$this->reverbPassword = $reverbinfo['reverb_pass'];
//Send email and password and receive back authentication token
$request = $this->getAuth($this->reverbEmail, $this->reverbPassword);
$reverbInfo = json_decode($request, true);
$this->reverbAuth = $reverbInfo['token'];
}
}
Here's the second class (revorderclass.php):
<?php
namespace rev;
use rev\reverbclass;
class revorderclass extends reverbclass
{
public function getOrders(){
$url = 'https://reverb.com/api/my/orders/selling/awaiting_shipment.json';
$postString = '';
$headers = array(
"Content-type: application/hal+json",
"X-Auth-Token: $this->reverbAuth"
);
$response = $this->reverbCurl($url, 'GET', $headers, $post_string);
return $response;
}
}
Here's the inventory class (revinventoryclass.php):
<?php
namespace rev;
use rev\reverbclass;
class revinventoryclass extends reverbclass
{
public function getReverbListings($page){
$url = 'https://reverb.com/api/my/listings.json?page=' . $page;
$postString = '';
$headers = array(
"Content-type: application/hal+json",
"X-Auth-Token: $this->reverbAuth"
);
$response = $this->reverbCurl($url, 'GET', $headers, $post_string);
return $response;
}
}
And here's where I instantiate the classes (revclasses.php):
<?php
//Reverb Classes
include_once 'classes/rev/revclass.php';
include_once 'classes/rev/revorderclass.php';
include_once 'classes/rev/revinventoryclass.php';
//Reverb Class Declarations
$reverb = new \rev\reverbclass();
$revorder = new \rev\revorderclass();
$revinventory = new \rev\revinventoryclass();
And here's an example of a call to retrieve orders and then we'll parse them:
<?php
require 'rev/revclasses.php';
$request = $revorder->getOrders();
I only included the code I thought was critical in the question so as to not muddy the waters more. Again, everything works, but I'm trying to remove the duplicate API authentication calls that happen when all three classes are instantiated as well as make the code more Object Oriented. Right now it feels too procedural. Any tips/corrections/critique is much appreciated!
Upvotes: 0
Views: 59
Reputation: 58444
My recommendation would be to create a separate class, which acts as the API client, which you pass as a dependency in the constructor of all the classes, that need to interact with Reverb API.
$client = new ReverbClient($hostname);
$client->authenticate($apiKey);
$inventory = new Inventorty($client);
$orderRepository = new Orders($client);
And then your getReverbListings()
method you only call:
$listing = $this->client->get('my/listings.json?page=' . $page, $parameters);
The client class is responsible for adding all the headers and turning the responses in some usable arrays.
Upvotes: 2