Skip to content

Update Framework interfaces and mark as API #32142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
namespace Magento\CustomerImportExport\Test\Unit\Model\ResourceModel\Import\CustomerComposite;

use Magento\Backend\Model\Auth\Session;
use Magento\CustomerImportExport\Model\Import\Address;
use Magento\CustomerImportExport\Model\Import\CustomerComposite;
use Magento\Framework\App\ResourceConnection;
Expand All @@ -20,13 +21,44 @@
use Magento\Framework\Json\Helper\Data;
use Magento\Framework\Model\ResourceModel\Db\Context;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\MockObject\MockBuilder;
use PHPUnit\Framework\TestCase;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class DataTest extends TestCase
{
/**
* @var string[]
*/
private $mockBuilderCallbacks = [
Session::class => 'getSessionMock'
];

/**
* @inheritDoc
*/
public function getMockBuilder(string $className): MockBuilder
{
$mockBuilder = parent::getMockBuilder($className);
if (isset($this->mockBuilderCallbacks[$className])) {
$mockBuilder = call_user_func_array([$this, $this->mockBuilderCallbacks[$className]], [$mockBuilder]);
}

return $mockBuilder;
}

/**
* @param MockBuilder $mockBuilder
* @return MockBuilder
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) Used in callback.
*/
private function getSessionMock(MockBuilder $mockBuilder): MockBuilder
{
return $mockBuilder->onlyMethods(['isLoggedIn']);
}

/**
* Array of customer attributes
*
Expand Down Expand Up @@ -57,9 +89,10 @@ protected function _getDependencies($entityType, $bunchData)
);

/** @var $selectMock \Magento\Framework\DB\Select */
$selectMock = $this->createPartialMock(Select::class, ['from', 'order']);
$selectMock = $this->createPartialMock(Select::class, ['from', 'order', 'where']);
$selectMock->expects($this->any())->method('from')->willReturnSelf();
$selectMock->expects($this->any())->method('order')->willReturnSelf();
$selectMock->expects($this->any())->method('where')->willReturnSelf();

/** @var AdapterInterface $connectionMock */
$connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\Pdo\Mysql::class)
Expand Down
19 changes: 2 additions & 17 deletions app/code/Magento/Eav/Model/TypeLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Eav\Model;

Expand All @@ -20,27 +21,19 @@ class TypeLocator implements CustomAttributeTypeLocatorInterface
*/
private $typeLocators;

/**
* @var ServiceTypeListInterface
*/
private $serviceTypeList;

/**
* Initialize TypeLocator
*
* @param ServiceTypeListInterface $serviceTypeList
* @param \Magento\Framework\Webapi\CustomAttributeTypeLocatorInterface[] $typeLocators
*/
public function __construct(
ServiceTypeListInterface $serviceTypeList,
array $typeLocators = []
) {
$this->typeLocators = $typeLocators;
$this->serviceTypeList = $serviceTypeList;
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getType($attributeCode, $entityType)
{
Expand All @@ -53,12 +46,4 @@ public function getType($attributeCode, $entityType)

return TypeProcessor::NORMALIZED_ANY_TYPE;
}

/**
* {@inheritDoc}
*/
public function getAllServiceDataInterfaces()
{
return $this->serviceTypeList->getDataTypes();
}
}
13 changes: 3 additions & 10 deletions app/code/Magento/Eav/Model/TypeLocator/ComplexType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Eav\Model\TypeLocator;

Expand Down Expand Up @@ -52,7 +53,7 @@ public function __construct(
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getType($attributeCode, $entityType)
{
Expand Down Expand Up @@ -83,7 +84,7 @@ public function getType($attributeCode, $entityType)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getDataTypes()
{
Expand All @@ -96,14 +97,6 @@ public function getDataTypes()
return array_unique($dataInterfaceArray);
}

/**
* {@inheritDoc}
*/
public function getAllServiceDataInterfaces()
{
return $this->getDataTypes();
}

/**
* @return array [['backend model' => 'simple or complex type'], ..]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,4 @@ public function getType($attributeCode, $entityType)

return $type;
}

/**
* @inheritDoc
*/
public function getAllServiceDataInterfaces()
{
return $this->complexTypeLocator->getDataTypes();
}
}
22 changes: 2 additions & 20 deletions app/code/Magento/Eav/Model/TypeLocator/SimpleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Eav\Model\TypeLocator;

use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Reflection\TypeProcessor;
use Magento\Framework\Webapi\CustomAttribute\ServiceTypeListInterface;
use Magento\Framework\Webapi\CustomAttributeTypeLocatorInterface;

/**
Expand All @@ -22,23 +22,15 @@ class SimpleType implements CustomAttributeTypeLocatorInterface
*/
private $attributeRepository;

/**
* @var ServiceTypeListInterface
*/
private $serviceTypeList;

/**
* Constructor
*
* @param AttributeRepositoryInterface $attributeRepository
* @param ServiceTypeListInterface $serviceTypeList
*/
public function __construct(
AttributeRepositoryInterface $attributeRepository,
ServiceTypeListInterface $serviceTypeList
AttributeRepositoryInterface $attributeRepository
) {
$this->attributeRepository = $attributeRepository;
$this->serviceTypeList = $serviceTypeList;
}

/**
Expand All @@ -62,14 +54,4 @@ public function getType($attributeCode, $entityType)
];
return $backendTypeMap[$backendType] ?? TypeProcessor::NORMALIZED_ANY_TYPE;
}

/**
* Get data Types from service type list
*
* @return void
*/
public function getAllServiceDataInterfaces()
{
$this->serviceTypeList->getDataTypes();
}
}
4 changes: 2 additions & 2 deletions app/code/Magento/Eav/Test/Unit/Model/TypeLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testGetType(
$serviceClass,
$serviceEntityTypeMapData,
$expected
) {
): void {
$this->complexType->expects($this->once())->method('getType')->willReturn($expected);
$type = $this->customAttributeTypeLocator->getType(
$attributeCode,
Expand All @@ -83,7 +83,7 @@ public function testGetType(
* @return array
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function getTypeDataProvider()
public function getTypeDataProvider(): array
{
$serviceInterface = ProductInterface::class;
$eavEntityType = 'catalog_product';
Expand Down
89 changes: 84 additions & 5 deletions app/code/Magento/ImportExport/Model/ResourceModel/Import/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@
*/
namespace Magento\ImportExport\Model\ResourceModel\Import;

use Magento\Backend\Model\Auth\Session;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DB\Select;

/**
* ImportExport import data resource model
*
* @api
* @since 100.0.2
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse) Necessary to get current logged in user without modifying methods
*/
class Data extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implements \IteratorAggregate
{
/**
* Offline import user ID
*/
private const DEFAULT_USER_ID = 0;
/**
* @var \Iterator
*/
Expand All @@ -24,21 +33,28 @@ class Data extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implemen
* @var \Magento\Framework\Json\Helper\Data
*/
protected $jsonHelper;
/**
* @var Session
*/
private $authSession;

/**
* Class constructor
*
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
* @param string $connectionName
* @param string|null $connectionName
* @param Session|null $authSession
*/
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
\Magento\Framework\Json\Helper\Data $jsonHelper,
$connectionName = null
$connectionName = null,
?Session $authSession = null
) {
parent::__construct($context, $connectionName);
$this->jsonHelper = $jsonHelper;
$this->authSession = $authSession ?? ObjectManager::getInstance()->get(Session::class);
}

/**
Expand All @@ -61,6 +77,7 @@ public function getIterator()
{
$connection = $this->getConnection();
$select = $connection->select()->from($this->getMainTable(), ['data'])->order('id ASC');
$select = $this->prepareSelect($select);
$stmt = $connection->query($select);

$stmt->setFetchMode(\Zend_Db::FETCH_NUM);
Expand All @@ -82,7 +99,7 @@ public function getIterator()
*/
public function cleanBunches()
{
return $this->getConnection()->delete($this->getMainTable());
return $this->getConnection()->delete($this->getMainTable(), $this->prepareDelete([]));
}

/**
Expand Down Expand Up @@ -115,7 +132,9 @@ public function getEntityTypeCode()
public function getUniqueColumnData($code)
{
$connection = $this->getConnection();
$values = array_unique($connection->fetchCol($connection->select()->from($this->getMainTable(), [$code])));
$select = $connection->select()->from($this->getMainTable(), [$code]);
$select = $this->prepareSelect($select);
$values = array_unique($connection->fetchCol($select));

if (count($values) != 1) {
throw new \Magento\Framework\Exception\LocalizedException(
Expand Down Expand Up @@ -170,7 +189,67 @@ public function saveBunch($entity, $behavior, array $data)

return $this->getConnection()->insert(
$this->getMainTable(),
['behavior' => $behavior, 'entity' => $entity, 'data' => $encodedData]
$this->prepareInsert(['behavior' => $behavior, 'entity' => $entity, 'data' => $encodedData])
);
}

/**
* Prepare select for query
*
* @param Select $select
* @return Select
*/
private function prepareSelect(Select $select): Select
{
// check if the table has not been overridden for backward compatibility
if ($this->getMainTable() === $this->getTable('importexport_importdata')) {
// user_id is NULL part is for backward compatibility
$select->where('user_id=? OR user_id is NULL', $this->getUserId());
}

return $select;
}

/**
* Prepare data for insert
*
* @param array $data
* @return array
*/
private function prepareInsert(array $data): array
{
// check if the table has not been overridden for backward compatibility
if ($this->getMainTable() === $this->getTable('importexport_importdata')) {
$data['user_id'] = $this->getUserId();
}

return $data;
}

/**
* Prepare delete constraints
*
* @param array $where
* @return array
*/
private function prepareDelete(array $where): array
{
// check if the table has not been overridden for backward compatibility
if ($this->getMainTable() === $this->getTable('importexport_importdata')) {
// user_id is NULL part is for backward compatibility
$where['user_id=? OR user_id is NULL'] = $this->getUserId();
}

return $where;
}

/**
* Get current user ID
*
* @return int
*/
private function getUserId(): int
{
return $this->authSession->isLoggedIn() ? $this->authSession->getUser()->getId() : self::DEFAULT_USER_ID;
}
}
Loading