forked from eellak/gredu_labs
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic implementation; fetch unit equipment
- Loading branch information
Showing
15 changed files
with
723 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* gredu_labs | ||
* | ||
* @link https://github.com/eellak/gredu_labs for the canonical source repository | ||
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/) | ||
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html | ||
*/ | ||
|
||
namespace GrEduLabs\Inventory; | ||
|
||
class Equipment | ||
{ | ||
protected $id; | ||
|
||
protected $category; | ||
|
||
protected $description; | ||
|
||
protected $location; | ||
|
||
protected $manufacturer; | ||
|
||
protected $propertyNumber; | ||
|
||
public function __construct($id, $category, $description, $location, $manufacturer, $propertyNumber) | ||
{ | ||
$this->id = $id; | ||
$this->category = $category; | ||
$this->description = $description; | ||
$this->location = $location; | ||
$this->manufacturer = $manufacturer; | ||
$this->propertyNumber = $propertyNumber; | ||
} | ||
|
||
public function __get($name) | ||
{ | ||
if (property_exists($this, $name)) { | ||
return $this->{$name}; | ||
} | ||
|
||
return; | ||
} | ||
|
||
public function toArray() | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'category' => $this->category, | ||
'description' => $this->description, | ||
'location' => $this->location, | ||
'manufacturer' => $this->manufacturer, | ||
'propertyNumber' => $this->propertyNumber, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* gredu_labs | ||
* | ||
* @link https://github.com/eellak/gredu_labs for the canonical source repository | ||
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/) | ||
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html | ||
*/ | ||
|
||
namespace GrEduLabs\Inventory; | ||
|
||
use CallbackFilterIterator; | ||
use GrEduLabs\Stdlib\ImmutableArrayObject; | ||
use InvalidArgumentException; | ||
use Traversable; | ||
|
||
class EquipmentCollection extends ImmutableArrayObject | ||
{ | ||
/** | ||
* Collection constructor | ||
* | ||
* @param array|Traversable | ||
*/ | ||
public function __construct($equipmentObjects) | ||
{ | ||
if ($equipmentObjects instanceof Traversable) { | ||
$equipmentObjects = iterator_to_array($equipmentObjects); | ||
} | ||
|
||
$previousHandler = set_error_handler(['self', 'handleErrors']); | ||
parent::__construct(array_map(function (Equipment $equipment) { | ||
return $equipment; | ||
}, $equipmentObjects)); | ||
set_error_handler($previousHandler); | ||
} | ||
|
||
/** | ||
* Returns a new Equipment collection with equimpment matching the given location | ||
* | ||
* @param string $location | ||
* @retun EquipmentCollection | ||
*/ | ||
public function withLocation($location) | ||
{ | ||
return new self(new CallbackFilterIterator($this->getIterator(), function (Equipment $equipment) use ($location) { | ||
return $equipment->location === $location; | ||
})); | ||
} | ||
|
||
/** | ||
* Returns a new Equipment collection with equimpment matching the given category | ||
* | ||
* @param string $category | ||
* @retun EquipmentCollection | ||
*/ | ||
public function withCategory($category) | ||
{ | ||
return new self(new CallbackFilterIterator($this->getIterator(), function (Equipment $equipment) use ($category) { | ||
return $equipment->category === $category; | ||
})); | ||
} | ||
|
||
private static function handleErrors($severity, $message, $file, $line) | ||
{ | ||
throw new InvalidArgumentException($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
/** | ||
* gredu_labs | ||
* | ||
* @link https://github.com/eellak/gredu_labs for the canonical source repository | ||
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/) | ||
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html | ||
*/ | ||
|
||
namespace GrEduLabs\Inventory; | ||
|
||
use GuzzleHttp\ClientInterface; | ||
|
||
/** | ||
* Inventory service implementation using GuzzleHttp library | ||
*/ | ||
class GuzzleHttpService implements ServiceInterface | ||
{ | ||
/** | ||
* @var ClientInterface | ||
*/ | ||
protected $httpClient; | ||
|
||
/** | ||
* Class constructor | ||
* | ||
* @param ClientInterface $httpClient | ||
*/ | ||
public function __construct(ClientInterface $httpClient) | ||
{ | ||
$this->httpClient = $httpClient; | ||
} | ||
|
||
/** | ||
* Retrieves all equipment data for unit | ||
* | ||
* @param mixed $unit | ||
* @return EquipmentCollection | ||
*/ | ||
public function getUnitEquipment($unit) | ||
{ | ||
$response = $this->httpClient->request('GET', $this->createBaseUri($unit)); | ||
|
||
$responseData = json_decode($response->getBody()->getContents(), true); | ||
|
||
return new EquipmentCollection( | ||
array_map([$this, 'hydrateEquipment'], $responseData['flat_results']) | ||
); | ||
} | ||
|
||
/** | ||
* Creates the uri with the unit query parameter | ||
* | ||
* @param mixed $unit | ||
* @return Psr\Http\Message\UriInterface | ||
*/ | ||
private function createBaseUri($unit) | ||
{ | ||
$config = $this->httpClient->getConfig(); | ||
$baseUri = $config['base_uri']; | ||
|
||
return $baseUri->withQueryValue($baseUri, 'unit', $unit); | ||
} | ||
|
||
private function hydrateEquipment(array $data) | ||
{ | ||
return new Equipment( | ||
(isset($data['id']) ? $data['id'] : null), | ||
(isset($data['item_template.category.name']) ? $data['item_template.category.name'] : null), | ||
(isset($data['item_template.description']) ? $data['item_template.description'] : null), | ||
(isset($data['location.name']) ? $data['location.name'] : null), | ||
(isset($data['item_template.manufacturer.name']) ? $data['item_template.manufacturer.name'] : null), | ||
(isset($data['property_number']) ? $data['property_number'] : null) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
/** | ||
* gredu_labs | ||
* | ||
* @link https://github.com/eellak/gredu_labs for the canonical source repository | ||
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/) | ||
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html | ||
*/ | ||
|
||
namespace GrEduLabs\Inventory; | ||
|
||
/** | ||
* Inventory service interface | ||
*/ | ||
interface ServiceInterface | ||
{ | ||
/** | ||
* Retrieves all equipment data for unit | ||
* | ||
* @param mixed $unit | ||
* @return EquipmentCollection | ||
*/ | ||
public function getUnitEquipment($unit); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/** | ||
* gredu_labs | ||
* | ||
* @link https://github.com/eellak/gredu_labs for the canonical source repository | ||
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/) | ||
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html | ||
*/ | ||
|
||
namespace GrEduLabs\Stdlib; | ||
|
||
use ArrayObject; | ||
use LogicException; | ||
|
||
class ImmutableArrayObject extends ArrayObject | ||
{ | ||
|
||
public function append($value) | ||
{ | ||
throw new LogicException('Attempting to write to an immutable array'); | ||
} | ||
|
||
public function exchangeArray($input) | ||
{ | ||
throw new LogicException('Attempting to write to an immutable array'); | ||
} | ||
|
||
public function offsetSet($index, $newval) | ||
{ | ||
throw new LogicException('Attempting to write to an immutable array'); | ||
} | ||
|
||
public function offsetUnset($index) | ||
{ | ||
throw new LogicException('Attempting to write to an immutable array'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.