Passed
Pull Request — develop (#167)
by Daniel
04:49
created

IndexCommandController::deleteDocumentsCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\IndexerInterface;
26
use Codappix\SearchCore\Domain\Index\NoMatchingIndexerException;
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
29
30
/**
31
 * Command controller to provide indexing through CLI.
32
 */
33
class IndexCommandController extends CommandController
34
{
35
    /**
36
     * @var IndexerFactory
37
     */
38
    protected $indexerFactory;
39
40
    /**
41
     * @param IndexerFactory $factory
42
     */
43
    public function injectIndexerFactory(IndexerFactory $factory)
44
    {
45
        $this->indexerFactory = $factory;
46
    }
47
48
    /**
49
     * Will index all documents for the given identifiers.
50
     *
51
     * @param string $identifier Comma separated list of identifiers.
52
     */
53
    public function indexCommand(string $identifiers)
54
    {
55
        $this->executeForIdentifier($identifiers, function (IndexerInterface $indexer) {
56
            $indexer->indexAllDocuments();
57
            $this->outputLine('Documents in index ' . $indexer->getIdentifier() . ' were indexed.');
58
        });
59
    }
60
61
    /**
62
     * Will delete all indexed documents for the given identifiers.
63
     *
64
     * @param string $identifier Comma separated list of identifiers.
65
     */
66
    public function deleteDocumentsCommand(string $identifiers)
67
    {
68
        $this->executeForIdentifier($identifiers, function (IndexerInterface $indexer) {
69
            $indexer->deleteAllDocuments();
70
            $this->outputLine('Documents in index ' . $indexer->getIdentifier() . ' were deleted.');
71
        });
72
    }
73
74
    /**
75
     * Will delete the index for given identifiers.
76
     *
77
     * @param string $identifier Comma separated list of identifiers.
78
     */
79
    public function deleteCommand(string $identifiers = 'pages')
80
    {
81
        $this->executeForIdentifier($identifiers, function (IndexerInterface $indexer) {
82
            $indexer->delete();
83
            $this->outputLine('Index ' . $indexer->getIdentifier() . ' was deleted.');
84
        });
85
    }
86
87
    /**
88
     * Executes the given callback method for each provided identifier.
89
     *
90
     * An indexer is created for each identifier, which is provided as first argument to the callback.
91
     */
92
    private function executeForIdentifier(string $identifiers, callable $callback)
93
    {
94
        foreach (GeneralUtility::trimExplode(',', $identifiers, true) as $identifier) {
95
            try {
96
                $callback($this->indexerFactory->getIndexer($identifier));
97
            } catch (NoMatchingIndexerException $e) {
98
                $this->outputLine('No indexer found for: ' . $identifier . '.');
99
            }
100
        }
101
    }
102
}
103