1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package
|
8
|
|
|
*/
|
9
|
|
|
Yii::import('ext.cackle.models.*');
|
10
|
|
|
Yii::import('ext.cackle.components.*');
|
11
|
|
|
|
12
|
|
|
final class CackleSync
|
13
|
|
|
{
|
14
|
|
|
private $limit;
|
15
|
|
|
|
16
|
|
|
/**
|
17
|
|
|
* @var CackleManagerInterface
|
18
|
|
|
*/
|
19
|
|
|
private $manager;
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* @param CackleManagerInterface $manager
|
23
|
|
|
* @param int $limit default 50
|
24
|
|
|
*/
|
25
|
|
|
public function __construct(CackleManagerInterface $manager, $limit = 50)
|
26
|
|
|
{
|
27
|
|
|
$this->manager = $manager;
|
28
|
|
|
$this->limit = $limit;
|
29
|
|
|
}
|
30
|
|
|
|
31
|
|
|
/**
|
32
|
|
|
* @var CackleManagerInterface $manager
|
33
|
|
|
*/
|
34
|
|
|
public function create()
|
35
|
|
|
{
|
36
|
|
|
Yii::app()->getDb()->beginTransaction();
|
37
|
|
|
|
38
|
|
|
$this->manager->clearAll();
|
39
|
|
|
$this->process();
|
40
|
|
|
|
41
|
|
|
Yii::app()->getDb()->currentTransaction->commit();
|
42
|
|
|
}
|
43
|
|
|
|
44
|
|
|
public function update()
|
45
|
|
|
{
|
46
|
|
|
Yii::app()->getDb()->beginTransaction();
|
47
|
|
|
|
48
|
|
|
$this->process($this->manager->getLastModified());
|
49
|
|
|
|
50
|
|
|
Yii::app()->getDb()->currentTransaction->commit();
|
51
|
|
|
}
|
52
|
|
|
|
53
|
|
|
/**
|
54
|
|
|
* @param CackleManagerInterface $manager
|
55
|
|
|
* @param CackleResponseReview[] $items
|
56
|
|
|
* @param array $idsForUpdate
|
57
|
|
|
*/
|
58
|
|
|
protected function saveItems($manager, array $items, $idsForUpdate = array())
|
59
|
|
|
{
|
60
|
|
|
foreach($items as $item)
|
61
|
|
|
{
|
62
|
|
|
if( !isset($idsForUpdate[$item->id]) )
|
|
|
|
|
63
|
|
|
{
|
64
|
|
|
$manager->insert($item);
|
65
|
|
|
}
|
66
|
|
|
else
|
67
|
|
|
{
|
68
|
|
|
$manager->update($item);
|
69
|
|
|
}
|
70
|
|
|
}
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
protected function process($modified = null)
|
74
|
|
|
{
|
75
|
|
|
$idsForUpdate = $this->manager->getIdsForUpdate();
|
76
|
|
|
$defaultItems = $this->manager->getRemoteItems(0, $this->limit, $modified);
|
77
|
|
|
|
78
|
|
|
for($page = 0; $page < $defaultItems->totalPages; $page++)
|
79
|
|
|
{
|
80
|
|
|
$items = $this->manager->getRemoteItems($page, $this->limit, $modified);
|
81
|
|
|
$this->saveItems($this->manager, $items->content, $idsForUpdate);
|
82
|
|
|
}
|
83
|
|
|
}
|
84
|
|
|
} |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: