Passed
Push — master ( b4e3f0...d6a119 )
by Robbie
04:16 queued 02:31
created

src/Solr/Tasks/Solr_Configure.php (2 issues)

Labels
Severity
1
<?php
2
namespace SilverStripe\FullTextSearch\Solr\Tasks;
3
4
use Exception;
5
use SilverStripe\Core\ClassInfo;
6
use SilverStripe\FullTextSearch\Solr\Solr;
7
use SilverStripe\FullTextSearch\Solr\Stores\SolrConfigStore_File;
8
use SilverStripe\FullTextSearch\Solr\Stores\SolrConfigStore_Post;
9
use SilverStripe\FullTextSearch\Solr\Stores\SolrConfigStore_WebDAV;
10
use SilverStripe\FullTextSearch\Solr\Stores\SolrConfigStore;
11
12
class Solr_Configure extends Solr_BuildTask
13
{
14
    private static $segment = 'Solr_Configure';
15
    protected $enabled = true;
16
17
    public function run($request)
18
    {
19
        parent::run($request);
20
21
        $this->extend('updateBeforeSolrConfigureTask', $request);
22
23
        // Find the IndexStore handler, which will handle uploading config files to Solr
24
        $store = $this->getSolrConfigStore();
25
26
        $indexes = Solr::get_indexes();
27
        foreach ($indexes as $instance) {
28
            try {
29
                $this->updateIndex($instance, $store);
30
            } catch (Exception $e) {
31
                // We got an exception. Warn, but continue to next index.
32
                $this
33
                    ->getLogger()
34
                    ->error("Failure: " . $e->getMessage());
35
            }
36
        }
37
38
        if (isset($e)) {
39
            exit(1);
40
        }
41
42
        $this->extend('updateAfterSolrConfigureTask', $request);
43
    }
44
45
    /**
46
     * Update the index on the given store
47
     *
48
     * @param SolrIndex $instance Instance
0 ignored issues
show
The type SilverStripe\FullTextSearch\Solr\Tasks\SolrIndex was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
49
     * @param SolrConfigStore $store
50
     */
51
    protected function updateIndex($instance, $store)
52
    {
53
        $index = $instance->getIndexName();
54
        $this->getLogger()->addInfo("Configuring $index.");
0 ignored issues
show
The method addInfo() does not exist on Psr\Log\LoggerInterface. It seems like you code against a sub-type of Psr\Log\LoggerInterface such as Monolog\Logger or Psr\Log\Test\TestLogger. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        $this->getLogger()->/** @scrutinizer ignore-call */ addInfo("Configuring $index.");
Loading history...
55
56
        // Upload the config files for this index
57
        $this->getLogger()->addInfo("Uploading configuration ...");
58
        $instance->uploadConfig($store);
59
60
        // Then tell Solr to use those config files
61
        $service = Solr::service();
62
        if ($service->coreIsActive($index)) {
63
            $this->getLogger()->addInfo("Reloading core ...");
64
            $service->coreReload($index);
65
        } else {
66
            $this->getLogger()->addInfo("Creating core ...");
67
            $service->coreCreate($index, $store->instanceDir($index));
68
        }
69
70
        $this->getLogger()->addInfo("Done");
71
    }
72
73
    /**
74
     * Get config store
75
     *
76
     * @return SolrConfigStore
77
     */
78
    protected function getSolrConfigStore()
79
    {
80
        $options = Solr::solr_options();
81
82
        if (!isset($options['indexstore']) || !($indexstore = $options['indexstore'])) {
83
            throw new Exception('No index configuration for Solr provided', E_USER_ERROR);
84
        }
85
86
        // Find the IndexStore handler, which will handle uploading config files to Solr
87
        $mode = $indexstore['mode'];
88
89
        if ($mode == 'file') {
90
            return new SolrConfigStore_File($indexstore);
91
        } elseif ($mode == 'webdav') {
92
            return new SolrConfigStore_WebDAV($indexstore);
93
        } elseif ($mode == 'post') {
94
            return new SolrConfigStore_Post($indexstore);
95
        } elseif (ClassInfo::exists($mode) && ClassInfo::classImplements($mode, SolrConfigStore::class)) {
96
            return new $mode($indexstore);
97
        } else {
98
            user_error('Unknown Solr index mode ' . $indexstore['mode'], E_USER_ERROR);
99
        }
100
    }
101
}
102