Skip to content

Commit

Permalink
[OBOL] Add metric
Browse files Browse the repository at this point in the history
  • Loading branch information
that-guy-iain committed Sep 25, 2024
1 parent 24770ae commit ee48cc9
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"ramsey/uuid": "^4.5.1",
"ramsey/uuid-doctrine": "^1.8.1",
"sendgrid/sendgrid": "~7.11",
"stripe/stripe-php": "^7.128",
"stripe/stripe-php": "^v15.10.0",
"symfony/asset": "^7.0",
"symfony/console": "^7.0",
"symfony/doctrine-bridge": "^7.0",
Expand Down
73 changes: 73 additions & 0 deletions src/Obol/Model/Metric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2020-2024 Iain Cambridge
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <https://www.gnu.org/licenses/>.
*/

namespace Obol\Model;

class Metric
{
private string $displayName;

private string $eventName;

private string $aggregation;

private string $eventTimeWindow;

public function getDisplayName(): string
{
return $this->displayName;
}

public function setDisplayName(string $displayName): void
{
$this->displayName = $displayName;
}

public function getEventName(): string
{
return $this->eventName;
}

public function setEventName(string $eventName): void
{
$this->eventName = $eventName;
}

public function getAggregation(): string
{
return $this->aggregation;
}

public function setAggregation(string $aggregation): void
{
$this->aggregation = $aggregation;
}

public function getEventTimeWindow(): string
{
return $this->eventTimeWindow;
}

public function setEventTimeWindow(string $eventTimeWindow): void
{
$this->eventTimeWindow = $eventTimeWindow;
}
}
12 changes: 12 additions & 0 deletions src/Obol/Model/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class Price

private array $tiers = [];

private ?Metric $metric = null;

public function isIncludingTax(): bool
{
return $this->includingTax;
Expand Down Expand Up @@ -183,4 +185,14 @@ public function setTiers(array $tiers): void
{
$this->tiers = $tiers;
}

public function getMetric(): ?Metric
{
return $this->metric;
}

public function setMetric(?Metric $metric): void
{
$this->metric = $metric;
}
}
23 changes: 21 additions & 2 deletions src/Obol/Provider/Stripe/PriceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
use Obol\Model\Enum\BillingType;
use Obol\Model\Enum\TierMode;
use Obol\Model\Enum\UsageType;
use Obol\Model\Metric;
use Obol\Model\Price;
use Obol\Model\PriceCreation;
use Obol\Model\Tier;
use Obol\PriceServiceInterface;
use Obol\Provider\ProviderInterface;
use Psr\Log\LoggerAwareTrait;
use Stripe\Billing\Meter;
use Stripe\StripeClient;
use Stripe\StripeObject;

Expand Down Expand Up @@ -131,16 +133,23 @@ public function populatePrice(\Stripe\Price $stripePrice): Price
$price->setTierMode(TierMode::fromString($stripePrice->tiers_mode));
$price->setBillingType(BillingType::fromStripe($stripePrice->billing_scheme));
$price->setUsageType(UsageType::fromStripe($stripePrice->recurring?->usage_type));

if ($stripePrice->recurring?->meter) {
/** @var Meter $stripeMeter */
$stripeMeter = $this->stripe->billing->meters->retrieve($stripePrice->recurring->meter);
$price->setMetric($this->populateMeter($stripeMeter));
}

$tiers = [];
foreach ($stripePrice->tiers ?? [] as $tier) {
$tiers[] = $this->populatTier($tier);
$tiers[] = $this->populateTier($tier);
}
$price->setTiers($tiers);

return $price;
}

public function populatTier(StripeObject $data): Tier
public function populateTier(StripeObject $data): Tier
{
$tier = new Tier();
$tier->setUpTo($data->up_to);
Expand All @@ -149,4 +158,14 @@ public function populatTier(StripeObject $data): Tier

return $tier;
}

public function populateMeter(Meter $data): Metric
{
$metric = new Metric();
$metric->setAggregation($data->default_aggregation->formula);
$metric->setDisplayName($data->display_name);
$metric->setEventName($data->event_name);

return $metric;
}
}

0 comments on commit ee48cc9

Please sign in to comment.