Skip to content

Commit

Permalink
Refactoring, updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
karllhughes committed Sep 28, 2015
1 parent 6f4a56c commit f82d37d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 33 deletions.
116 changes: 88 additions & 28 deletions src/Govt.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,65 @@

class Govt extends AbstractProvider
{
/**
* Map of setter methods to query parameters
*
* @var array
*/
protected $queryMap = [
'setCount' => 'size',
'setFrom' => 'from',
'setHl' => 'hl',
'setKeyword' => 'query',
'setLatLon' => 'lat_lon',
'setOrganizationIds' => 'organization_ids',
'setQuery' => 'query',
'setSize' => 'size',
'setTags' => 'tags',
];

/**
* Current api query parameters
*
* @var array
*/
protected $queryParams = [
'from' => null,
'hl' => null,
'lat_lon' => null,
'organization_ids' => null,
'query' => null,
'size' => null,
'tags' => null,
];

/**
* Create new Govt jobs client.
*
* @param array $parameters
*/
public function __construct($parameters = [])
{
parent::__construct($parameters);
array_walk($parameters, [$this, 'updateQuery']);
}

/**
* Magic method to handle get and set methods for properties
*
* @param string $method
* @param array $parameters
*
* @return mixed
*/
public function __call($method, $parameters)
{
if (isset($this->queryMap[$method], $parameters[0])) {
$this->updateQuery($parameters[0], $this->queryMap[$method]);
}
return parent::__call($method, $parameters);
}

/**
* Returns the standardized job object
*
Expand Down Expand Up @@ -103,12 +162,20 @@ public function getFormat()
}

/**
* Get listings path
* Get page
*
* @return string
*/
public function getListingsPath()
public function getFrom()
{
if ($this->page) {
$from = ($this->page - 1) * $this->count;

if ($from) {
return $from;
}
}

return null;
}

Expand All @@ -129,20 +196,12 @@ public function getKeyword()
}

/**
* Get page
* Get listings path
*
* @return string
*/
public function getFrom()
public function getListingsPath()
{
if ($this->page) {
$from = ($this->page - 1) * $this->count;

if ($from) {
return $from;
}
}

return null;
}

Expand All @@ -153,22 +212,7 @@ public function getFrom()
*/
public function getQueryString()
{
$query_params = [
'query' => 'getKeyword',
'from' => 'getFrom',
'size' => 'getCount',
];

$query_string = [];

array_walk($query_params, function ($value, $key) use (&$query_string) {
$computed_value = $this->$value();
if (!is_null($computed_value)) {
$query_string[$key] = $computed_value;
}
});

return http_build_query($query_string);
return http_build_query($this->queryParams);
}

/**
Expand All @@ -191,4 +235,20 @@ public function getVerb()
{
return 'GET';
}

/**
* Attempts to update current query parameters.
*
* @param string $value
* @param string $key
*
* @return Careerbuilder
*/
protected function updateQuery($value, $key)
{
if (array_key_exists($key, $this->queryParams)) {
$this->queryParams[$key] = $value;
}
return $this;
}
}
9 changes: 4 additions & 5 deletions tests/src/GovtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ public function testUrlNotIncludesSizeWhenNotProvided()

public function testUrlIncludesFromWhenProvided()
{
$page = rand(5, 15);
$count = rand(10, 100);
$param = 'from='.(($page - 1) * $count);
$from = rand(10, 100);
$param = 'from='.$from;

$url = $this->client->setPage($page)->setCount($count)->getUrl();
$url = $this->client->setFrom($from)->getUrl();

$this->assertContains($param, $url);
}
Expand All @@ -97,7 +96,7 @@ public function testUrlNotIncludesFromWhenNotProvided()
{
$param = 'from=';

$url = $this->client->setPage(null)->getUrl();
$url = $this->client->setFrom(null)->getUrl();

$this->assertNotContains($param, $url);
}
Expand Down

0 comments on commit f82d37d

Please sign in to comment.