Completed
Pull Request — master (#1020)
by Eugene
06:11
created

AbstractProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 4
crap 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
            $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.
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
112 9
        $this->resolver->setRequired(array(
113 9
            'indexName',
114
            'typeName',
115
        ));
116 9
    }
117
118
119
    /**
120
     * Filters objects away if they are not indexable.
121
     *
122
     * @param array $options
123
     * @param array $objects
124
     * @return array
125
     */
126 9
    protected function filterObjects(array $options, array $objects)
127
    {
128 9
        if ($options['skip_indexable_check']) {
129
            return $objects;
130
        }
131
132 9
        $index = $options['indexName'];
133 9
        $type = $options['typeName'];
134
135 9
        $return = array();
136 9
        foreach ($objects as $object) {
137 9
            if (!$this->indexable->isObjectIndexable($index, $type, $object)) {
138 2
                continue;
139
            }
140
141 8
            $return[] = $object;
142
        }
143
144 9
        return $return;
145
    }
146
147
    /**
148
     * Checks if a given object should be indexed or not.
149
     *
150
     * @deprecated To be removed in 4.0
151
     *
152
     * @param object $object
153
     *
154
     * @return bool
155
     */
156
    protected function isObjectIndexable($object)
157
    {
158
        return $this->indexable->isObjectIndexable(
159
            $this->baseOptions['indexName'],
160
            $this->baseOptions['typeName'],
161
            $object
162
        );
163
    }
164
165
    /**
166
     * Get string with RAM usage information (current and peak).
167
     *
168
     * @deprecated To be removed in 4.0
169
     *
170
     * @return string
171
     */
172
    protected function getMemoryUsage()
173
    {
174
        $memory = round(memory_get_usage() / (1024 * 1024)); // to get usage in Mo
175
        $memoryMax = round(memory_get_peak_usage() / (1024 * 1024)); // to get max usage in Mo
176
177
        return sprintf('(RAM : current=%uMo peak=%uMo)', $memory, $memoryMax);
178
    }
179
180
    /**
181
     * Merges the base options provided by the class with options passed to the populate
182
     * method and runs them through the resolver.
183
     *
184
     * @param array $options
185
     *
186
     * @return array
187
     */
188 9
    protected function resolveOptions(array $options)
189
    {
190 9
        return $this->resolver->resolve(array_merge($this->baseOptions, $options));
191
    }
192
}
193