Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit a086084

Browse files
committed
fix(ShopOrder): add console version field filling
nb: I don't why but when a console was ordered and we needed to create a console in the db, it won't set the console version. Now what the ConsoleManager do is just setting the new console version as the latest version as defined in console-versions.php (with the attribute 'latest')
1 parent 60e4edf commit a086084

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

App/Controllers/Payment/PaypalController.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,16 @@ public function postExecute(ServerRequestInterface $request, Response $response,
184184
]
185185
], 400);
186186

187+
/** @var $order ShopOrder */
188+
ConsoleManager::createConsolesFromOrder($this->container, $order);
189+
187190
// we have a successful payment so we change the state in db
188191
$order->status = 'payed';
189192
$order->save();
190193

191194
// emit "order.payed" event
192195
$queue->publish('order.payed', ['id' => $order['id']]);
193196

194-
/** @var $order ShopOrder */
195-
ConsoleManager::createConsolesFromOrder($order);
196-
197197
return $response->withJson([
198198
'success' => true
199199
]);

App/Controllers/Payment/StripeController.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,14 @@ public function postExecute(ServerRequestInterface $request, Response $response,
186186
], 400);
187187
}
188188

189+
/** @var $order ShopOrder */
190+
ConsoleManager::createConsolesFromOrder($this->container, $order);
191+
189192
$order['status'] = 'payed';
190193
$order->save();
191194

192195
$queue->publish('order.payed', ['id' => $order['id']]);
193196

194-
/** @var $order ShopOrder */
195-
ConsoleManager::createConsolesFromOrder($order);
196-
197197
return $response->withJson([
198198
'success' => true,
199199
'notice' => 'Thanks you for this really cool event. This is very grateful from you. Love you!',

App/Utils/ConsoleManager.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
use App\Models\Console;
66
use App\Models\ShopItem;
77
use App\Models\ShopOrder;
8+
use Psr\Container\ContainerInterface;
89

910
class ConsoleManager
1011
{
1112
/**
1213
* From a shop order will create the consoles
1314
*
15+
* @param ContainerInterface $container
1416
* @param ShopOrder $shopOrder
1517
* @return array
1618
*/
17-
public static function createConsolesFromOrder($shopOrder)
19+
public static function createConsolesFromOrder(ContainerInterface $container, $shopOrder)
1820
{
1921
$result = ['consoles_ids' => [], 'items' => []];
2022
foreach ($shopOrder['items'] as $item) {
@@ -32,10 +34,25 @@ public static function createConsolesFromOrder($shopOrder)
3234
$console['storage'] = $item['pivot']['shop_item_custom_option_storage'];
3335
$console['color'] = $item['pivot']['shop_item_custom_option_color'];
3436
$console['token'] = \App\GraphQL\Query\Console::generateRandom(32);
37+
$console['version'] = ConsoleManager::getLatestConsoleVersion($container)['id'];
3538
$console->save();
3639
$consolesId['consoles_ids'][] = $console['id'];
3740
}
3841
}
3942
return $result;
4043
}
44+
45+
/**
46+
* Will return the last console version object from 'console-versions.php' container file
47+
*
48+
* @param ContainerInterface $container
49+
* @return array
50+
*/
51+
public static function getLatestConsoleVersion(ContainerInterface $container): array
52+
{
53+
return array_values(array_filter(
54+
$container->get('console-versions'),
55+
fn($v) => isset($v['latest']) && $v['latest']
56+
))[0];
57+
}
4158
}

App/config/console-versions.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
<?php
22

33
return [
4-
"console-versions" => [
5-
[
6-
"id" => "2.02"
7-
],
8-
[
9-
"id" => "3.06"
10-
]
4+
'console-versions' => [
5+
['id' => '2.02'],
6+
['id' => '3.06', 'latest' => true]
117
]
128
];

0 commit comments

Comments
 (0)