Skip to content

Commit 5300fff

Browse files
committed
Merge branch 'MAGETWO-63640' into MAGETWO-63638
2 parents e826366 + a738ddf commit 5300fff

File tree

7 files changed

+268
-3
lines changed

7 files changed

+268
-3
lines changed

app/code/Magento/Sales/Model/ResourceModel/AbstractGrid.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function getGridTable()
6767
{
6868
return $this->getTable($this->gridTableName);
6969
}
70+
7071
/**
7172
* Purge grid row
7273
*
@@ -89,6 +90,9 @@ public function purge($value, $field = null)
8990
*
9091
* @param string $default
9192
* @return string
93+
* @deprecated this method is not used in abstract model but only in single child so
94+
* this deprecation is a part of cleaning abstract classes.
95+
* @see \Magento\Sales\Model\ResourceModel\Provider\UpdatedIdListProvider
9296
*/
9397
protected function getLastUpdatedAtValue($default = '0000-00-00 00:00:00')
9498
{

app/code/Magento/Sales/Model/ResourceModel/Grid.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
*/
66
namespace Magento\Sales\Model\ResourceModel;
77

8+
use Magento\Framework\App\ObjectManager;
89
use Magento\Framework\DB\Adapter\AdapterInterface;
9-
use Magento\Sales\Model\ResourceModel\AbstractGrid;
1010
use Magento\Framework\Model\ResourceModel\Db\Context;
11+
use Magento\Sales\Model\ResourceModel\Provider\NotSyncedDataProviderInterface;
1112

1213
/**
1314
* Class Grid
@@ -39,6 +40,11 @@ class Grid extends AbstractGrid
3940
*/
4041
protected $columns;
4142

43+
/**
44+
* @var NotSyncedDataProviderInterface
45+
*/
46+
private $notSyncedDataProvider;
47+
4248
/**
4349
* @param Context $context
4450
* @param string $mainTableName
@@ -47,6 +53,7 @@ class Grid extends AbstractGrid
4753
* @param array $joins
4854
* @param array $columns
4955
* @param string $connectionName
56+
* @param NotSyncedDataProviderInterface $notSyncedDataProvider
5057
*/
5158
public function __construct(
5259
Context $context,
@@ -55,13 +62,16 @@ public function __construct(
5562
$orderIdField,
5663
array $joins = [],
5764
array $columns = [],
58-
$connectionName = null
65+
$connectionName = null,
66+
NotSyncedDataProviderInterface $notSyncedDataProvider = null
5967
) {
6068
$this->mainTableName = $mainTableName;
6169
$this->gridTableName = $gridTableName;
6270
$this->orderIdField = $orderIdField;
6371
$this->joins = $joins;
6472
$this->columns = $columns;
73+
$this->notSyncedDataProvider =
74+
$notSyncedDataProvider ?: ObjectManager::getInstance()->get(NotSyncedDataProviderInterface::class);
6575
parent::__construct($context, $connectionName);
6676
}
6777

@@ -99,7 +109,10 @@ public function refresh($value, $field = null)
99109
public function refreshBySchedule()
100110
{
101111
$select = $this->getGridOriginSelect()
102-
->where($this->mainTableName . '.updated_at >= ?', $this->getLastUpdatedAtValue());
112+
->where(
113+
$this->mainTableName . '.entity_id IN (?)',
114+
$this->notSyncedDataProvider->getIds($this->mainTableName, $this->gridTableName)
115+
);
103116

104117
return $this->getConnection()->query(
105118
$this->getConnection()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sales\Model\ResourceModel\Provider;
7+
8+
use Magento\Framework\ObjectManager\TMapFactory;
9+
10+
/**
11+
* Implements NotSyncedDataProviderInterface as composite
12+
*/
13+
class NotSyncedDataProvider implements NotSyncedDataProviderInterface
14+
{
15+
/**
16+
* @var NotSyncedDataProviderInterface[]
17+
*/
18+
private $providers;
19+
20+
/**
21+
* @param TMapFactory $tmapFactory
22+
* @param array $providers
23+
*/
24+
public function __construct(
25+
TMapFactory $tmapFactory,
26+
array $providers = []
27+
) {
28+
$this->providers = $tmapFactory->create(
29+
[
30+
'array' => $providers,
31+
'type' => NotSyncedDataProviderInterface::class
32+
]
33+
);
34+
}
35+
36+
/**
37+
* @inheritDoc
38+
*/
39+
public function getIds($mainTableName, $gridTableName)
40+
{
41+
$result = [];
42+
foreach ($this->providers as $provider) {
43+
$result = array_merge($result, $provider->getIds($mainTableName, $gridTableName));
44+
}
45+
46+
return array_unique($result);
47+
}
48+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sales\Model\ResourceModel\Provider;
7+
8+
/**
9+
* Interface provides entities id list that should be updated in grid
10+
*/
11+
interface NotSyncedDataProviderInterface
12+
{
13+
/**
14+
* Returns id list of entities for adding or updating in grid.
15+
*
16+
* @param string $mainTableName source table name
17+
* @param string $gridTableName grid table name
18+
* @return array
19+
*/
20+
public function getIds($mainTableName, $gridTableName);
21+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sales\Model\ResourceModel\Provider;
7+
8+
use Magento\Framework\App\ResourceConnection;
9+
use Magento\Framework\DB\Adapter\AdapterInterface;
10+
11+
/**
12+
* Provides latest updated entities ids list
13+
*/
14+
class UpdatedIdListProvider implements NotSyncedDataProviderInterface
15+
{
16+
/**
17+
* @var ResourceConnection
18+
*/
19+
private $resourceConnection;
20+
21+
/**
22+
* @var AdapterInterface
23+
*/
24+
private $connection;
25+
26+
/**
27+
* NotSyncedDataProvider constructor.
28+
* @param ResourceConnection $resourceConnection
29+
*/
30+
public function __construct(
31+
ResourceConnection $resourceConnection
32+
) {
33+
$this->resourceConnection = $resourceConnection;
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function getIds($mainTableName, $gridTableName)
40+
{
41+
$select = $this->getConnection()->select()
42+
->from($this->getConnection()->getTableName($mainTableName), [$mainTableName.'.entity_id'])
43+
->joinLeft(
44+
[$gridTableName => $this->getConnection()->getTableName($gridTableName)],
45+
sprintf(
46+
'%s.%s = %s.%s',
47+
$mainTableName,
48+
'entity_id',
49+
$gridTableName,
50+
'entity_id'
51+
),
52+
[]
53+
)
54+
->where($gridTableName.'.entity_id IS NULL');
55+
56+
return $this->getConnection()->fetchAll($select, [], \Zend_Db::FETCH_COLUMN);
57+
}
58+
59+
/**
60+
* Returns connection.
61+
*
62+
* @return AdapterInterface
63+
*/
64+
private function getConnection()
65+
{
66+
if (!$this->connection) {
67+
$this->connection = $this->resourceConnection->getConnection('sales');
68+
}
69+
70+
return $this->connection;
71+
}
72+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sales\Test\Unit\Model\ResourceModel\Provider;
7+
8+
use Magento\Framework\ObjectManager\TMap;
9+
use Magento\Framework\ObjectManager\TMapFactory;
10+
use Magento\Sales\Model\ResourceModel\Provider\NotSyncedDataProvider;
11+
use Magento\Sales\Model\ResourceModel\Provider\NotSyncedDataProviderInterface;
12+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
13+
14+
/**
15+
* Class NotSyncedDataProviderTest
16+
*/
17+
class NotSyncedDataProviderTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testGetIdsEmpty()
20+
{
21+
/** @var TMapFactory|MockObject $tMapFactory */
22+
$tMapFactory = $this->getMockBuilder(TMapFactory::class)
23+
->disableOriginalConstructor()
24+
->setMethods(['create'])
25+
->getMock();
26+
$tMap = $this->getMockBuilder(TMap::class)
27+
->disableOriginalConstructor()
28+
->getMock();
29+
30+
$tMapFactory->expects(static::once())
31+
->method('create')
32+
->with(
33+
[
34+
'array' => [],
35+
'type' => NotSyncedDataProviderInterface::class
36+
]
37+
)
38+
->willReturn($tMap);
39+
$tMap->expects(static::once())
40+
->method('getIterator')
41+
->willReturn(new \ArrayIterator([]));
42+
43+
$provider = new NotSyncedDataProvider($tMapFactory, []);
44+
static::assertEquals([], $provider->getIds('main_table', 'grid_table'));
45+
}
46+
47+
/**
48+
* @covers \Magento\Sales\Model\ResourceModel\Provider\NotSyncedDataProvider::getIds
49+
*/
50+
public function testGetIds()
51+
{
52+
/** @var TMapFactory|MockObject $tMapFactory */
53+
$tMapFactory = $this->getMockBuilder(TMapFactory::class)
54+
->disableOriginalConstructor()
55+
->setMethods(['create'])
56+
->getMock();
57+
$tMap = $this->getMockBuilder(TMap::class)
58+
->disableOriginalConstructor()
59+
->getMock();
60+
61+
$provider1 = $this->getMockBuilder(NotSyncedDataProviderInterface::class)
62+
->getMockForAbstractClass();
63+
$provider1->expects(static::once())
64+
->method('getIds')
65+
->willReturn([1, 2]);
66+
67+
$provider2 = $this->getMockBuilder(NotSyncedDataProviderInterface::class)
68+
->getMockForAbstractClass();
69+
$provider2->expects(static::once())
70+
->method('getIds')
71+
->willReturn([2, 3, 4]);
72+
73+
$tMapFactory->expects(static::once())
74+
->method('create')
75+
->with(
76+
[
77+
'array' => [
78+
'provider1' => NotSyncedDataProviderInterface::class,
79+
'provider2' => NotSyncedDataProviderInterface::class
80+
],
81+
'type' => NotSyncedDataProviderInterface::class
82+
]
83+
)
84+
->willReturn($tMap);
85+
$tMap->expects(static::once())
86+
->method('getIterator')
87+
->willReturn(new \ArrayIterator([$provider1, $provider2]));
88+
89+
$provider = new NotSyncedDataProvider(
90+
$tMapFactory,
91+
[
92+
'provider1' => NotSyncedDataProviderInterface::class,
93+
'provider2' => NotSyncedDataProviderInterface::class,
94+
]
95+
);
96+
97+
static::assertEquals([1, 2, 3, 4], array_values($provider->getIds('main_table', 'grid_table')));
98+
}
99+
}

app/code/Magento/Sales/etc/di.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@
113113
<preference for="Magento\Sales\Model\Order\Creditmemo\NotifierInterface" type="Magento\Sales\Model\Order\Creditmemo\Notifier"/>
114114
<preference for="Magento\Sales\Api\RefundOrderInterface" type="Magento\Sales\Model\RefundOrder"/>
115115
<preference for="Magento\Sales\Api\RefundInvoiceInterface" type="Magento\Sales\Model\RefundInvoice"/>
116+
<preference for="Magento\Sales\Model\ResourceModel\Provider\NotSyncedDataProviderInterface" type="Magento\Sales\Model\ResourceModel\Provider\NotSyncedDataProvider" />
117+
<type name="Magento\Sales\Model\ResourceModel\Provider\NotSyncedDataProvider">
118+
<arguments>
119+
<argument name="providers" xsi:type="array">
120+
<item name="default" xsi:type="string">Magento\Sales\Model\ResourceModel\Provider\UpdatedIdListProvider</item>
121+
</argument>
122+
</arguments>
123+
</type>
116124
<type name="Magento\Sales\Model\ResourceModel\Report" shared="false"/>
117125
<type name="Magento\Sales\Model\Order\Pdf\Config\Reader">
118126
<arguments>

0 commit comments

Comments
 (0)