Skip to content

Commit

Permalink
basic implementation; fetch unit equipment
Browse files Browse the repository at this point in the history
  • Loading branch information
kanellov committed Jan 20, 2016
1 parent ae1c00c commit 8bb6a57
Show file tree
Hide file tree
Showing 15 changed files with 723 additions and 175 deletions.
11 changes: 10 additions & 1 deletion app/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
return new GrEduLabs\Authentication\Adapter\Cas($settings['phpcas']);
};


$container['authentication_storage'] = function ($c) {
return new \GrEduLabs\Authentication\Storage\PhpSession();
};
Expand Down Expand Up @@ -139,6 +138,16 @@
};
};

// 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) {
Expand Down
2 changes: 2 additions & 0 deletions app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
'user' => $identity('uid'),
]);

var_dump($this->get('inventory_service')->getUnitEquipment('0551040'));

return $response;
})->setName('index');

Expand Down
56 changes: 56 additions & 0 deletions app/src/Inventory/Equipment.php
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,
];
}
}
67 changes: 67 additions & 0 deletions app/src/Inventory/EquipmentCollection.php
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);
}
}
76 changes: 76 additions & 0 deletions app/src/Inventory/GuzzleHttpService.php
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)
);
}
}
24 changes: 24 additions & 0 deletions app/src/Inventory/ServiceInterface.php
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);
}
37 changes: 37 additions & 0 deletions app/src/Stdlib/ImmutableArrayObject.php
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');
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"zendframework/zend-authentication": "^2.0",
"zendframework/zend-eventmanager": "^3.0",
"zendframework/zend-permissions-acl": "^2.0",
"jasig/phpcas": "1.3.4"
"jasig/phpcas": "1.3.4",
"guzzlehttp/guzzle": "^6.1"
},
"require-dev": {
"fabpot/php-cs-fixer": "1.*",
Expand Down
Loading

0 comments on commit 8bb6a57

Please sign in to comment.