Completed
Push — develop ( c60de3...4fc294 )
by Susi
05:38
created

DocumentTypeService::deleteDocumentsOfType()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 14
nc 2
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace T3G\Elasticorn\Service;
5
6
7
use Elastica\Client;
8
use Elastica\Query;
9
use Psr\Log\LoggerInterface;
10
use T3G\Elasticorn\Configuration\ApplicationConfiguration;
11
12
class DocumentTypeService
13
{
14
    /**
15
     * @var \Elastica\Client
16
     */
17
    protected $client;
18
19
    /**
20
     * @var \Psr\Log\LoggerInterface
21
     */
22
    protected $logger;
23
24
    /**
25
     * @var string
26
     */
27
    private $typeName;
28
    /**
29
     * @var string
30
     */
31
    private $indexName;
32
33
    /**
34
     * DocumentTypeService constructor.
35
     *
36
     * @param \Elastica\Client $client
37
     * @param \Psr\Log\LoggerInterface $logger
38
     * @param string $indexName
39
     * @param string $typeName
40
     */
41
    public function __construct(
42
        Client $client,
43
        LoggerInterface $logger,
44
        string $indexName,
45
        string $typeName
46
    ) {
47
        $this->client = $client;
48
        $this->logger = $logger;
49
        $this->indexName = $indexName;
50
        $this->typeName = $typeName;
51
    }
52
53
    public function deleteDocumentsOfType()
54
    {
55
        $this->logger->info('Removing all documents of type ' . $this->typeName);
56
        $languages = ApplicationConfiguration::getLanguageConfiguration();
57
        if (count($languages) > 0) {
58
            foreach ($languages as $language) {
59
                $index = $this->client->getIndex($this->indexName . '_' . $language);
60
                $this->logger->info('Removing documents of type ' . $this->typeName . ' from index ' . $index);
61
                $index->getType($this->typeName)->deleteByQuery(
62
                   new Query\MatchAll()
0 ignored issues
show
Documentation introduced by
new \Elastica\Query\MatchAll() is of type object<Elastica\Query\MatchAll>, but the function expects a object<Elastica\Query>|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...
63
               );
64
                $index->refresh();
65
            }
66
67
        } else {
68
            $this->client->getIndex($this->indexName)->getType($this->typeName)->deleteByQuery(new Query\MatchAll());
0 ignored issues
show
Documentation introduced by
new \Elastica\Query\MatchAll() is of type object<Elastica\Query\MatchAll>, but the function expects a object<Elastica\Query>|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...
69
            $this->client->getIndex($this->indexName)->refresh();
70
        }
71
        $this->logger->info('Removed all documents of type ' . $this->typeName);
72
    }
73
}