1 | <?php |
||
20 | abstract class AbstractProvider implements ProviderInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $baseOptions; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $objectClass; |
||
31 | |||
32 | /** |
||
33 | * @var ObjectPersisterInterface |
||
34 | */ |
||
35 | protected $objectPersister; |
||
36 | |||
37 | /** |
||
38 | * @var OptionsResolver |
||
39 | */ |
||
40 | protected $resolver; |
||
41 | |||
42 | /** |
||
43 | * @var IndexableInterface |
||
44 | */ |
||
45 | private $indexable; |
||
46 | |||
47 | /** |
||
48 | * Constructor. |
||
49 | * |
||
50 | * @param ObjectPersisterInterface $objectPersister |
||
51 | * @param IndexableInterface $indexable |
||
52 | * @param string $objectClass |
||
53 | * @param array $baseOptions |
||
54 | */ |
||
55 | 9 | public function __construct( |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 9 | public function populate(\Closure $loggerClosure = null, array $options = []) |
|
73 | { |
||
74 | 9 | $options = $this->resolveOptions($options); |
|
75 | |||
76 | 9 | $logger = !$options['debug_logging'] ? |
|
77 | 9 | $this->disableLogging() : |
|
78 | 9 | null; |
|
79 | |||
80 | 9 | $this->doPopulate($options, $loggerClosure); |
|
81 | |||
82 | 8 | if (null !== $logger) { |
|
83 | $this->enableLogging($logger); |
||
84 | } |
||
85 | 8 | } |
|
86 | |||
87 | /** |
||
88 | * Disables logging and returns the logger that was previously set. |
||
89 | * |
||
90 | * @return mixed |
||
91 | */ |
||
92 | abstract protected function disableLogging(); |
||
93 | |||
94 | /** |
||
95 | * Perform actual population. |
||
96 | * |
||
97 | * @param array $options |
||
98 | * @param \Closure $loggerClosure |
||
99 | */ |
||
100 | abstract protected function doPopulate($options, \Closure $loggerClosure = null); |
||
101 | |||
102 | /** |
||
103 | * Reenables the logger with the previously returned logger from disableLogging();. |
||
104 | * |
||
105 | * @param mixed $logger |
||
106 | * |
||
107 | * @return mixed |
||
108 | */ |
||
109 | abstract protected function enableLogging($logger); |
||
110 | |||
111 | /** |
||
112 | * Configures the option resolver. |
||
113 | */ |
||
114 | 9 | protected function configureOptions() |
|
115 | { |
||
116 | 9 | $this->resolver->setDefaults([ |
|
117 | 9 | 'reset' => true, |
|
118 | 9 | 'delete' => true, |
|
119 | 9 | 'batch_size' => 100, |
|
120 | 9 | 'skip_indexable_check' => false, |
|
121 | 9 | ]); |
|
122 | |||
123 | 9 | $this->resolver->setRequired([ |
|
124 | 9 | 'indexName', |
|
125 | 9 | 'typeName', |
|
126 | 9 | ]); |
|
127 | 9 | } |
|
128 | |||
129 | /** |
||
130 | * Filters objects away if they are not indexable. |
||
131 | * |
||
132 | * @param array $options |
||
133 | * @param array $objects |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | 9 | protected function filterObjects(array $options, array $objects) |
|
138 | { |
||
139 | 9 | if ($options['skip_indexable_check']) { |
|
140 | return $objects; |
||
141 | } |
||
142 | |||
143 | 9 | $index = $options['indexName']; |
|
144 | 9 | $type = $options['typeName']; |
|
145 | |||
146 | 9 | $return = []; |
|
147 | 9 | foreach ($objects as $object) { |
|
148 | 9 | if (!$this->indexable->isObjectIndexable($index, $type, $object)) { |
|
149 | 2 | continue; |
|
150 | } |
||
151 | |||
152 | 8 | $return[] = $object; |
|
153 | 9 | } |
|
154 | |||
155 | 9 | return $return; |
|
156 | } |
||
157 | |||
158 | /** |
||
159 | * Merges the base options provided by the class with options passed to the populate |
||
160 | * method and runs them through the resolver. |
||
161 | * |
||
162 | * @param array $options |
||
163 | * |
||
164 | * @return array |
||
165 | */ |
||
166 | 9 | protected function resolveOptions(array $options) |
|
170 | } |
||
171 |