Completed
Pull Request — master (#1311)
by
unknown
10:42
created

AbstractProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 8
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 4
crap 1.0156
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\Provider;
13
14
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
17
/**
18
 * AbstractProvider.
19
 */
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()
115
    {
116
        $this->resolver->setDefaults([
117
            'reset' => true,
118
            'delete' => true,
119
            'batch_size' => 100,
120
            'skip_indexable_check' => false,
121
        ]);
122
123
        $this->resolver->setRequired([
124
            'indexName',
125
            'typeName',
126
        ]);
127
    }
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)
138
    {
139
        if ($options['skip_indexable_check']) {
140
            return $objects;
141
        }
142
143
        $index = $options['indexName'];
144
        $type = $options['typeName'];
145
146
        $return = [];
147
        foreach ($objects as $object) {
148
            if (!$this->indexable->isObjectIndexable($index, $type, $object)) {
149
                continue;
150
            }
151
152
            $return[] = $object;
153
        }
154
155
        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
    protected function resolveOptions(array $options)
167
    {
168
        return $this->resolver->resolve(array_merge($this->baseOptions, $options));
169
    }
170
}
171