Skip to content

Commit 0786e14

Browse files
authored
Adding Bill::usageInterval (#83)
1 parent 0b9b077 commit 0786e14

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

src/action/UsageInterval.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,18 @@ public function ratioOfMonth(): float
169169

170170
return $usageSeconds / $secondsInCurrentMonth;
171171
}
172+
173+
/**
174+
* Extends the usage interval to include both current and other intervals.
175+
*
176+
* @param UsageInterval $other
177+
* @return self
178+
*/
179+
public function extend(self $other): self
180+
{
181+
return new self(
182+
min($this->start, $other->start),
183+
max($this->end, $other->end),
184+
);
185+
}
172186
}

src/bill/Bill.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace hiqdev\php\billing\bill;
1212

1313
use DateTimeImmutable;
14+
use hiqdev\php\billing\action\UsageInterval;
1415
use hiqdev\php\billing\charge\ChargeInterface;
1516
use hiqdev\php\billing\customer\CustomerInterface;
1617
use hiqdev\php\billing\Exception\CannotReassignException;
@@ -63,6 +64,9 @@ class Bill implements BillInterface
6364
/** @var string */
6465
protected $comment;
6566

67+
/** @var UsageInterval */
68+
protected $usageInterval;
69+
6670
public function __construct(
6771
$id,
6872
TypeInterface $type,
@@ -85,6 +89,7 @@ public function __construct(
8589
$this->plan = $plan;
8690
$this->charges = $charges;
8791
$this->state = $state;
92+
$this->setUsageInterval(UsageInterval::wholeMonth($time));
8893
}
8994

9095
/**
@@ -104,6 +109,10 @@ public function getUniqueString(): string
104109
return implode('-', $parts);
105110
}
106111

112+
public function getUsageInterval(): UsageInterval
113+
{
114+
}
115+
107116
public function calculatePrice()
108117
{
109118
$quantity = $this->quantity->getQuantity();
@@ -233,4 +242,9 @@ public function jsonSerialize(): array
233242
{
234243
return array_filter(get_object_vars($this));
235244
}
245+
246+
public function setUsageInterval(UsageInterval $usageInterval): void
247+
{
248+
$this->usageInterval = $usageInterval;
249+
}
236250
}

src/bill/BillInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace hiqdev\php\billing\bill;
1212

1313
use DateTimeImmutable;
14+
use hiqdev\php\billing\action\UsageInterval;
1415
use hiqdev\php\billing\charge\ChargeInterface;
1516
use hiqdev\php\billing\customer\CustomerInterface;
1617
use hiqdev\php\billing\EntityInterface;
@@ -50,4 +51,8 @@ public function getPlan(): ?PlanInterface;
5051
* @return ChargeInterface[]
5152
*/
5253
public function getCharges();
54+
55+
public function getUsageInterval(): UsageInterval;
56+
57+
public function setUsageInterval(UsageInterval $usageInterval): void;
5358
}

src/charge/Generalizer.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace hiqdev\php\billing\charge;
1212

13+
use hiqdev\php\billing\action\UsageInterval;
1314
use hiqdev\php\billing\bill\Bill;
1415
use hiqdev\php\billing\bill\BillInterface;
1516
use hiqdev\php\billing\customer\CustomerInterface;
@@ -26,7 +27,7 @@ class Generalizer implements GeneralizerInterface
2627
{
2728
public function createBill(ChargeInterface $charge): BillInterface
2829
{
29-
return new Bill(
30+
$bill = new Bill(
3031
null,
3132
$this->generalizeType($charge),
3233
$this->generalizeTime($charge),
@@ -35,8 +36,12 @@ public function createBill(ChargeInterface $charge): BillInterface
3536
$this->generalizeCustomer($charge),
3637
$this->generalizeTarget($charge),
3738
$this->generalizePlan($charge),
38-
[$charge]
39+
[$charge],
3940
);
41+
42+
$bill->setUsageInterval($this->generalizeUsageInterval($charge));
43+
44+
return $bill;
4045
}
4146

4247
public function generalizeType(ChargeInterface $charge): TypeInterface
@@ -83,4 +88,9 @@ public function specializeTarget(TargetInterface $first, TargetInterface $other)
8388
{
8489
return $first;
8590
}
91+
92+
private function generalizeUsageInterval(ChargeInterface $charge): UsageInterval
93+
{
94+
return $charge->getAction()->getUsageInterval();
95+
}
8696
}

src/tools/Aggregator.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace hiqdev\php\billing\tools;
1212

13+
use hiqdev\php\billing\action\UsageInterval;
1314
use hiqdev\php\billing\bill\Bill;
1415
use hiqdev\php\billing\bill\BillInterface;
1516
use hiqdev\php\billing\charge\ChargeInterface;
@@ -77,7 +78,7 @@ protected function aggregateBills(array $bills, array $others): array
7778

7879
protected function aggregateBill(BillInterface $first, BillInterface $other): BillInterface
7980
{
80-
return new Bill(
81+
$bill = new Bill(
8182
$this->aggregateId($first, $other),
8283
$first->getType(),
8384
$first->getTime(),
@@ -88,6 +89,15 @@ protected function aggregateBill(BillInterface $first, BillInterface $other): Bi
8889
$first->getPlan(),
8990
array_merge($first->getCharges(), $other->getCharges())
9091
);
92+
93+
$bill->setUsageInterval($this->aggregateUsageInterval($first, $other));
94+
95+
return $bill;
96+
}
97+
98+
protected function aggregateUsageInterval(BillInterface $first, BillInterface $other): UsageInterval
99+
{
100+
return $first->getUsageInterval()->extend($other->getUsageInterval());
91101
}
92102

93103
/**

0 commit comments

Comments
 (0)