1 | <?php |
||
11 | abstract class AbstractProvider implements ProviderInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $baseOptions; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $objectClass; |
||
22 | |||
23 | /** |
||
24 | * @var ObjectPersisterInterface |
||
25 | */ |
||
26 | protected $objectPersister; |
||
27 | |||
28 | /** |
||
29 | * @var OptionsResolver |
||
30 | */ |
||
31 | protected $resolver; |
||
32 | |||
33 | /** |
||
34 | * @var IndexableInterface |
||
35 | */ |
||
36 | private $indexable; |
||
37 | |||
38 | /** |
||
39 | * Constructor. |
||
40 | * |
||
41 | * @param ObjectPersisterInterface $objectPersister |
||
42 | * @param IndexableInterface $indexable |
||
43 | * @param string $objectClass |
||
44 | * @param array $baseOptions |
||
45 | */ |
||
46 | 9 | public function __construct( |
|
47 | ObjectPersisterInterface $objectPersister, |
||
48 | IndexableInterface $indexable, |
||
49 | $objectClass, |
||
50 | array $baseOptions = array() |
||
51 | ) { |
||
52 | 9 | $this->baseOptions = $baseOptions; |
|
53 | 9 | $this->indexable = $indexable; |
|
54 | 9 | $this->objectClass = $objectClass; |
|
55 | 9 | $this->objectPersister = $objectPersister; |
|
56 | 9 | $this->resolver = new OptionsResolver(); |
|
57 | 9 | $this->configureOptions(); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * {@inheritDoc} |
||
62 | */ |
||
63 | public function populate(\Closure $loggerClosure = null, array $options = array()) |
||
64 | { |
||
65 | $options = $this->resolveOptions($options); |
||
66 | |||
67 | $logger = !$options['debug_logging'] ? |
||
68 | $this->disableLogging() : |
||
69 | null; |
||
70 | |||
71 | $this->doPopulate($options, $loggerClosure); |
||
72 | |||
73 | if (null !== $logger) { |
||
74 | $this->enableLogging($logger); |
||
75 | } |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Disables logging and returns the logger that was previously set. |
||
80 | * |
||
81 | * @return mixed |
||
82 | */ |
||
83 | abstract protected function disableLogging(); |
||
84 | |||
85 | /** |
||
86 | * Perform actual population. |
||
87 | * |
||
88 | * @param array $options |
||
89 | * @param \Closure $loggerClosure |
||
90 | */ |
||
91 | abstract protected function doPopulate($options, \Closure $loggerClosure = null); |
||
92 | |||
93 | /** |
||
94 | * Reenables the logger with the previously returned logger from disableLogging();. |
||
95 | * |
||
96 | * @param mixed $logger |
||
97 | * |
||
98 | * @return mixed |
||
99 | */ |
||
100 | abstract protected function enableLogging($logger); |
||
101 | |||
102 | /** |
||
103 | * Configures the option resolver. |
||
104 | */ |
||
105 | 9 | protected function configureOptions() |
|
106 | { |
||
107 | 9 | $this->resolver->setDefaults(array( |
|
108 | 9 | 'batch_size' => 100, |
|
109 | 'skip_indexable_check' => false, |
||
110 | )); |
||
111 | 9 | $this->resolver->setAllowedTypes(array( |
|
112 | 'batch_size' => 'int' |
||
113 | 9 | )); |
|
114 | |||
115 | $this->resolver->setRequired(array( |
||
116 | 'indexName', |
||
117 | 'typeName', |
||
118 | )); |
||
119 | } |
||
120 | |||
121 | |||
122 | /** |
||
123 | * Filters objects away if they are not indexable. |
||
124 | * |
||
125 | * @param array $options |
||
126 | * @param array $objects |
||
127 | * @return array |
||
128 | */ |
||
129 | protected function filterObjects(array $options, array $objects) |
||
130 | { |
||
131 | if ($options['skip_indexable_check']) { |
||
132 | return $objects; |
||
133 | } |
||
134 | |||
135 | $index = $options['indexName']; |
||
136 | $type = $options['typeName']; |
||
137 | |||
138 | $return = array(); |
||
139 | foreach ($objects as $object) { |
||
140 | if (!$this->indexable->isObjectIndexable($index, $type, $object)) { |
||
141 | continue; |
||
142 | } |
||
143 | |||
144 | $return[] = $object; |
||
145 | } |
||
146 | |||
147 | return $return; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Checks if a given object should be indexed or not. |
||
152 | * |
||
153 | * @deprecated To be removed in 4.0 |
||
154 | * |
||
155 | * @param object $object |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | protected function isObjectIndexable($object) |
||
167 | |||
168 | /** |
||
169 | * Get string with RAM usage information (current and peak). |
||
170 | * |
||
171 | * @deprecated To be removed in 4.0 |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | protected function getMemoryUsage() |
||
182 | |||
183 | /** |
||
184 | * Merges the base options provided by the class with options passed to the populate |
||
185 | * method and runs them through the resolver. |
||
186 | * |
||
187 | * @param array $options |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | protected function resolveOptions(array $options) |
||
195 | } |
||
196 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: