Completed
Pull Request — 3.1.x (#1038)
by Karel
25:25 queued 15:24
created

AbstractProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.18%

Importance

Changes 8
Bugs 4 Features 0
Metric Value
wmc 12
c 8
b 4
f 0
lcom 1
cbo 2
dl 0
loc 199
ccs 41
cts 44
cp 0.9318
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
disableLogging() 0 1 ?
doPopulate() 0 1 ?
enableLogging() 0 1 ?
A __construct() 0 13 1
A populate() 0 14 3
B configureOptions() 0 29 1
A filterObjects() 0 20 4
A isObjectIndexable() 0 8 1
A getMemoryUsage() 0 7 1
A resolveOptions() 0 4 1
1
<?php
2
3
namespace FOS\ElasticaBundle\Provider;
4
5
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
/**
9
 * AbstractProvider.
10
 */
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 9
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63 9
    public function populate(\Closure $loggerClosure = null, array $options = array())
64
    {
65 9
        $options = $this->resolveOptions($options);
66
67 9
        $logger = !$options['debug_logging'] ?
68 9
            $this->disableLogging() :
69 9
            null;
70
71 9
        $this->doPopulate($options, $loggerClosure);
72
73 8
        if (null !== $logger) {
74
            $this->enableLogging($logger);
75
        }
76 8
    }
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. Some of these options can be ignored if
104
     * implementing this class if they do not have any behaviour for a specific provider.
105 9
     */
106
    protected function configureOptions()
107 9
    {
108 9
        $this->resolver->setDefaults(array(
109 9
            'batch_size' => 100,
110 9
            'clear_object_manager' => true,
111 9
            'debug_logging' => false,
112
            'ignore_errors' => false,
113 9
            'offset' => 0,
114
            'query_builder_method' => 'createQueryBuilder',
115 9
            'skip_indexable_check' => false,
116 9
            'sleep' => 0
117 9
        ));
118 9
        $this->resolver->setRequired(array(
119 9
            'indexName',
120
            'typeName',
121
        ));
122
        $this->resolver->setAllowedTypes(array(
0 ignored issues
show
Documentation introduced by
array('batch_size' => 'i...ool', 'sleep' => 'int') is of type array<string,string,{"ba...ing","sleep":"string"}>, but the function expects a string.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
            'batch_size' => 'int',
124
            'clear_object_manager' => 'bool',
125
            'debug_logging' => 'bool',
126
            'ignore_errors' => 'bool',
127
            'indexName' => 'string',
128
            'typeName' => 'string',
129 9
            'offset' => 'int',
130
            'query_builder_method' => 'string',
131 9
            'skip_indexable_check' => 'bool',
132
            'sleep' => 'int'
133
        ));
134
    }
135 9
136 9
    /**
137
     * Filters objects away if they are not indexable.
138 9
     *
139 9
     * @param array $options
140 9
     * @param array $objects
141 2
     * @return array
142
     */
143
    protected function filterObjects(array $options, array $objects)
144 8
    {
145 9
        if ($options['skip_indexable_check']) {
146
            return $objects;
147 9
        }
148
149
        $index = $options['indexName'];
150
        $type = $options['typeName'];
151
152
        $return = array();
153
        foreach ($objects as $object) {
154
            if (!$this->indexable->isObjectIndexable($index, $type, $object)) {
155
                continue;
156
            }
157
158
            $return[] = $object;
159
        }
160
161
        return $return;
162
    }
163
164
    /**
165
     * Checks if a given object should be indexed or not.
166
     *
167
     * @deprecated To be removed in 4.0
168
     *
169
     * @param object $object
170
     *
171
     * @return bool
172
     */
173
    protected function isObjectIndexable($object)
174
    {
175
        return $this->indexable->isObjectIndexable(
176
            $this->baseOptions['indexName'],
177
            $this->baseOptions['typeName'],
178
            $object
179
        );
180
    }
181
182
    /**
183
     * Get string with RAM usage information (current and peak).
184
     *
185
     * @deprecated To be removed in 4.0
186
     *
187
     * @return string
188
     */
189
    protected function getMemoryUsage()
190
    {
191 9
        $memory = round(memory_get_usage() / (1024 * 1024)); // to get usage in Mo
192
        $memoryMax = round(memory_get_peak_usage() / (1024 * 1024)); // to get max usage in Mo
193 9
194
        return sprintf('(RAM : current=%uMo peak=%uMo)', $memory, $memoryMax);
195
    }
196
197
    /**
198
     * Merges the base options provided by the class with options passed to the populate
199
     * method and runs them through the resolver.
200
     *
201
     * @param array $options
202
     *
203
     * @return array
204
     */
205
    protected function resolveOptions(array $options)
206
    {
207
        return $this->resolver->resolve(array_merge($this->baseOptions, $options));
208
    }
209
}
210