Skip to content

Commit

Permalink
🎨 Refactor hard-coded reports to objects
Browse files Browse the repository at this point in the history
  • Loading branch information
saleem-hadad committed Jan 15, 2022
1 parent 5af2ccc commit 2492a47
Show file tree
Hide file tree
Showing 16 changed files with 318 additions and 78 deletions.
28 changes: 28 additions & 0 deletions app/Domain/Element.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Domain;

use JsonSerializable;
use Illuminate\Support\Str;

abstract class Element implements JsonSerializable
{
protected $name;

public function humanizedName()
{
return Str::title(Str::snake(class_basename(get_class($this)), ' '));
}

public function name()
{
return $this->name ?: $this->humanizedName();
}

public function jsonSerialize()
{
return [
'name' => $this->name(),
];
}
}
53 changes: 53 additions & 0 deletions app/Domain/Metrics/Metric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Domain\Metrics;

use App\Domain\Element;
use Illuminate\Support\Str;
use \App\Domain\Ranges\LastYear;
use \App\Domain\Ranges\LastMonth;
use \App\Domain\Ranges\CurrentYear;
use \App\Domain\Ranges\CurrentMonth;

abstract class Metric extends Element
{
protected $component;
protected $width = '1/2';
protected $ranges = [];
protected $graphqlQuery;

public function component()
{
return $this->component;
}

public function width()
{
return $this->width;
}

public function ranges()
{
return [
new CurrentMonth,
new LastMonth,
new CurrentYear,
new LastYear,
];
}

public function graphqlQuery()
{
return $this->graphqlQuery ?: Str::camel(class_basename(get_class($this)));
}

public function jsonSerialize()
{
return array_merge(parent::jsonSerialize(), [
'component' => $this->component(),
'width' => $this->width(),
'ranges' => $this->ranges(),
'graphql_query' => $this->graphqlQuery()
]);
}
}
8 changes: 8 additions & 0 deletions app/Domain/Metrics/PartitionMetric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Domain\Metrics;

abstract class PartitionMetric extends Metric
{
protected $component = 'partition-metric';
}
21 changes: 21 additions & 0 deletions app/Domain/Metrics/RelationPartitionMetric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Domain\Metrics;

abstract class RelationPartitionMetric extends PartitionMetric
{
protected $relationGraphqlQuery;
protected $relationDisplayUsing;
protected $relationForeignKey;

public function jsonSerialize()
{
return array_merge(parent::jsonSerialize(), [
'relation' => [
'graphql_query' => $this->relationGraphqlQuery,
'display_using' => $this->relationDisplayUsing,
'foreign_key' => $this->relationForeignKey,
]
]);
}
}
8 changes: 8 additions & 0 deletions app/Domain/Metrics/ValueMetric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Domain\Metrics;

abstract class ValueMetric extends Metric
{
protected $component = 'value-metric';
}
20 changes: 4 additions & 16 deletions app/Domain/Ranges/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,19 @@

namespace App\Domain\Ranges;

use JsonSerializable;
use App\Domain\Element;
use Illuminate\Support\Str;

abstract class Range implements JsonSerializable
abstract class Range extends Element
{
protected $name;
protected $start;
protected $end;

public function humanizedName()
{
return Str::title(Str::snake(class_basename(get_class($this)), ' '));
}

public function key()
{
return Str::slug($this->name(), '-', null);
}

public function name()
{
return $this->name ?: $this->humanizedName();
}

abstract public function start();

abstract public function end();
Expand All @@ -37,11 +26,10 @@ abstract public function end();
*/
public function jsonSerialize()
{
return [
return array_merge(parent::jsonSerialize(), [
'key' => $this->key(),
'name' => $this->name(),
'start' => $this->start(),
'end' => $this->end(),
];
]);
}
}
3 changes: 2 additions & 1 deletion app/GraphQL/Queries/ExpensesPerCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use App\Models\Category;
use Illuminate\Support\Facades\DB;
use App\Domain\Metrics\PartitionMetric;

class ExpensesPerCategory
class ExpensesPerCategory extends PartitionMetric
{
/**
* @param null $_
Expand Down
3 changes: 2 additions & 1 deletion app/GraphQL/Queries/IncomePerCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use App\Models\Category;
use Illuminate\Support\Facades\DB;
use App\Domain\Metrics\PartitionMetric;

class IncomePerCategory
class IncomePerCategory extends PartitionMetric
{
/**
* @param null $_
Expand Down
3 changes: 2 additions & 1 deletion app/GraphQL/Queries/TotalExpenses.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace App\GraphQL\Queries;

use App\Models\Transaction;
use App\Domain\Metrics\ValueMetric;

class TotalExpenses
class TotalExpenses extends ValueMetric
{
/**
* @param null $_
Expand Down
3 changes: 2 additions & 1 deletion app/GraphQL/Queries/TotalIncome.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace App\GraphQL\Queries;

use App\Models\Transaction;
use App\Domain\Metrics\ValueMetric;

class TotalIncome
class TotalIncome extends ValueMetric
{
/**
* @param null $_
Expand Down
9 changes: 7 additions & 2 deletions app/GraphQL/Queries/TotalPerBrand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@

use App\Models\Brand;
use Illuminate\Support\Facades\DB;
use App\Domain\Metrics\RelationPartitionMetric;

class TotalPerBrand
class TotalPerBrand extends RelationPartitionMetric
{
protected $relationGraphqlQuery = 'allCategories';
protected $relationDisplayUsing = 'name';
protected $relationForeignKey = 'category_id';

/**
* @param null $_
* @param array<string, mixed> $args
*/
public function __invoke($_, array $args)
{
$rangeData = app('findRangeByKey', ["key" => $args['range']]);
$categoryId = $args['category_id'] ?? 41;
$categoryId = $args['category_id'];

$query = Brand::query()
->where('category_id', $categoryId)
Expand Down
71 changes: 15 additions & 56 deletions config/finance.php
Original file line number Diff line number Diff line change
@@ -1,69 +1,28 @@
<?php

use \App\Domain\Ranges\CurrentMonth;
use \App\Domain\Ranges\LastMonth;
use \App\Domain\Ranges\CurrentYear;
use \App\Domain\Ranges\LastYear;

$ranges = [
new CurrentMonth,
new LastMonth,
new CurrentYear,
new LastYear,
];
use App\GraphQL\Queries\TotalIncome;
use App\GraphQL\Queries\TotalExpenses;
use App\GraphQL\Queries\TotalPerBrand;
use App\GraphQL\Queries\IncomePerCategory;
use App\GraphQL\Queries\ExpensesPerCategory;

return [
'currency' => 'AED',
'sms_templates' => [
'Purchase of AED {amount} with {card} at {brand},',
'Payment of AED {amount} to {brand} with {card}.',
'{brand} of AED {amount} has been credited ',
],
'currency' => 'AED',
'reports' => [
[
'name' => 'Total Income',
'graphql_query' => 'totalIncome',
'component' => 'value-metric',
'width' => '1/2',
'ranges' => $ranges,
],
[
'name' => 'Total Expenses',
'graphql_query' => 'totalExpenses',
'component' => 'value-metric',
'width' => '1/2',
'ranges' => $ranges,
],
[
'name' => 'Expenses per Category',
'graphql_query' => 'expensesPerCategory',
'component' => 'partition-metric',
'width' => '1/2',
'ranges' => $ranges,
],
[
'name' => 'Income per Category',
'graphql_query' => 'incomePerCategory',
'component' => 'partition-metric',
'width' => '1/2',
'ranges' => $ranges,
],
[
'name' => 'Total per Brand',
'graphql_query' => 'totalPerBrand',
'component' => 'partition-metric',
'width' => '1/2',
'ranges' => $ranges,
'relation' => [
'graphql_query' => 'allCategories',
'display_using' => 'name',
'foreign_key' => 'category_id',
]
],
new TotalIncome,
new TotalExpenses,
new IncomePerCategory,
new ExpensesPerCategory,
new TotalPerBrand,

// Total Income graph
// Total Expenses graph
// Expenses per Category graph
// Transactions per Brand graph
// TotalIncomeTrend
// TotalExpensesTrend
// ExpensesPerCategoryTrend
// TotalPerBrandTrend
]
];
Loading

0 comments on commit 2492a47

Please sign in to comment.