Skip to content

Commit 7b45a06

Browse files
committed
updating InvoiceHydrator to handle assigned Orders
1 parent d984464 commit 7b45a06

File tree

3 files changed

+95
-8
lines changed

3 files changed

+95
-8
lines changed

config/autoload/global.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use CleanPhp\Invoicer\Domain\Entity\Customer;
44
use CleanPhp\Invoicer\Domain\Entity\Invoice;
55
use CleanPhp\Invoicer\Domain\Entity\Order;
6+
use CleanPhp\Invoicer\Persistence\Hydrator\InvoiceHydrator;
67
use CleanPhp\Invoicer\Persistence\Hydrator\OrderHydrator;
78
use CleanPhp\Invoicer\Persistence\Zend\DataTable\CustomerTable;
89
use CleanPhp\Invoicer\Persistence\Zend\DataTable\InvoiceTable;
@@ -15,6 +16,13 @@
1516
'factories' => [
1617
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
1718

19+
'InvoiceHydrator' => function ($sm) {
20+
return new InvoiceHydrator(
21+
new ClassMethods(),
22+
$sm->get('OrderTable')
23+
);
24+
},
25+
1826
'OrderHydrator' => function ($sm) {
1927
return new OrderHydrator(
2028
new ClassMethods(),
@@ -38,7 +46,7 @@
3846
},
3947
'InvoiceTable' => function($sm) {
4048
$factory = new TableGatewayFactory();
41-
$hydrator = new ClassMethods();
49+
$hydrator = $sm->get('InvoiceHydrator');
4250

4351
return new InvoiceTable(
4452
$factory->createGateway(

specs/hydrator/invoice.spec.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
use CleanPhp\Invoicer\Domain\Entity\Invoice;
44
use CleanPhp\Invoicer\Persistence\Hydrator\InvoiceHydrator;
5+
use CleanPhp\Invoicer\Domain\Entity\Order;
56
use Zend\Stdlib\Hydrator\ClassMethods;
67

78
describe('Hydrator\InvoiceHydrator', function () {
89
beforeEach(function () {
9-
$this->hydrator = new InvoiceHydrator(new ClassMethods());
10+
$this->repository = $this->getProphet()
11+
->prophesize('CleanPhp\Invoicer\Domain\Repository\OrderRepositoryInterface');
12+
$this->hydrator = new InvoiceHydrator(
13+
new ClassMethods(),
14+
$this->repository->reveal()
15+
);
1016
});
1117

1218
describe('->extract()', function () {
@@ -30,6 +36,15 @@
3036
$invoice->getInvoiceDate()->format('Y-m-d')
3137
);
3238
});
39+
40+
it('should extract the order object', function () {
41+
$invoice = new Invoice();
42+
$invoice->setOrder((new Order())->setId(14));
43+
44+
$data = $this->hydrator->extract($invoice);
45+
46+
expect($data['order_id'])->to->equal($invoice->getOrder()->getId());
47+
});
3348
});
3449

3550
describe('->hydrate()', function () {
@@ -46,5 +61,31 @@
4661

4762
expect($invoice->getInvoiceDate()->format('Y-m-d'))->to->equal($data['invoice_date']);
4863
});
64+
65+
it('should hydrate an Order entity on the Invoice', function () {
66+
$data = ['order_id' => 500];
67+
68+
$order = (new Order())->setId(500);
69+
$invoice = new Invoice();
70+
71+
$this->repository->getById(500)
72+
->shouldBeCalled()
73+
->willReturn($order);
74+
75+
$this->hydrator->hydrate($data, $invoice);
76+
77+
expect($invoice->getOrder())->to->equal($order);
78+
79+
$this->getProphet()->checkPredictions();
80+
});
81+
82+
it('should hydrate the embedded order data', function () {
83+
$data = ['order' => ['id' => 20]];
84+
$invoice = new Invoice();
85+
86+
$this->hydrator->hydrate($data, $invoice);
87+
88+
expect($invoice->getOrder()->getId())->to->equal($data['order']['id']);
89+
});
4990
});
5091
});

src/Persistence/Hydrator/InvoiceHydrator.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace CleanPhp\Invoicer\Persistence\Hydrator;
44

55
use CleanPhp\Invoicer\Persistence\Hydrator\Strategy\DateStrategy;
6+
use CleanPhp\Invoicer\Domain\Entity\Order;
7+
use CleanPhp\Invoicer\Domain\Repository\OrderRepositoryInterface;
68
use Zend\Stdlib\Hydrator\ClassMethods;
79
use Zend\Stdlib\Hydrator\HydratorInterface;
810

@@ -18,15 +20,22 @@ class InvoiceHydrator implements HydratorInterface
1820
protected $wrappedHydrator;
1921

2022
/**
21-
* @param ClassMethods $wrappedHydrator
23+
* @var OrderRepositoryInterface
2224
*/
23-
public function __construct(ClassMethods $wrappedHydrator)
25+
private $orderRepository;
26+
27+
/**
28+
* @param HydratorInterface $wrappedHydrator
29+
* @param OrderRepositoryInterface $orderRepository
30+
*/
31+
public function __construct(HydratorInterface $wrappedHydrator, OrderRepositoryInterface $orderRepository)
2432
{
2533
$this->wrappedHydrator = $wrappedHydrator;
2634
$this->wrappedHydrator->addStrategy(
2735
'invoice_date',
2836
new DateStrategy()
2937
);
38+
$this->orderRepository = $orderRepository;
3039
}
3140

3241
/**
@@ -35,16 +44,45 @@ public function __construct(ClassMethods $wrappedHydrator)
3544
*/
3645
public function extract($object)
3746
{
38-
return $this->wrappedHydrator->extract($object);
47+
$data = $this->wrappedHydrator->extract($object);
48+
49+
if (array_key_exists('order', $data) &&
50+
!empty($data['order'])) {
51+
52+
$data['order_id'] = $data['order']->getId();
53+
unset($data['order']);
54+
}
55+
56+
return $data;
3957
}
4058

4159
/**
4260
* @param array $data
43-
* @param object $object
61+
* @param object $invoice
4462
* @return object
4563
*/
46-
public function hydrate(array $data, $object)
64+
public function hydrate(array $data, $invoice)
4765
{
48-
return $this->wrappedHydrator->hydrate($data, $object);
66+
$order = null;
67+
68+
if (isset($data['order'])) {
69+
$order = $this->wrappedHydrator->hydrate(
70+
$data['order'],
71+
new Order()
72+
);
73+
unset($data['order']);
74+
}
75+
76+
if (isset($data['order_id'])) {
77+
$order = $this->orderRepository->getById($data['order_id']);
78+
}
79+
80+
$invoice = $this->wrappedHydrator->hydrate($data, $invoice);
81+
82+
if ($order) {
83+
$invoice->setOrder($order);
84+
}
85+
86+
return $invoice;
4987
}
5088
}

0 commit comments

Comments
 (0)