Skip to content
Snippets Groups Projects
Commit aaa0c54a authored by Sebastian Kehr's avatar Sebastian Kehr :rowboat_tone2: Committed by Dorian Merz
Browse files

refs #16222 [fid] prepare api client for order management

parent 21201724
No related merge requests found
......@@ -33,6 +33,7 @@ use fid\Hydrator\UserHydratorDelegatorFactory;
use fid\Listener\ErrorListener;
use fid\Listener\ErrorListenerFactory;
use fid\Listener\LocaleListener;
use fid\Serializer\OrderCreationRequestUserNormalizer;
use fid\Service\Client;
use fid\Service\ClientFactory;
use fid\VuFind\Auth\Authenticator;
......@@ -45,6 +46,7 @@ use fid\VuFind\ILS\Fid;
use fid\VuFind\ILS\FidFactory;
use fid\VuFind\Resolver\Driver\Ezb;
use fid\VuFind\Resolver\Driver\EzbDelegatorFactory;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use VuFind\Auth\ILSAuthenticator as BaseILSAuthenticator;
use VuFind\Db\Row\User as BaseUser;
use VuFind\Db\Row\UserFactory;
......@@ -344,4 +346,11 @@ return [
]
],
],
'symfony_serializer' => [
'normalizers' => [
// TODO: add DateTimeNormalizer to finc/symfony-zend-serializer-bridge
DateTimeNormalizer::class => 2000,
OrderCreationRequestUserNormalizer::class => 1000,
],
],
];
<?php
/**
* Copyright (C) 2019 Leipzig University Library
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Gregor Gawol <gawol@ub.uni-leipzig.de>
* @author Sebastian Kehr <kehr@ub.uni-leipzig.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU GPLv2
*/
namespace fid\Serializer;
use fid\Service\DataTransferObject\User;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
class OrderCreationRequestUserNormalizer implements
ContextAwareNormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
public function supportsNormalization(
$data,
$format = null,
array $context = []
) {
return $data instanceof User
&& in_array('order:creation:request', $context['groups'] ?? []);
}
/**
* @param mixed|User $object
* @param null $format
* @param array $context
*
* @return array|bool|float|int|string
*/
public function normalize($object, $format = null, array $context = [])
{
return $object->getId();
}
}
\ No newline at end of file
......@@ -23,6 +23,7 @@ namespace fid\Service;
use fid\Service\DataTransferObject\Library;
use fid\Service\DataTransferObject\Logon;
use fid\Service\DataTransferObject\Order;
use fid\Service\DataTransferObject\User;
use InvalidArgumentException;
use Psr\Http\Client\ClientExceptionInterface as HttpClientExceptionInterface;
......@@ -199,6 +200,59 @@ class Client
}
}
/**
* @param Order $order
*
* @return Order
* @throws ClientException
*/
public function requestOrderCreation(Order $order): Order
{
$body = $this->serializer->serialize($order, 'json', [
'groups' => ['order:creation:request'],
]);
$request = $this->buildRequest('post', 'orders', $body);
$response = $this->sendAuthenticatedRequest($request);
if ($response->getStatusCode() !== 201) {
$this->throwException($response);
}
/** @var Order $result */
$result = $this->serializer->deserialize((string)$response->getBody(),
Order::class, 'json', ['groups' => ['order:creation:response']]);
return $result;
}
/**
* @return array|Order[]
* @throws ClientException
*/
public function requestOrderList(): array
{
if ($list = $this->session['orders'] ?? null) {
return $list;
}
$request = $this->buildRequest('get', 'orders');
$response = $this->sendAuthenticatedRequest($request);
if ($response->getStatusCode() !== 200) {
$this->throwException($response);
}
/** @var Order[] $list */
$list = $this->serializer->deserialize((string)$response->getBody(),
Order::class . '[]', 'json');
$keys = array_map(function (Order $order) {
return $order->getId();
}, $list);
return $this->session['orders'] = array_combine($keys, $list);
}
/**
* @param string $baseUrl
* @param string $username
......
<?php
/**
* Copyright (C) 2019 Leipzig University Library
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Sebastian Kehr <kehr@ub.uni-leipzig.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU GPLv2
*/
namespace fid\Service\DataTransferObject;
namespace fid\Service\DataTransferObject;
use Symfony\Component\Serializer\Annotation\Groups;
class Order
{
/**
* @var string|null
* @Groups({
* "order:creation:response",
* "order:list:response",
* "user:details:response",
* "user:creation:response",
* "user:update:response"
* })
*/
protected $id;
/**
* @var string
* @Groups({
* "order:creation:request",
* "order:creation:response",
* "order:list:response",
* "user:details:response",
* "user:creation:response",
* "user:update:response"
* })
*/
protected $type = '';
/**
* @var User|null
* @Groups({
* "order:creation:request",
* "order:creation:response",
* "order:list:response",
* "user:details:response",
* "user:creation:response",
* "user:update:response"
* })
*/
protected $user;
/**
* @var array
* @Groups({
* "order:creation:request",
* "order:creation:response",
* "order:list:response",
* "user:details:response",
* "user:creation:response",
* "user:update:response"
* })
*/
protected $data = [];
/**
* @var \DateTime
* @Groups({
* "order:creation:response",
* "order:list:response",
* "user:details:response",
* "user:creation:response",
* "user:update:response"
* })
*/
protected $createdAt;
/**
* @return string|null
*/
public function getId(): ?string
{
return $this->id;
}
/**
* @param string|null $id
*/
public function setId(?string $id): void
{
$this->id = $id;
}
/**
* @return string
*/
public function getType(): string
{
return $this->type;
}
/**
* @param string $type
*/
public function setType(string $type): void
{
$this->type = $type;
}
/**
* @return User|null
*/
public function getUser(): ?User
{
return $this->user;
}
/**
* @param User|null $user
*/
public function setUser(?User $user): void
{
$this->user = $user;
}
/**
* @return array
*/
public function getData(): array
{
return $this->data;
}
/**
* @param array $data
*/
public function setData(array $data): void
{
$this->data = $data;
}
/**
* @return \DateTime
*/
public function getCreatedAt(): \DateTime
{
return $this->createdAt;
}
/**
* @param \DateTime $createdAt
*/
public function setCreatedAt(\DateTime $createdAt): void
{
$this->createdAt = $createdAt;
}
}
\ No newline at end of file
......@@ -28,6 +28,9 @@ class User
/**
* @var null|string
* @Groups({
* "order:creation:response",
* "order:details:response",
* "order:list:response",
* "user:details:response",
* "user:creation:response",
* "user:update:response"
......@@ -38,6 +41,9 @@ class User
/**
* @var null|string
* @Groups({
* "order:creation:response",
* "order:details:response",
* "order:list:response",
* "user:details:response",
* "user:creation:request",
* "user:creation:response",
......@@ -84,6 +90,9 @@ class User
/**
* @var null|string
* @Groups({
* "order:creation:response",
* "order:details:response",
* "order:list:response",
* "user:details:response",
* "user:creation:request",
* "user:creation:response",
......@@ -96,6 +105,9 @@ class User
/**
* @var null|string
* @Groups({
* "order:creation:response",
* "order:details:response",
* "order:list:response",
* "user:details:response",
* "user:creation:request",
* "user:creation:response",
......@@ -153,6 +165,16 @@ class User
*/
protected $addresses = [];
/**
* @var Order[]
* @Groups({
* "user:details:response",
* "user:creation:response",
* "user:update:response"
* })
*/
protected $orders = [];
/**
* @var array[]
* @Groups({
......@@ -180,16 +202,19 @@ class User
/**
* Magic function as shortcut for all getters
* TODO ensure public visibility of all called getters
*
* @param $name
*
* @return |null
*/
public function __get($name)
{
$methodName = 'get'.ucfirst($name);
if (method_exists($this,$methodName)) {
$methodName = 'get' . ucfirst($name);
if (method_exists($this, $methodName)) {
return $this->$methodName();
} else {
return null;
}
else return null;
}
/**
......@@ -370,6 +395,26 @@ class User
$address->setUser($this);
}
}
/**
* @return Order[]
*/
public function getOrders(): array
{
return $this->orders;
}
/**
* @param Order[] $orders
*/
public function setOrders(array $orders): void
{
$this->orders = $orders;
foreach ($orders as $order) {
$order->setUser($this);
}
}
/**
* @return array[]
*/
......@@ -390,8 +435,7 @@ class User
{
if (
isset($this->permissions[$permission])
&&
$this->permissions[$permission] === 'granted'
&& $this->permissions[$permission] === 'granted'
) {
return true;
}
......
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