Skip to content

Commit fea0bba

Browse files
authored
Merge pull request #7213 from magento-trigger/ph-delivery
[Platform Health] Updates for PHP8.1
2 parents 13d8ccd + dd83201 commit fea0bba

File tree

8 files changed

+937
-197
lines changed

8 files changed

+937
-197
lines changed

app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99
*/
1010
namespace Magento\Cron\Observer;
1111

12+
use Exception;
13+
use Magento\Cron\Model\DeadlockRetrierInterface;
1214
use Magento\Cron\Model\ResourceModel\Schedule\Collection as ScheduleCollection;
1315
use Magento\Cron\Model\Schedule;
1416
use Magento\Framework\App\State;
1517
use Magento\Framework\Console\Cli;
18+
use Magento\Framework\Event\Observer;
1619
use Magento\Framework\Event\ObserverInterface;
1720
use Magento\Framework\Exception\CronException;
21+
use Magento\Framework\Exception\LocalizedException;
1822
use Magento\Framework\Profiler\Driver\Standard\Stat;
1923
use Magento\Framework\Profiler\Driver\Standard\StatFactory;
20-
use Magento\Cron\Model\DeadlockRetrierInterface;
24+
use Throwable;
2125

2226
/**
2327
* The observer for processing cron jobs.
@@ -226,13 +230,15 @@ public function __construct(
226230
* Generate tasks schedule
227231
* Cleanup tasks schedule
228232
*
229-
* @param \Magento\Framework\Event\Observer $observer
233+
* @param Observer $observer
234+
*
230235
* @return void
236+
* @throws LocalizedException
231237
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
232238
* @SuppressWarnings(PHPMD.NPathComplexity)
233239
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
234240
*/
235-
public function execute(\Magento\Framework\Event\Observer $observer)
241+
public function execute(Observer $observer)
236242
{
237243
$currentTime = $this->dateTime->gmtTimestamp();
238244
$jobGroupsRoot = $this->_config->getJobs();
@@ -311,8 +317,9 @@ private function lockGroup(string $groupId, callable $callback): void
311317
* @param string[] $jobConfig
312318
* @param Schedule $schedule
313319
* @param string $groupId
320+
*
314321
* @return void
315-
* @throws \Exception
322+
* @throws Exception|Throwable
316323
*/
317324
protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId)
318325
{
@@ -322,25 +329,29 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
322329
if ($scheduledTime < $currentTime - $scheduleLifetime) {
323330
$schedule->setStatus(Schedule::STATUS_MISSED);
324331
// phpcs:ignore Magento2.Exceptions.DirectThrow
325-
throw new \Exception(sprintf('Cron Job %s is missed at %s', $jobCode, $schedule->getScheduledAt()));
332+
throw new Exception(sprintf('Cron Job %s is missed at %s', $jobCode, $schedule->getScheduledAt()));
326333
}
327334

328335
if (!isset($jobConfig['instance'], $jobConfig['method'])) {
329336
$schedule->setStatus(Schedule::STATUS_ERROR);
330337
// phpcs:ignore Magento2.Exceptions.DirectThrow
331-
throw new \Exception(sprintf('No callbacks found for cron job %s', $jobCode));
338+
throw new Exception(sprintf('No callbacks found for cron job %s', $jobCode));
332339
}
333340
$model = $this->_objectManager->create($jobConfig['instance']);
334341
$callback = [$model, $jobConfig['method']];
335342
if (!is_callable($callback)) {
336343
$schedule->setStatus(Schedule::STATUS_ERROR);
337344
// phpcs:ignore Magento2.Exceptions.DirectThrow
338-
throw new \Exception(
339-
sprintf('Invalid callback: %s::%s can\'t be called', $jobConfig['instance'], $jobConfig['method'])
345+
throw new Exception(
346+
sprintf(
347+
'Invalid callback: %s::%s can\'t be called',
348+
$jobConfig['instance'],
349+
$jobConfig['method']
350+
)
340351
);
341352
}
342353

343-
$schedule->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', $this->dateTime->gmtTimestamp()));
354+
$schedule->setExecutedAt(date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp()));
344355
$this->retrier->execute(
345356
function () use ($schedule) {
346357
$schedule->save();
@@ -355,7 +366,7 @@ function () use ($schedule) {
355366
$this->logger->info(sprintf('Cron Job %s is run', $jobCode));
356367
//phpcs:ignore Magento2.Functions.DiscouragedFunction
357368
call_user_func_array($callback, [$schedule]);
358-
} catch (\Throwable $e) {
369+
} catch (Throwable $e) {
359370
$schedule->setStatus(Schedule::STATUS_ERROR);
360371
$this->logger->error(
361372
sprintf(
@@ -380,8 +391,8 @@ function () use ($schedule) {
380391
$schedule->setStatus(
381392
Schedule::STATUS_SUCCESS
382393
)->setFinishedAt(
383-
strftime(
384-
'%Y-%m-%d %H:%M:%S',
394+
date(
395+
'Y-m-d H:i:s',
385396
$this->dateTime->gmtTimestamp()
386397
)
387398
);
@@ -607,14 +618,16 @@ protected function getConfigSchedule($jobConfig)
607618
* @param string $cronExpression
608619
* @param int $timeInterval
609620
* @param array $exists
621+
*
610622
* @return void
623+
* @throws Exception
611624
*/
612625
protected function saveSchedule($jobCode, $cronExpression, $timeInterval, $exists)
613626
{
614627
$currentTime = $this->dateTime->gmtTimestamp();
615628
$timeAhead = $currentTime + $timeInterval;
616629
for ($time = $currentTime; $time < $timeAhead; $time += self::SECONDS_IN_MINUTE) {
617-
$scheduledAt = strftime('%Y-%m-%d %H:%M:00', $time);
630+
$scheduledAt = date('Y-m-d H:i:00', $time);
618631
$alreadyScheduled = !empty($exists[$jobCode . '/' . $scheduledAt]);
619632
$schedule = $this->createSchedule($jobCode, $cronExpression, $time);
620633
$valid = $schedule->trySchedule();
@@ -648,8 +661,8 @@ protected function createSchedule($jobCode, $cronExpression, $time)
648661
->setCronExpr($cronExpression)
649662
->setJobCode($jobCode)
650663
->setStatus(Schedule::STATUS_PENDING)
651-
->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $this->dateTime->gmtTimestamp()))
652-
->setScheduledAt(strftime('%Y-%m-%d %H:%M', $time));
664+
->setCreatedAt(date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp()))
665+
->setScheduledAt(date('Y-m-d H:i', $time));
653666

654667
return $schedule;
655668
}

app/code/Magento/ImportExport/Model/Import/AbstractEntity.php

Lines changed: 52 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@
55
*/
66
namespace Magento\ImportExport\Model\Import;
77

8+
use Magento\Framework\App\Config\ScopeConfigInterface;
89
use Magento\Framework\App\ObjectManager;
910
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\DB\Adapter\AdapterInterface;
1012
use Magento\Framework\Serialize\Serializer\Json;
13+
use Magento\Framework\Stdlib\StringUtils;
1114
use Magento\ImportExport\Model\Import;
1215
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
1316
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
17+
use Magento\ImportExport\Model\ImportFactory;
18+
use Magento\ImportExport\Model\ResourceModel\Helper;
19+
use Magento\Store\Model\ScopeInterface;
1420

1521
/**
1622
* Import entity abstract model
1723
*
1824
* phpcs:disable Magento2.Classes.AbstractApi
1925
* @api
20-
*
2126
* @SuppressWarnings(PHPMD.TooManyFields)
2227
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2328
* @since 100.0.2
@@ -34,20 +39,24 @@ abstract class AbstractEntity
3439
*/
3540
const COLUMN_ACTION_VALUE_DELETE = 'delete';
3641

37-
/**#@+
38-
* XML paths to parameters
42+
/**
43+
* Path to bunch size configuration
3944
*/
4045
const XML_PATH_BUNCH_SIZE = 'import/format_v2/bunch_size';
4146

47+
/**
48+
* Path to page size configuration
49+
*/
4250
const XML_PATH_PAGE_SIZE = 'import/format_v2/page_size';
4351

44-
/**#@-*/
45-
46-
/**#@+
47-
* Database constants
52+
/**
53+
* Size of varchar value
4854
*/
4955
const DB_MAX_VARCHAR_LENGTH = 256;
5056

57+
/**
58+
* Size of text value
59+
*/
5160
const DB_MAX_TEXT_LENGTH = 65536;
5261

5362
const ERROR_CODE_SYSTEM_EXCEPTION = 'systemException';
@@ -84,9 +93,9 @@ abstract class AbstractEntity
8493
. ", see acceptable values on settings specified for Admin",
8594
];
8695

87-
/**#@-*/
88-
89-
/**#@-*/
96+
/**
97+
* @var AdapterInterface
98+
*/
9099
protected $_connection;
91100

92101
/**
@@ -97,9 +106,7 @@ abstract class AbstractEntity
97106
protected $_dataValidated = false;
98107

99108
/**
100-
* Valid column names
101-
*
102-
* @array
109+
* @var array
103110
*/
104111
protected $validColumnNames = [];
105112

@@ -132,7 +139,7 @@ abstract class AbstractEntity
132139
/**
133140
* Magento string lib
134141
*
135-
* @var \Magento\Framework\Stdlib\StringUtils
142+
* @var StringUtils
136143
*/
137144
protected $string;
138145

@@ -252,7 +259,7 @@ abstract class AbstractEntity
252259
/**
253260
* Core store config
254261
*
255-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
262+
* @var ScopeConfigInterface
256263
*/
257264
protected $_scopeConfig;
258265

@@ -285,54 +292,45 @@ abstract class AbstractEntity
285292
private $serializer;
286293

287294
/**
288-
* @param \Magento\Framework\Stdlib\StringUtils $string
289-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
290-
* @param \Magento\ImportExport\Model\ImportFactory $importFactory
291-
* @param \Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper
292-
* @param \Magento\Framework\App\ResourceConnection $resource
295+
* @param StringUtils $string
296+
* @param ScopeConfigInterface $scopeConfig
297+
* @param ImportFactory $importFactory
298+
* @param Helper $resourceHelper
299+
* @param ResourceConnection $resource
293300
* @param ProcessingErrorAggregatorInterface $errorAggregator
294301
* @param array $data
302+
* @param Json|null $serializer
295303
* @SuppressWarnings(PHPMD.NPathComplexity)
296304
*/
297305
public function __construct(
298-
\Magento\Framework\Stdlib\StringUtils $string,
299-
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
300-
\Magento\ImportExport\Model\ImportFactory $importFactory,
301-
\Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper,
306+
StringUtils $string,
307+
ScopeConfigInterface $scopeConfig,
308+
ImportFactory $importFactory,
309+
Helper $resourceHelper,
302310
ResourceConnection $resource,
303311
ProcessingErrorAggregatorInterface $errorAggregator,
304-
array $data = []
312+
array $data = [],
313+
Json $serializer = null
305314
) {
306-
$this->_scopeConfig = $scopeConfig;
307-
$this->_dataSourceModel = isset(
308-
$data['data_source_model']
309-
) ? $data['data_source_model'] : $importFactory->create()->getDataSourceModel();
310-
$this->_connection =
311-
isset($data['connection']) ?
312-
$data['connection'] :
313-
$resource->getConnection();
314315
$this->string = $string;
315-
$this->_pageSize = isset(
316-
$data['page_size']
317-
) ? $data['page_size'] : (static::XML_PATH_PAGE_SIZE ? (int)$this->_scopeConfig->getValue(
316+
$this->_scopeConfig = $scopeConfig;
317+
$this->_dataSourceModel = $data['data_source_model'] ?? $importFactory->create()->getDataSourceModel();
318+
$this->_maxDataSize = $data['max_data_size'] ?? $resourceHelper->getMaxDataSize();
319+
$this->_connection = $data['connection'] ?? $resource->getConnection();
320+
$this->errorAggregator = $errorAggregator;
321+
$this->_pageSize = $data['page_size'] ?? ((int) $this->_scopeConfig->getValue(
318322
static::XML_PATH_PAGE_SIZE,
319-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
320-
) : 0);
321-
$this->_maxDataSize = isset(
322-
$data['max_data_size']
323-
) ? $data['max_data_size'] : $resourceHelper->getMaxDataSize();
324-
$this->_bunchSize = isset(
325-
$data['bunch_size']
326-
) ? $data['bunch_size'] : (static::XML_PATH_BUNCH_SIZE ? (int)$this->_scopeConfig->getValue(
323+
ScopeInterface::SCOPE_STORE
324+
) ?: 0);
325+
$this->_bunchSize = $data['bunch_size'] ?? ((int) $this->_scopeConfig->getValue(
327326
static::XML_PATH_BUNCH_SIZE,
328-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
329-
) : 0);
330-
331-
$this->errorAggregator = $errorAggregator;
327+
ScopeInterface::SCOPE_STORE
328+
) ?: 0);
332329

333330
foreach ($this->errorMessageTemplates as $errorCode => $message) {
334331
$this->getErrorAggregator()->addErrorMessageTemplate($errorCode, $message);
335332
}
333+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
336334
}
337335

338336
/**
@@ -462,7 +460,7 @@ protected function _saveValidatedBunches()
462460
foreach ($entityGroup as $key => $value) {
463461
$bunchRows[$key] = $value;
464462
}
465-
$productDataSize = strlen($this->getSerializer()->serialize($bunchRows));
463+
$productDataSize = strlen($this->serializer->serialize($bunchRows));
466464

467465
/* Check if the new bunch should be started */
468466
$isBunchSizeExceeded = ($this->_bunchSize > 0 && count($bunchRows) >= $this->_bunchSize);
@@ -473,7 +471,7 @@ protected function _saveValidatedBunches()
473471
$entityGroup = [];
474472
}
475473

476-
if (isset($entityGroup) && $this->validateRow($rowData, $source->key())) {
474+
if (isset($entityGroup) && isset($rowData) && $this->validateRow($rowData, $source->key())) {
477475
/* Add row to entity group */
478476
$entityGroup[$source->key()] = $this->_prepareRowForDb($rowData);
479477
} elseif (isset($entityGroup)) {
@@ -488,22 +486,6 @@ protected function _saveValidatedBunches()
488486
return $this;
489487
}
490488

491-
/**
492-
* Get Serializer instance
493-
*
494-
* Workaround. Only way to implement dependency and not to break inherited child classes
495-
*
496-
* @return Json
497-
* @deprecated 100.2.0
498-
*/
499-
private function getSerializer()
500-
{
501-
if (null === $this->serializer) {
502-
$this->serializer = ObjectManager::getInstance()->get(Json::class);
503-
}
504-
return $this->serializer;
505-
}
506-
507489
/**
508490
* Add error with corresponding current data source row number.
509491
*
@@ -553,7 +535,7 @@ public function addMessageTemplate($errorCode, $message)
553535
/**
554536
* Import behavior getter
555537
*
556-
* @param array $rowData
538+
* @param array|null $rowData
557539
* @return string
558540
*/
559541
public function getBehavior(array $rowData = null)
@@ -569,7 +551,9 @@ public function getBehavior(array $rowData = null)
569551
if ($rowData !== null && $behavior == \Magento\ImportExport\Model\Import::BEHAVIOR_CUSTOM) {
570552
// try analyze value in self::COLUMN_CUSTOM column and return behavior for given $rowData
571553
if (array_key_exists(self::COLUMN_ACTION, $rowData)) {
572-
if (strtolower($rowData[self::COLUMN_ACTION]) == self::COLUMN_ACTION_VALUE_DELETE) {
554+
if ($rowData[self::COLUMN_ACTION]
555+
&& strtolower($rowData[self::COLUMN_ACTION]) == self::COLUMN_ACTION_VALUE_DELETE
556+
) {
573557
$behavior = \Magento\ImportExport\Model\Import::BEHAVIOR_DELETE;
574558
} else {
575559
// as per task description, if column value is different to self::COLUMN_CUSTOM_VALUE_DELETE,

0 commit comments

Comments
 (0)