basic implementation; fetch unit equipment

parent ae1c00ce
......@@ -97,7 +97,6 @@ $container['authentication_cas_adapter'] = function ($c) {
return new GrEduLabs\Authentication\Adapter\Cas($settings['phpcas']);
};
$container['authentication_storage'] = function ($c) {
return new \GrEduLabs\Authentication\Storage\PhpSession();
};
......@@ -139,6 +138,16 @@ $container['maybe_identity'] = function ($c) {
};
};
// Inventory service
$container['inventory_service'] = function ($c) {
$settings = $c->get('settings');
return new GrEduLabs\Inventory\GuzzleHttpService(
new GuzzleHttp\Client($settings['inventory'])
);
};
// Actions
$container['GrEduLabs\\Action\\User\\Login'] = function ($c) {
......
......@@ -18,6 +18,8 @@ $app->get('/', function ($request, $response, $args) {
'user' => $identity('uid'),
]);
var_dump($this->get('inventory_service')->getUnitEquipment('0551040'));
return $response;
})->setName('index');
......
<?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,
];
}
}
<?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);
}
}
<?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)
);
}
}
<?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);
}
<?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 diff is collapsed.
<?php return [
'inventory' => [
'base_uri' => '',
],
];
\ No newline at end of file
<?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 GrEduLabsTest\Inventory;
use ArrayObject;
use GrEduLabs\Inventory\Equipment;
use GrEduLabs\Inventory\EquipmentCollection;
class EquipmentCollectionTest extends \PHPUnit_Framework_TestCase
{
private $collection;
private $collectionData;
protected function setup()
{
$this->collectionData = [
new Equipment(
82225,
'RACK',
'EASYNET 15U W/DOOR',
'ΕΡΓΑΣΤΗΡΙΟ ΠΛΗΡ/ΚΗΣ 1',
'VERO ELEC',
'ΚΤ-23243'
),
new Equipment(
82182,
'MODEM / ROUTER ',
'CISCO 876',
'ΑΠΟΘΗΚΗ',
'CISCO SYSTEMS',
'ΚΤ-23203'
),
new Equipment(
98487,
'ΟΘΟΝΗ',
'SUN MICROSYSTEMS INC X7202A',
'ΔΙΟΙΚΗΣΗ',
'SUN MICROSYSTEMS INC.',
'ΚΤ-27160'
),
new Equipment(
98787,
'WORKSTATION',
'GENERIC WORKSTATION',
'ΑΠΟΘΗΚΗ',
'GENERIC',
'ΚΤ-27172'
),
];
$this->collection = new EquipmentCollection($this->collectionData);
}
public function testConstructorThrowsIfArgNotContainsEquipmentObject()
{
$this->setExpectedException('InvalidArgumentException');
new EquipmentCollection(['test', [], new \stdClass()]);
}
public function testConsturctorAcceptsTraversable()
{
$collection = new EquipmentCollection(new ArrayObject($this->collectionData));
$this->assertEquals($collection, $this->collection);
}
public function testWithLocationMethodReturnsANewCollection()
{
$collection = $this->collection->withLocation('ΔΙΟΙΚΗΣΗ');
$this->assertNotSame($this->collection, $collection);
}
public function testWithLocationMethodReturnsACollectionWithGivenLocation()
{
$collection = $this->collection->withLocation('ΔΙΟΙΚΗΣΗ');
$data = array_values($collection->getArrayCopy());
$this->assertEquals($data, [$this->collectionData[2]]);
}
public function testWithCategoryMethodReturnsANewCollection()
{
$collection = $this->collection->withCategory('WORKSTATION');
$this->assertNotSame($this->collection, $collection);
}
public function testWithCategoryMethodReturnsACollectionWithGivenCategory()
{
$collection = $this->collection->withCategory('WORKSTATION');
$data = array_values($collection->getArrayCopy());
$this->assertEquals($data, [$this->collectionData[3]]);
}
}
<?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 GrEduLabsTest\Inventory;
use GrEduLabs\Inventory\Equipment;
class EquipmentTest extends \PHPUnit_Framework_TestCase
{
private $equipment;
private $equipmentData = [
'id' => 82225,
'category' => 'RACK',
'description' => 'EASYNET 15U W/DOOR',
'location' => 'ΕΡΓΑΣΤΗΡΙΟ ΠΛΗΡ/ΚΗΣ 1',
'manufacturer' => 'VERO ELEC',
'propertyNumber' => 'ΚΤ-23243',
];
protected function setup()
{
$this->equipment = new Equipment(
$this->equipmentData['id'],
$this->equipmentData['category'],
$this->equipmentData['description'],
$this->equipmentData['location'],
$this->equipmentData['manufacturer'],
$this->equipmentData['propertyNumber']
);
}
public function testConstructorSetProperties()
{
$this->assertAttributeSame($this->equipmentData['id'], 'id', $this->equipment);
$this->assertAttributeSame($this->equipmentData['category'], 'category', $this->equipment);
$this->assertAttributeSame($this->equipmentData['description'], 'description', $this->equipment);
$this->assertAttributeSame($this->equipmentData['location'], 'location', $this->equipment);
$this->assertAttributeSame($this->equipmentData['manufacturer'], 'manufacturer', $this->equipment);
$this->assertAttributeSame($this->equipmentData['propertyNumber'], 'propertyNumber', $this->equipment);
}
public function testMagicGetMethod()
{
$this->assertSame($this->equipmentData['id'], $this->equipment->id);
$this->assertSame($this->equipmentData['category'], $this->equipment->category);
$this->assertSame($this->equipmentData['description'], $this->equipment->description);
$this->assertSame($this->equipmentData['location'], $this->equipment->location);
$this->assertSame($this->equipmentData['manufacturer'], $this->equipment->manufacturer);
$this->assertSame($this->equipmentData['propertyNumber'], $this->equipment->propertyNumber);
}
public function testMagicGetMethodReturnsNullIfNoPropertyExists()
{
$this->assertNull($this->equipment->someNotExistingProperty);
}
public function testToArrayMethod()
{
$this->assertEquals($this->equipmentData, $this->equipment->toArray());
}
}
<?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 GrEduLabsTest\Inventory;
use GrEduLabs\Inventory\GuzzleHttpService;
use GuzzleHttp\Psr7\Uri;
class GuzzleHttpServiceTest extends \PHPUnit_Framework_TestCase
{
private $guzzleClient;
private $service;
protected function setup()
{
$responseBody = $this->getMock('Psr\\Http\\Message\\StreamInterface');
$responseBody->expects($this->any())
->method('getContents')
->will($this->returnValue(
file_get_contents(__DIR__ . '/dummy-response.json')
));
$response = $this->getMock('Psr\\Http\\Message\\ResponseInterface');
$response->expects($this->any())
->method('getBody')
->will($this->returnValue($responseBody));
$uri = new Uri();
$this->guzzleClient = $this->getMock('GuzzleHttp\\ClientInterface');
$this->guzzleClient->expects($this->any())
->method('request')
->will($this->returnValue($response));
$this->guzzleClient->expects($this->any())
->method('getConfig')
->will($this->returnValue([
'base_uri' => $uri,
]));
$this->service = new GuzzleHttpService($this->guzzleClient);
}
public function testGetUnitEquipmentReturnsCollection()
{
$response = $this->service->getUnitEquipment('0123456');
$this->assertInstanceof('\\GrEduLabs\\Inventory\\EquipmentCollection', $response);
}
}
This diff is collapsed.
<?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 GrEduLabsTest\Stdlib;
use GrEduLabs\Stdlib\ImmutableArrayObject;
class ImmutableArrayObjectTest extends \PHPUnit_Framework_TestCase
{
protected $arrayObject;
protected function setup()
{
$this->arrayObject = new ImmutableArrayObject([
'five',
]);
}
public function testAppendThrowsException()
{
$this->setExpectedException('LogicException', 'Attempting to write to an immutable array');
$this->arrayObject->append('one');
}
public function testExchangeArrayThrowsException()
{
$this->setExpectedException('LogicException', 'Attempting to write to an immutable array');
$this->arrayObject->exchangeArray(['one', 'two', 'three']);
}
public function testOffsetSetThrowsException()
{
$this->setExpectedException('LogicException', 'Attempting to write to an immutable array');
$this->arrayObject->offsetSet(0, 'four');
}
public function testOffsetUnsetThrowsException()
{
$this->setExpectedException('LogicException', 'Attempting to write to an immutable array');
$this->arrayObject->offsetUnset(0);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment