Skip to content

Commit 7b13a3f

Browse files
Taras KubivIgor Liubarskyi
authored andcommitted
Merged in APITOCART-18507-shipments-workflow (pull request #51)
APITOCART-18507-shipments-workflow [MASTER] Approved-by: Oleg Syvak
2 parents 1f5e6bc + 22ed05a commit 7b13a3f

File tree

16 files changed

+1829
-3
lines changed

16 files changed

+1829
-3
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Api2Cart\Client\Model\ModelInterface;
6+
use App\Models\User;
7+
use App\Services\Api2Cart;
8+
use Carbon\Carbon;
9+
use GuzzleHttp\Promise\Promise;
10+
use GuzzleHttp\Promise\Utils as GuzzleHttpPromiseUtils;
11+
use Illuminate\Console\Command;
12+
use Illuminate\Support\Collection;
13+
use Illuminate\Support\Facades\Auth;
14+
15+
class MakeRequest extends Command
16+
{
17+
private $_api2Cart;
18+
19+
/**
20+
* The name and signature of the console command.
21+
*
22+
* @var string
23+
*/
24+
protected $signature = 'sendRequestToA2C
25+
{method : Method name}
26+
{parameters : base64encoded parameters}
27+
{resultEntity : Result entity in response}
28+
{resultEntityCount : Count of entity in response}
29+
{--subEntities=no : Get sub entities}
30+
{--subEntityId=id : SubEntity ID}';
31+
32+
/**
33+
* The console command description.
34+
*
35+
* @var string
36+
*/
37+
protected $description = 'Send request to Api2Cart';
38+
39+
/**
40+
* Create a new command instance.
41+
*
42+
* @return void
43+
*/
44+
public function __construct(Api2Cart $api2Cart)
45+
{
46+
$this->_api2Cart = $api2Cart;
47+
parent::__construct();
48+
}
49+
50+
/**
51+
* Execute the console command.
52+
*
53+
* @return void
54+
* @throws \Throwable
55+
*/
56+
public function handle()
57+
{
58+
$userId = $_SERVER['USER_ID'];
59+
60+
$user = User::find($userId);
61+
62+
if ($user) {
63+
$logs = collect([]);
64+
$entities = collect([]);
65+
Auth::login($user);
66+
$this->_api2Cart->setApiKey($user->api2cart_key);
67+
$resultEntity = $this->argument('resultEntity');
68+
$resultEntityCount = $this->argument('resultEntityCount');
69+
$method = $this->argument('method');
70+
71+
$parameters = json_decode(base64_decode($this->argument('parameters')), true);
72+
$cartId = $parameters['cart_id'] ?? '';
73+
unset($parameters['cart_id']);
74+
$subEntities = strtolower($this->option('subEntities'));
75+
76+
$result = $this->_api2Cart->{$method}(...$parameters);
77+
78+
$entities = $this->_getEntities($result, $resultEntityCount, $resultEntity, $entities, $cartId);
79+
80+
if (isset($result['pagination']['next']) && strlen($result['pagination']['next'])) {
81+
while (isset($result['pagination']['next']) && strlen($result['pagination']['next'])) {
82+
$result = $this->_api2Cart->{$method . 'Page'}(null, $result['pagination']['next']);
83+
$entities = $this->_getEntities($result, $resultEntityCount, $resultEntity, $entities, $cartId);
84+
}
85+
}
86+
87+
if ($entities->count()) {
88+
$entities = $entities->keyBy('id');
89+
}
90+
91+
if ($subEntities !== 'no') {
92+
$subEntityId = strtolower($this->option('subEntityId'));
93+
$entitiesIds = $entities->pluck($subEntityId)->all();
94+
95+
$maxConcurrent = 5;
96+
97+
/**
98+
* @var Promise[] $promises
99+
*/
100+
$promises = [];
101+
$subEntitiesResults = [];
102+
103+
foreach ($entitiesIds as $entityId) {
104+
if (count($promises) >= $maxConcurrent) {
105+
GuzzleHttpPromiseUtils::unwrap($promises);
106+
107+
foreach ($promises as $entityId => $promise) {
108+
if ($promise->getState() === Promise::FULFILLED) {
109+
110+
/**
111+
* @var ModelInterface $result
112+
*/
113+
$result = $promise->wait()[0] ?? [];
114+
115+
if ($result instanceof ModelInterface && $result->getReturnCode() === 10) {
116+
list($promise, $callback) = $this->_api2Cart->{$subEntities}($entityId);
117+
$subEntitiesResults[$entityId]['callback'] = $callback;
118+
$promises[$entityId] = $promise;
119+
break 2;
120+
} else {
121+
$subEntitiesResults[$entityId]['result'] = $result;
122+
}
123+
}
124+
}
125+
126+
$promises = [];
127+
usleep(1000000);
128+
}
129+
130+
list($promise, $callback) = $this->_api2Cart->{$subEntities}($entityId);
131+
$subEntitiesResults[$entityId]['callback'] = $callback;
132+
$promises[$entityId] = $promise;
133+
}
134+
135+
GuzzleHttpPromiseUtils::unwrap($promises);
136+
137+
foreach ($promises as $entityId => $promise) {
138+
$subEntitiesResults[$entityId]['result'] = $promise->wait()[0] ?? [];
139+
}
140+
141+
$entities = $entities->transform(function ($item) use ($subEntitiesResults, $subEntityId) {
142+
if (isset($subEntitiesResults[$item[$subEntityId]]['result'])
143+
&& $subEntitiesResults[$item[$subEntityId]]['result'] instanceof ModelInterface
144+
) {
145+
$item['sub_entities'] = $subEntitiesResults[$item[$subEntityId]]['callback']($subEntitiesResults[$item[$subEntityId]]['result']);
146+
}
147+
148+
return $item;
149+
});
150+
}
151+
152+
foreach ($this->_api2Cart->getLog()->all() as $item) {
153+
$logs->push($item);
154+
}
155+
156+
if (isset($entities->first()->create_at)) {
157+
$entities = $entities->sortBy('create_at.value');
158+
} else {
159+
$entities = $entities->sortBy('id');
160+
}
161+
162+
$data = collect([
163+
'result' => $entities,
164+
'logs' => $this->_api2Cart->getLog()->all()
165+
]);
166+
167+
$this->getOutput()->write($data->toJson());
168+
} else {
169+
$this->error('User not found');
170+
}
171+
}
172+
173+
/**
174+
* @param array $result Result
175+
* @param string $resultEntityCount Result entity count field name
176+
* @param string $resultEntity Result entity field name
177+
* @param Collection $entities Entities
178+
* @param string $cartId Cart ID
179+
*
180+
* @return Collection
181+
*/
182+
protected function _getEntities(
183+
array $result, $resultEntityCount, $resultEntity, Collection $entities, string $cartId
184+
): Collection
185+
{
186+
$resEntities = (!empty($result['result'][$resultEntityCount]))
187+
? collect($result['result'][$resultEntity])
188+
: collect([]);
189+
190+
if ($resEntities->count()) {
191+
foreach ($resEntities as $item) {
192+
$newItem = $item;
193+
$newItem['create_at']['value'] = Carbon::parse($item['create_at']['value'])->setTimezone('UTC')->format("Y-m-d\TH:i:sO");
194+
$newItem['cart_id'] = $cartId;
195+
$entities->push($newItem);
196+
}
197+
}
198+
199+
return $entities;
200+
}
201+
}

src/app/Console/Kernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Kernel extends ConsoleKernel
1313
* @var array
1414
*/
1515
protected $commands = [
16-
//
16+
\App\Console\Commands\MakeRequest::class,
1717
];
1818

1919
/**
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/*-----------------------------------------------------------------------------+
3+
| MagneticOne |
4+
| Copyright (c) 2023 MagneticOne.com <[email protected]> |
5+
| All rights reserved |
6+
+------------------------------------------------------------------------------+
7+
| PLEASE READ THE FULL TEXT OF SOFTWARE LICENSE AGREEMENT IN THE "license.txt"|
8+
| FILE PROVIDED WITH THIS DISTRIBUTION. THE AGREEMENT TEXT IS ALSO AVAILABLE |
9+
| AT THE FOLLOWING URL: http://www.magneticone.com/store/license.php |
10+
| |
11+
| THIS AGREEMENT EXPRESSES THE TERMS AND CONDITIONS ON WHICH YOU MAY USE |
12+
| THIS SOFTWARE PROGRAM AND ASSOCIATED DOCUMENTATION THAT MAGNETICONE |
13+
| (hereinafter referred to as "THE AUTHOR") IS FURNISHING OR MAKING |
14+
| AVAILABLE TO YOU WITH THIS AGREEMENT (COLLECTIVELY, THE "SOFTWARE"). |
15+
| PLEASE REVIEW THE TERMS AND CONDITIONS OF THIS LICENSE AGREEMENT |
16+
| CAREFULLY BEFORE INSTALLING OR USING THE SOFTWARE. BY INSTALLING, |
17+
| COPYING OR OTHERWISE USING THE SOFTWARE, YOU AND YOUR COMPANY |
18+
| (COLLECTIVELY, "YOU") ARE ACCEPTING AND AGREEING TO THE TERMS OF THIS |
19+
| LICENSE AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY THIS |
20+
| AGREEMENT, DO NOT INSTALL OR USE THE SOFTWARE. VARIOUS COPYRIGHTS AND |
21+
| OTHER INTELLECTUAL PROPERTY RIGHTS PROTECT THE SOFTWARE. THIS |
22+
| AGREEMENT IS A LICENSE AGREEMENT THAT GIVES YOU LIMITED RIGHTS TO USE |
23+
| THE SOFTWARE AND NOT AN AGREEMENT FOR SALE OR FOR TRANSFER OF TITLE. |
24+
| THE AUTHOR RETAINS ALL RIGHTS NOT EXPRESSLY GRANTED BY THIS AGREEMENT. |
25+
| |
26+
| The Developer of the Code is MagneticOne, |
27+
| Copyright (C) 2006 - 2023 All Rights Reserved. |
28+
+-----------------------------------------------------------------------------*/
29+
30+
namespace App\Http\Controllers\BusinessCases;
31+
32+
use App\Http\Controllers\Controller;
33+
use App\Jobs\MakeRequestToA2C;
34+
use App\Services\Api2Cart;
35+
use ShipmentsController;
36+
37+
/**
38+
* Date: 22.06.23 10:26
39+
* @category
40+
* @package
41+
* @author Taras Kubiv <[email protected]>
42+
* @license Not public license
43+
* @link https://www.api2cart.com
44+
*/
45+
46+
class OrdersShipmentsController extends Controller
47+
{
48+
private $_api2cart;
49+
50+
/**
51+
* OrdersShipmentsController constructor.
52+
*
53+
* @param Api2Cart $api2Cart Api2Cart service
54+
*/
55+
public function __construct(Api2Cart $api2Cart)
56+
{
57+
$this->_api2cart = $api2Cart;
58+
}
59+
60+
/**
61+
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
62+
*/
63+
public function index()
64+
{
65+
return view('business_cases.orders_shipments_controller.index');
66+
}
67+
}

0 commit comments

Comments
 (0)