-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6caa50c
commit d016ce3
Showing
9 changed files
with
124 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace App\Service; | ||
|
||
use App\Repository\DynamicFormFieldRepository; | ||
use App\Repository\ItemPriceHistoryRepository; | ||
use Cake\Chronos\Chronos; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
readonly class ChartDataGenerator | ||
{ | ||
public function __construct( | ||
private ItemPriceHistoryRepository $priceHistoryRepository, | ||
private DynamicFormFieldRepository $dynamicFormFieldRepository, | ||
private TranslatorInterface $translator, | ||
) { | ||
} | ||
|
||
public function getSerializedChartData(string $chartKey, int $relatedEntityId): array | ||
{ | ||
return match ($chartKey) { | ||
'price_history' => $this->getPriceHistoryChartData($relatedEntityId), | ||
default => [] | ||
}; | ||
} | ||
|
||
private function getPriceHistoryChartData(int $itemId): array | ||
{ | ||
$itemPrices = $this->priceHistoryRepository->getPricesForItem($itemId); | ||
|
||
$priceHistoryFields = $this->dynamicFormFieldRepository->getUserFieldsByFormKey('itemPrice'); | ||
$priceFields = []; | ||
foreach ($priceHistoryFields as $formField) { | ||
if (str_starts_with($formField->getFieldKey(), 'price_')) { | ||
$priceFields[] = $formField; | ||
} | ||
} | ||
|
||
$chartData = []; | ||
foreach ($priceFields as $priceField) { | ||
$priceData = []; | ||
foreach ($itemPrices as $itemPrice) { | ||
if (array_key_exists($formField->getFieldKey(), $itemPrice)) { | ||
$priceData[] = $itemPrice[$priceField->getFieldKey()]; | ||
} | ||
} | ||
$chartData[] = [ | ||
'name' => $this->translator->trans($priceField->getLabel()), | ||
'color' => '', | ||
'data' => $priceData, | ||
]; | ||
} | ||
|
||
$chartCategories = []; | ||
foreach ($itemPrices as $itemPrice) { | ||
$chartCategories[] = Chronos::parse($itemPrice['date'])->format('Y-m-d'); | ||
} | ||
|
||
return [ | ||
'data' => $chartData, | ||
'categories' => $chartCategories, | ||
]; | ||
} | ||
} |