Completed
Push — master ( f7252d...955fe9 )
by André
19:56 queued 06:52
created

Indexer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
createSearchIndex() 0 1 ?
A getContentDbFieldsStmt() 0 11 1
A getContentLocationIds() 0 12 2
A logWarning() 0 6 1
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Search\Common;
10
11
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
12
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
13
use eZ\Publish\SPI\Persistence\Handler as PersistenceHandler;
14
use eZ\Publish\SPI\Search\Handler as SearchHandler;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Component\Console\Helper\ProgressBar;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use PDO;
19
20
/**
21
 * Base class for the Search Engine Indexer Service aimed to recreate Search Engine Index.
22
 * Each Search Engine has to extend it on its own.
23
 */
24
abstract class Indexer
25
{
26
    const CONTENTOBJECT_TABLE = 'ezcontentobject';
27
    const CONTENTOBJECT_TREE_TABLE = 'ezcontentobject_tree';
28
29
    /**
30
     * @var \Psr\Log\LoggerInterface
31
     */
32
    protected $logger;
33
34
    /**
35
     * @var \eZ\Publish\SPI\Persistence\Handler
36
     */
37
    protected $persistenceHandler;
38
39
    /**
40
     * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler
41
     */
42
    protected $databaseHandler;
43
44
    /**
45
     * @var \eZ\Publish\SPI\Search\Handler
46
     */
47
    protected $searchHandler;
48
49
    public function __construct(
50
        LoggerInterface $logger,
51
        PersistenceHandler $persistenceHandler,
52
        DatabaseHandler $databaseHandler,
53
        SearchHandler $searchHandler
54
    ) {
55
        $this->logger = $logger;
56
        $this->persistenceHandler = $persistenceHandler;
57
        $this->databaseHandler = $databaseHandler;
58
        $this->searchHandler = $searchHandler;
59
    }
60
61
    /**
62
     * Create search engine index.
63
     *
64
     * @param \Symfony\Component\Console\Output\OutputInterface $output
65
     * @param int $iterationCount
66
     * @param bool $commit commit changes after each iteration
67
     */
68
    abstract public function createSearchIndex(OutputInterface $output, $iterationCount, $commit);
69
70
    /**
71
     * Get PDOStatement to fetch metadata about content objects to be indexed.
72
     *
73
     * @param array $fields Select fields
74
     * @return \PDOStatement
75
     */
76
    protected function getContentDbFieldsStmt(array $fields)
77
    {
78
        $query = $this->databaseHandler->createSelectQuery();
79
        $query->select($fields)
80
            ->from($this->databaseHandler->quoteTable(self::CONTENTOBJECT_TABLE))
81
            ->where($query->expr->eq('status', ContentInfo::STATUS_PUBLISHED));
82
        $stmt = $query->prepare();
83
        $stmt->execute();
84
85
        return $stmt;
86
    }
87
88
    /**
89
     * Fetch location Ids for the given content object.
90
     *
91
     * @param int $contentObjectId
92
     * @return array Location nodes Ids
93
     */
94
    protected function getContentLocationIds($contentObjectId)
95
    {
96
        $query = $this->databaseHandler->createSelectQuery();
97
        $query->select('node_id')
98
            ->from($this->databaseHandler->quoteTable(self::CONTENTOBJECT_TREE_TABLE))
99
            ->where($query->expr->eq('contentobject_id', $contentObjectId));
100
        $stmt = $query->prepare();
101
        $stmt->execute();
102
        $nodeIds = $stmt->fetchAll(PDO::FETCH_COLUMN);
103
104
        return is_array($nodeIds) ? array_map('intval', $nodeIds) : [];
105
    }
106
107
    /**
108
     * Log warning while progress bar is shown.
109
     *
110
     * @param \Symfony\Component\Console\Helper\ProgressBar $progress
111
     * @param $message
112
     */
113
    protected function logWarning(ProgressBar $progress, $message)
114
    {
115
        $progress->clear();
116
        $this->logger->warning($message);
117
        $progress->display();
118
    }
119
}
120