/
Binding.php
163 lines (139 loc) · 3.43 KB
/
Binding.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace chervand\sync;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\base\Model;
/**
* Class Binding implements a sync logic between two entities.
* @package chervand\sync
*/
abstract class Binding extends Component implements BindingInterface
{
const DIRECTION_MODEL = 0;
const DIRECTION_RELATED = 1;
const EVENT_BEFORE_SYNC = 'beforeSync';
const EVENT_AFTER_SYNC = 'afterSync';
/**
* @var string
*/
public $id;
/**
* @var \yii\base\Model
*/
public $model;
/**
* @var callable|null
*/
public $sync;
/**
* @var callable|null
*/
public $attributes;
/**
* @var callable|null
*/
public $commit;
/**
* @var callable|integer|null
*/
public $direction;
/**
* Init.
* @throws InvalidConfigException
*/
public function init()
{
parent::init();
if (!isset($this->id)) {
throw new InvalidConfigException(get_class($this) . '::$id');
}
if (!$this->model instanceof Model) {
throw new InvalidConfigException(get_class($this) . '::$model');
}
$this->model->setScenario('sync');
}
/**
* @inheritdoc
*/
public function sync()
{
$synced = false;
if ($this->beforeSync()) {
if (is_callable($this->sync)) {
$synced = call_user_func($this->sync, $this);
} else {
$synced = $this->syncInternal();
}
$this->afterSync();
}
Yii::info($this->id . ': ' . implode(', ', [
get_class($this->model),
$synced ? 'OK' : 'FAILED'
]), __METHOD__);
return $synced;
}
protected function beforeSync()
{
$event = new SyncEvent(['binding' => $this]);
$this->trigger(self::EVENT_BEFORE_SYNC, $event);
return $event->isValid;
}
/**
* @return bool
*/
protected abstract function syncInternal();
protected function afterSync()
{
$this->trigger(self::EVENT_AFTER_SYNC, new SyncEvent([
'binding' => $this
]));
}
/**
* @inheritdoc
*/
public function attributes(&$changed)
{
if (is_callable($this->attributes)) {
return call_user_func($this->attributes, $changed);
}
return $this->attributesInternal($changed);
}
/**
* @param \yii\base\Model $changed
* @return array
*/
protected abstract function attributesInternal($changed);
/**
* @inheritdoc
*/
public function commit(&$model, &$attributes)
{
if ($this->commit instanceof \Closure) {
return call_user_func($this->commit, $model, $attributes);
}
return $this->commitInternal($model, $attributes);
}
/**
* @param \yii\base\Model $model
* @param array $attributes
* @return bool
*/
protected abstract function commitInternal($model, $attributes);
/**
* @inheritdoc
*/
public function direction(&$model, &$related)
{
if (is_callable($this->direction)) {
return call_user_func($this->direction, $model);
}
if (in_array($this->direction, [
self::DIRECTION_MODEL,
self::DIRECTION_RELATED,
])) {
return $this->direction;
}
return null;
}
}