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( |
|
56 | ObjectPersisterInterface $objectPersister, |
||
57 | IndexableInterface $indexable, |
||
58 | $objectClass, |
||
59 | array $baseOptions = [] |
||
60 | ) { |
||
61 | 9 | $this->baseOptions = $baseOptions; |
|
62 | 9 | $this->indexable = $indexable; |
|
63 | 9 | $this->objectClass = $objectClass; |
|
64 | 9 | $this->objectPersister = $objectPersister; |
|
65 | 9 | $this->resolver = new OptionsResolver(); |
|
66 | $this->configureOptions(); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | public function populate(\Closure $loggerClosure = null, array $options = []) |
||
73 | { |
||
74 | $options = $this->resolveOptions($options); |
||
75 | |||
76 | $logger = !$options['debug_logging'] ? |
||
77 | $this->disableLogging() : |
||
78 | null; |
||
79 | |||
80 | $this->doPopulate($options, $loggerClosure); |
||
81 | |||
82 | if (null !== $logger) { |
||
83 | $this->enableLogging($logger); |
||
84 | } |
||
85 | } |
||
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 | protected function configureOptions() |
||
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 | protected function filterObjects(array $options, array $objects) |
||
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 | protected function resolveOptions(array $options) |
||
170 | } |
||
171 |