Skip to content

Commit

Permalink
Major overhaul
Browse files Browse the repository at this point in the history
- Objects and arrays are now "null" by default
- No longer attempting to create Query or WriteMeta objects on error
- Lots of fixes in PSR-7 objects
- Minor documentation updates
  • Loading branch information
dcarbone committed Mar 28, 2017
1 parent e45bdf8 commit cc98a77
Show file tree
Hide file tree
Showing 40 changed files with 555 additions and 537 deletions.
40 changes: 26 additions & 14 deletions src/ACL/ACLClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ public function create(ACLEntry $acl, WriteOptions $writeOptions = null)

/** @var \Psr\Http\Message\ResponseInterface $response */
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
$wm = $this->buildWriteMeta($duration);

if (null !== $err)
return [null, $wm, $err];
return [null, $err];

$wm = $this->buildWriteMeta($duration);

list($data, $err) = $this->decodeBody($response->getBody());
if (null !== $err)
return ['', $wm, $err];

return [$data, $wm, $err];
return [$data, $wm, null];
}

/**
Expand All @@ -68,7 +70,10 @@ public function update(ACLEntry $acl, WriteOptions $writeOptions = null)

list($duration, $_, $err) = $this->requireOK($this->doRequest($r));

return [$this->buildWriteMeta($duration), $err];
if (null !== $err)
return [null, $err];

return [$this->buildWriteMeta($duration), null];
}

/**
Expand All @@ -86,7 +91,10 @@ public function destroy($id, WriteOptions $writeOptions = null)

list($duration, $_, $err) = $this->requireOK($this->doRequest($r));

return [$this->buildWriteMeta($duration), $err];
if (null !== $err)
return [null, $err];

return [$this->buildWriteMeta($duration), null];
}

/**
Expand All @@ -105,14 +113,16 @@ public function cloneACL($id, WriteOptions $writeOptions = null)

/** @var \Psr\Http\Message\ResponseInterface $response */
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
$wm = $this->buildWriteMeta($duration);

if (null !== $err)
return [null, $wm, $err];
return [null, null, $err];

$wm = $this->buildWriteMeta($duration);

list($data, $err) = $this->decodeBody($response->getBody());
if (null !== $err)
return ['', $wm, $err];

return [$data, $wm, $err];
return [$data, $wm, null];
}

/**
Expand All @@ -131,9 +141,10 @@ public function info($id, QueryOptions $queryOptions = null)

/** @var \Psr\Http\Message\ResponseInterface $response */
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
$qm = $this->buildQueryMeta($duration, $response, $r->getUri());
if (null !== $err)
return [null, $qm, $err];
return [null, null, $err];

$qm = $this->buildQueryMeta($duration, $response, $r->getUri());

list($data, $err) = $this->decodeBody($response->getBody());
if (null !== $err)
Expand Down Expand Up @@ -163,10 +174,11 @@ public function listACLs(QueryOptions $queryOptions = null)

/** @var \Psr\Http\Message\ResponseInterface $response */
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
if (null !== $err)
return [null, null, $err];

$qm = $this->buildQueryMeta($duration, $response, $r->getUri());

if (null !== $err)
return [null, $qm, $err];

list($data, $err) = $this->decodeBody($response->getBody());
if (null !== $err)
Expand Down
6 changes: 3 additions & 3 deletions src/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
abstract class AbstractCollection implements \JsonSerializable, \ArrayAccess, \Iterator, \Countable
{
/** @var array */
protected $_storage = array();
protected $_storage = [];

/**
* AbstractResponseModel constructor.
* @param array $data
*/
public function __construct(array $data = array())
public function __construct(array $data = [])
{
foreach($data as $k => $v)
{
Expand Down Expand Up @@ -227,7 +227,7 @@ protected function _triggerOutOfBoundsError($key, $level = E_USER_NOTICE)
*/
protected function _findKeyMatches($key)
{
$possibleMatches = array();
$possibleMatches = [];
if (is_string($key))
{
$regex = sprintf('{^.*%s.*$}i', substr($key, 0, 2));
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ abstract class AbstractModel implements \JsonSerializable
*
* @param array $data
*/
public function __construct(array $data = array())
public function __construct(array $data = [])
{
foreach($data as $k => $v)
{
Expand Down
4 changes: 2 additions & 2 deletions src/AbstractOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
abstract class AbstractOptions extends AbstractCollection
{
/** @var array */
protected $_definition = array();
protected $_definition = [];

/**
* AbstractConsulConfig constructor.
* @param array $data
*/
public function __construct(array $data = array())
public function __construct(array $data = [])
{
$this->_definition = $this->getDefinition();
parent::__construct($data + $this->_definition);
Expand Down
72 changes: 9 additions & 63 deletions src/Agent/AgentClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public function self()

/** @var \Psr\Http\Message\ResponseInterface $response */
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
$qm = $this->buildQueryMeta($duration, $response, $r->getUri());

if (null !== $err)
return [null, $qm, $err];
return [null, null, $err];

$qm = $this->buildQueryMeta($duration, $response, $r->getUri());

list($data, $err) = $this->decodeBody($response->getBody());

Expand Down Expand Up @@ -95,7 +95,7 @@ public function checks()
if (null !== $err)
return [null, $err];

$checks = array();
$checks = [];
foreach($data as $k => $v)
{
$checks[$k] = new AgentCheck($v);
Expand Down Expand Up @@ -125,7 +125,7 @@ public function services()
if (null !== $err)
return [null, $err];

$services = array();
$services = [];
foreach($data as $k => $v)
{
$services[$k] = new AgentService($v);
Expand Down Expand Up @@ -155,7 +155,7 @@ public function members()
if (null !== $err)
return [null, $err];

$members = array();
$members = [];
foreach($data as $v)
{
$members[] = new AgentMember($v);
Expand All @@ -172,8 +172,7 @@ public function members()
*/
public function serviceRegister(AgentServiceRegistration $agentServiceRegistration)
{
$r = new Request('put', 'v1/agent/service/register', $this->c);
$r->body = ($agentServiceRegistration);
$r = new Request('put', 'v1/agent/service/register', $this->c, $agentServiceRegistration);

list($_, $_, $err) = $this->requireOK($this->doRequest($r));

Expand Down Expand Up @@ -241,8 +240,7 @@ public function failTTL($checkID, $note)
*/
public function updateTTL($checkID, $output, $status)
{
$r = new Request('put', sprintf('v1/agent/check/update/%s', $checkID), $this->c);
$r->body = (new AgentCheckUpdate(['Output' => $output, 'Status' => $status]));
$r = new Request('put', sprintf('v1/agent/check/update/%s', $checkID), $this->c, new AgentCheckUpdate(['Output' => $output, 'Status' => $status]));

list($_, $_, $err) = $this->requireOK($this->doRequest($r));

Expand All @@ -255,8 +253,7 @@ public function updateTTL($checkID, $output, $status)
*/
public function checkRegister(AgentCheckRegistration $agentCheckRegistration)
{
$r = new Request('put', 'v1/agent/check/register', $this->c);
$r->body = ($agentCheckRegistration);
$r = new Request('put', 'v1/agent/check/register', $this->c, $agentCheckRegistration);

list($_, $_, $err) = $this->requireOK($this->doRequest($r));

Expand Down Expand Up @@ -363,57 +360,6 @@ public function disableNodeMaintenance()
return $err;
}

/**
* Set non-ttl check's state to passing with optional note
*
* @param string $checkID
* @param string $note
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function checkPass($checkID, $note = '')
{
$r = new Request('get', sprintf('v1/agent/check/pass/%s', $checkID), $this->c);
$r->params->set('note', $note);

list($_, $_, $err) = $this->requireOK($this->doRequest($r));

return $err;
}

/**
* Set non-ttl check's state to warning with optional note
*
* @param string $checkID
* @param string $note
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function checkWarn($checkID, $note = '')
{
$r = new Request('get', sprintf('v1/agent/check/warn/%s', $checkID), $this->c);
$r->params->set('note', $note);

list($_, $_, $err) = $this->requireOK($this->doRequest($r));

return $err;
}

/**
* Set non-ttl check's state to critical with optional note
*
* @param string $checkID
* @param string $note
* @return \DCarbone\PHPConsulAPI\Error|null
*/
public function checkFail($checkID, $note = '')
{
$r = new Request('get', sprintf('v1/agent/check/fail/%s', $checkID), $this->c);
$r->params->set('note', $note);

list($_, $_, $err) = $this->requireOK($this->doRequest($r));

return $err;
}

/**
* @return \DCarbone\PHPConsulAPI\Error|null
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Agent/AgentSelf.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ class AgentSelf extends AbstractModel
* AgentSelf constructor.
* @param array $data
*/
public function __construct(array $data = array())
public function __construct(array $data = [])
{
parent::__construct($data);
if (!($this->Config instanceof AgentSelfConfig))
if (null !== $this->Config && !($this->Config instanceof AgentSelfConfig))
$this->Config = new AgentSelfConfig((array)$this->Config);
if (!($this->Coord instanceof AgentSelfCoord))
if (null !== $this->Coord && !($this->Coord instanceof AgentSelfCoord))
$this->Coord = new AgentSelfCoord((array)$this->Coord);
if (!($this->Member instanceof AgentMember))
if (null !== $this->Member && !($this->Member instanceof AgentMember))
$this->Member = new AgentMember((array)$this->Member);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Agent/AgentSelfConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class AgentSelfConfig extends AbstractModel
/** @var string */
public $DNSRecursor = '';
/** @var string[] */
public $DNSRecursors = array();
public $DNSRecursors = [];
/** @var string[] */
public $DNSConfig = array();
public $DNSConfig = [];
/** @var string */
public $Domain = '';
/** @var string */
Expand All @@ -55,13 +55,13 @@ class AgentSelfConfig extends AbstractModel
/** @var string */
public $AdvertiseAddr = '';
/** @var string[] */
public $Ports = array();
public $Ports = [];
/** @var bool */
public $LeaveOnTerm = false;
/** @var bool */
public $SkipLeaveOnInt = false;
/** @var string[] */
public $Telemetry = array();
public $Telemetry = [];
/** @var int */
public $Protocol = 0;
/** @var bool */
Expand All @@ -77,7 +77,7 @@ class AgentSelfConfig extends AbstractModel
/** @var string */
public $KeyFile = '';
/** @var string[] */
public $StartJoin = array();
public $StartJoin = [];
/** @var string */
public $UiDir = '';
/** @var string */
Expand Down
2 changes: 1 addition & 1 deletion src/Agent/AgentSelfCoord.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class AgentSelfCoord extends AbstractModel
{
/** @var array */
public $Vec = array();
public $Vec = [];
/** @var float */
public $Error = 0.0;
/** @var float */
Expand Down
21 changes: 13 additions & 8 deletions src/Agent/AgentServiceRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,30 @@ class AgentServiceRegistration extends AbstractModel
/** @var \DCarbone\PHPConsulAPI\Agent\AgentCheck */
public $Check = null;
/** @var \DCarbone\PHPConsulAPI\Agent\AgentCheck[] */
public $Checks = array();
public $Checks = [];

/**
* AgentServiceRegistration constructor.
* @param array $data
*/
public function __construct(array $data = array())
public function __construct(array $data = [])
{
parent::__construct($data);

if (!($this->Check instanceof AgentCheck))
if (null !== $this->Check && !($this->Check instanceof AgentCheck))
$this->Check = new AgentCheck((array)$this->Check);

for ($i = 0, $cnt = count($this->Checks); $i < $cnt; $i++)
if (0 < count($this->Checks))
{
if ($this->Checks[$i] instanceof AgentCheck)
continue;

$this->Checks[$i] = new AgentCheck($this->Checks[$i]);
$this->Checks = array_filter($this->Checks);
if (0 < ($cnt = count($this->Checks)))
{
for ($i = 0, $cnt = count($this->Checks); $i < $cnt; $i++)
{
if (!($this->Checks[$i] instanceof AgentCheck))
$this->Checks[$i] = new AgentCheck($this->Checks[$i]);
}
}
}
}

Expand Down
Loading

0 comments on commit cc98a77

Please sign in to comment.