Passed
Pull Request — develop (#167)
by
unknown
04:28
created

IndexCommandController::flushCommand()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 3
nop 1
crap 6
1
<?php
2
3
namespace Codappix\SearchCore\Command;
4
5
/*
6
 * Copyright (C) 2016  Daniel Siepmann <[email protected]>
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
 * 02110-1301, USA.
22
 */
23
24
use Codappix\SearchCore\Domain\Index\IndexerFactory;
25
use Codappix\SearchCore\Domain\Index\NoMatchingIndexerException;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
28
29
/**
30
 * Command controller to provide indexing through CLI.
31
 */
32
class IndexCommandController extends CommandController
33
{
34
    /**
35
     * @var IndexerFactory
36
     */
37
    protected $indexerFactory;
38
39
    /**
40
     * @param IndexerFactory $factory
41
     * @return void
42
     */
43
    public function injectIndexerFactory(IndexerFactory $factory)
44
    {
45
        $this->indexerFactory = $factory;
46
    }
47
48
    /**
49
     * Will index the given identifier.
50
     *
51
     * @param string $identifier
52
     * @return void
53
     */
54
    public function indexCommand(string $identifier)
55
    {
56
        // Allow multiple identifiers per import task
57
        $identifiers = GeneralUtility::trimExplode(',', $identifier, true);
58
        foreach ($identifiers as $value) {
59
            try {
60
                $this->indexerFactory->getIndexer($value)->indexAllDocuments();
61
                $this->outputLine($value . ' was indexed.');
62
            } catch (NoMatchingIndexerException $e) {
63
                $this->outputLine('No indexer found for: ' . $value);
64
            }
65
        }
66
    }
67
68
    /**
69
     * Will delete the given identifier.
70
     *
71
     * @param string $identifier
72
     * @return void
73
     */
74
    public function deleteCommand(string $identifier)
75
    {
76
        // Allow multiple identifiers per import task
77
        $identifiers = GeneralUtility::trimExplode(',', $identifier, true);
78
        foreach ($identifiers as $value) {
79
            try {
80
                $this->indexerFactory->getIndexer($value)->deleteDocuments();
81
                $this->outputLine($value . ' was deleted.');
82
            } catch (NoMatchingIndexerException $e) {
83
                $this->outputLine('No indexer found for: ' . $value);
84
            }
85
        }
86
    }
87
88
    /**
89
     * Will delete the full index.
90
     *
91
     * @param string $identifier
92
     * @return void
93
     */
94
    public function flushCommand(string $identifier = 'pages')
95
    {
96
        try {
97
            $this->indexerFactory->getIndexer($identifier)->delete();
98
            $this->outputLine('Default configured indices were deleted via ' . $identifier . '.');
99
        } catch (NoMatchingIndexerException $e) {
100
            $this->outputLine('No indexer found for: ' . $identifier);
101
        }
102
    }
103
}
104