Completed
Push — master ( 6eb08a...7e2c22 )
by Karel
02:55
created

AbstractProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 2
dl 0
loc 151
ccs 33
cts 36
cp 0.9167
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A populate() 0 14 3
disableLogging() 0 1 ?
doPopulate() 0 1 ?
enableLogging() 0 1 ?
A configureOptions() 0 14 1
A filterObjects() 0 20 4
A resolveOptions() 0 4 1
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 9
        $this->configureOptions();
67 9
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 9
    public function populate(\Closure $loggerClosure = null, array $options = [])
73
    {
74 9
        $options = $this->resolveOptions($options);
75
76 9
        $logger = !$options['debug_logging'] ?
77
            $this->disableLogging() :
78 9
            null;
79
80 9
        $this->doPopulate($options, $loggerClosure);
81
82 8
        if (null !== $logger) {
83
            $this->enableLogging($logger);
84
        }
85 8
    }
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 9
    protected function configureOptions()
115
    {
116 9
        $this->resolver->setDefaults([
117 9
            'reset' => true,
118
            'delete' => true,
119
            'batch_size' => 100,
120
            'skip_indexable_check' => false,
121
        ]);
122
123 9
        $this->resolver->setRequired([
124 9
            'indexName',
125
            'typeName',
126
        ]);
127 9
    }
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 9
    protected function filterObjects(array $options, array $objects)
138
    {
139 9
        if ($options['skip_indexable_check']) {
140
            return $objects;
141
        }
142
143 9
        $index = $options['indexName'];
144 9
        $type = $options['typeName'];
145
146 9
        $return = [];
147 9
        foreach ($objects as $object) {
148 9
            if (!$this->indexable->isObjectIndexable($index, $type, $object)) {
149 2
                continue;
150
            }
151
152 8
            $return[] = $object;
153
        }
154
155 9
        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 9
    protected function resolveOptions(array $options)
167
    {
168 9
        return $this->resolver->resolve(array_merge($this->baseOptions, $options));
169
    }
170
}
171