Skip to content

Commit

Permalink
refactor: statistics typing DTO (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbarnsley authored Jan 16, 2024
1 parent 6114870 commit 75a60db
Show file tree
Hide file tree
Showing 21 changed files with 905 additions and 281 deletions.
47 changes: 47 additions & 0 deletions app/DTO/Statistics/AddressHoldingStatistics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace App\DTO\Statistics;

use Illuminate\Support\Collection;

final class AddressHoldingStatistics
{
public int $greaterThanOne;

public int $greaterThanOneThousand;

public int $greaterThanTenThousand;

public int $greaterThanOneHundredThousand;

public int $greaterThanOneMillion;

public function __construct(array $data)
{
$grouped = (new Collection($data))->pluck('count', 'grouped');

$this->greaterThanOne = $grouped->get(1, 0);
$this->greaterThanOneThousand = $grouped->get(1000, 0);
$this->greaterThanTenThousand = $grouped->get(10000, 0);
$this->greaterThanOneHundredThousand = $grouped->get(100000, 0);
$this->greaterThanOneMillion = $grouped->get(1000000, 0);
}

public static function make(array $data): self
{
return new self($data);
}

public function toArray(): array
{
return [
1 => $this->greaterThanOne,
1000 => $this->greaterThanOneThousand,
10000 => $this->greaterThanTenThousand,
100000 => $this->greaterThanOneHundredThousand,
1000000 => $this->greaterThanOneMillion,
];
}
}
53 changes: 53 additions & 0 deletions app/DTO/Statistics/DelegateStatistics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace App\DTO\Statistics;

use App\Models\Wallet;
use App\ViewModels\WalletViewModel;

final class DelegateStatistics
{
public ?WalletViewModel $mostUniqueVoters = null;

public ?WalletViewModel $leastUniqueVoters = null;

public ?WalletViewModel $mostBlocksForged = null;

public function __construct(
?Wallet $mostUniqueVoters = null,
?Wallet $leastUniqueVoters = null,
?Wallet $mostBlocksForged = null,
public ?WalletWithValue $oldestActiveDelegate = null,
public ?WalletWithValue $newestActiveDelegate = null,
) {
if ($mostUniqueVoters !== null) {
$this->mostUniqueVoters = new WalletViewModel($mostUniqueVoters);
}

if ($leastUniqueVoters !== null) {
$this->leastUniqueVoters = new WalletViewModel($leastUniqueVoters);
}

if ($mostBlocksForged !== null) {
$this->mostBlocksForged = new WalletViewModel($mostBlocksForged);
}
}

public static function make(
?Wallet $mostUniqueVoters = null,
?Wallet $leastUniqueVoters = null,
?Wallet $mostBlocksForged = null,
?WalletWithValue $oldestActiveDelegate = null,
?WalletWithValue $newestActiveDelegate = null,
): self {
return new self(
$mostUniqueVoters,
$leastUniqueVoters,
$mostBlocksForged,
$oldestActiveDelegate,
$newestActiveDelegate,
);
}
}
22 changes: 22 additions & 0 deletions app/DTO/Statistics/LowHighValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace App\DTO\Statistics;

final class LowHighValue
{
public function __construct(public ?float $low = null, public ?float $high = null)
{
//
}

public static function fromArray(?array $data): self
{
if ($data === null) {
return new self();
}

return new self($data['low'], $data['high']);
}
}
115 changes: 115 additions & 0 deletions app/DTO/Statistics/MarketDataPriceStatistics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

namespace App\DTO\Statistics;

use App\Services\NumberFormatter;
use ARKEcosystem\Foundation\UserInterface\Support\DateFormat;
use Carbon\Carbon;

final class MarketDataPriceStatistics
{
public function __construct(
public TimestampedValue $atl,
public TimestampedValue $ath,
public LowHighValue $daily,
public LowHighValue $year,
) {
//
}

public static function make(
TimestampedValue $atl,
TimestampedValue $ath,
LowHighValue $daily,
LowHighValue $year,
): self {
return new self(
$atl,
$ath,
$daily,
$year,
);
}

public function atlValue(): ?string
{
if ($this->atl->value === null) {
return null;
}

return $this->formatCurrency($this->atl->value);
}

public function atlDate(): ?string
{
if ($this->atl->timestamp === null) {
return null;
}

return Carbon::createFromTimestamp($this->atl->timestamp)->format(DateFormat::DATE);
}

public function athValue(): ?string
{
if ($this->ath->value === null) {
return null;
}

return $this->formatCurrency($this->ath->value);
}

public function athDate(): ?string
{
if ($this->ath->timestamp === null) {
return null;
}

return Carbon::createFromTimestamp($this->ath->timestamp)->format(DateFormat::DATE);
}

public function dailyLow(): ?string
{
if ($this->daily->low === null) {
return null;
}

return $this->formatCurrency($this->daily->low);
}

public function dailyHigh(): ?string
{
if ($this->daily->high === null) {
return null;
}

return $this->formatCurrency($this->daily->high);
}

public function yearLow(): ?string
{
if ($this->year->low === null) {
return null;
}

return $this->formatCurrency($this->year->low);
}

public function yearHigh(): ?string
{
if ($this->year->high === null) {
return null;
}

return $this->formatCurrency($this->year->high);
}

/**
* @param string|int|float $value
*/
private function formatCurrency($value): string
{
return NumberFormatter::currencyWithDecimals($value, 'USD', 2);
}
}
85 changes: 85 additions & 0 deletions app/DTO/Statistics/MarketDataRecordStatistics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace App\DTO\Statistics;

use App\Services\NumberFormatter;
use ARKEcosystem\Foundation\UserInterface\Support\DateFormat;
use Carbon\Carbon;

final class MarketDataRecordStatistics
{
public function __construct(
public ?float $today,
public TimestampedValue $atl,
public TimestampedValue $ath,
) {
//
}

public static function make(
?float $today,
TimestampedValue $atl,
TimestampedValue $ath,
): self {
return new self(
$today,
$atl,
$ath,
);
}

public function todayValueValue(): ?string
{
if ($this->today === null) {
return null;
}

return $this->formatCurrency($this->today);
}

public function atlValue(): ?string
{
if ($this->atl->value === null) {
return null;
}

return $this->formatCurrency($this->atl->value);
}

public function atlDate(): ?string
{
if ($this->atl->timestamp === null) {
return null;
}

return Carbon::createFromTimestamp($this->atl->timestamp)->format(DateFormat::DATE);
}

public function athValue(): ?string
{
if ($this->ath->value === null) {
return null;
}

return $this->formatCurrency($this->ath->value);
}

public function athDate(): ?string
{
if ($this->ath->timestamp === null) {
return null;
}

return Carbon::createFromTimestamp($this->ath->timestamp)->format(DateFormat::DATE);
}

/**
* @param string|int|float $value
*/
private function formatCurrency($value): string
{
return NumberFormatter::currencyForViews($value, 'USD');
}
}
28 changes: 28 additions & 0 deletions app/DTO/Statistics/MarketDataStatistics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\DTO\Statistics;

final class MarketDataStatistics
{
public function __construct(
public MarketDataPriceStatistics $prices,
public MarketDataVolumeStatistics $volume,
public MarketDataRecordStatistics $caps,
) {
//
}

public static function make(
MarketDataPriceStatistics $prices,
MarketDataVolumeStatistics $volume,
MarketDataRecordStatistics $caps,
): self {
return new self(
$prices,
$volume,
$caps,
);
}
}
Loading

0 comments on commit 75a60db

Please sign in to comment.