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

src/Solr/Tasks/Solr_Configure.php (3 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\SolrIndex;
8
use SilverStripe\FullTextSearch\Solr\Stores\SolrConfigStore;
9
use SilverStripe\FullTextSearch\Solr\Stores\SolrConfigStore_File;
10
use SilverStripe\FullTextSearch\Solr\Stores\SolrConfigStore_Post;
11
use SilverStripe\FullTextSearch\Solr\Stores\SolrConfigStore_WebDAV;
12
13
class Solr_Configure extends Solr_BuildTask
14
{
15
    private static $segment = 'Solr_Configure';
16
    protected $enabled = true;
17
18
    public function run($request)
19
    {
20
        parent::run($request);
21
22
        $this->extend('updateBeforeSolrConfigureTask', $request);
23
24
        // Find the IndexStore handler, which will handle uploading config files to Solr
25
        $store = $this->getSolrConfigStore();
26
27
        $indexes = Solr::get_indexes();
28
        foreach ($indexes as $instance) {
29
            try {
30
                $this->updateIndex($instance, $store);
31
            } catch (Exception $e) {
32
                // We got an exception. Warn, but continue to next index.
33
                $this
34
                    ->getLogger()
35
                    ->error("Failure: " . $e->getMessage());
36
            }
37
        }
38
39
        if (isset($e)) {
40
            exit(1);
41
        }
42
43
        $this->extend('updateAfterSolrConfigureTask', $request);
44
    }
45
46
    /**
47
     * Update the index on the given store
48
     *
49
     * @param SolrIndex $instance Instance
50
     * @param SolrConfigStore $store
51
     */
52
    protected function updateIndex($instance, $store)
53
    {
54
        $index = $instance->getIndexName();
55
        $this->getLogger()->info("Configuring $index.");
56
57
        // Upload the config files for this index
58
        $this->getLogger()->info("Uploading configuration ...");
59
        $instance->uploadConfig($store);
60
61
        // Then tell Solr to use those config files
62
        $service = Solr::service();
63
        if ($service->coreIsActive($index)) {
0 ignored issues
show
The method coreIsActive() does not exist on SilverStripe\FullTextSea...rvices\SolrService_Core. It seems like you code against a sub-type of SilverStripe\FullTextSea...rvices\SolrService_Core such as SilverStripe\FullTextSea...lr\Services\SolrService. ( Ignorable by Annotation )

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

63
        if ($service->/** @scrutinizer ignore-call */ coreIsActive($index)) {
Loading history...
64
            $this->getLogger()->info("Reloading core ...");
65
            $service->coreReload($index);
0 ignored issues
show
The method coreReload() does not exist on SilverStripe\FullTextSea...rvices\SolrService_Core. It seems like you code against a sub-type of SilverStripe\FullTextSea...rvices\SolrService_Core such as SilverStripe\FullTextSea...lr\Services\SolrService. ( Ignorable by Annotation )

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

65
            $service->/** @scrutinizer ignore-call */ 
66
                      coreReload($index);
Loading history...
66
        } else {
67
            $this->getLogger()->info("Creating core ...");
68
            $service->coreCreate($index, $store->instanceDir($index));
0 ignored issues
show
The method coreCreate() does not exist on SilverStripe\FullTextSea...rvices\SolrService_Core. It seems like you code against a sub-type of SilverStripe\FullTextSea...rvices\SolrService_Core such as SilverStripe\FullTextSea...lr\Services\SolrService. ( Ignorable by Annotation )

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

68
            $service->/** @scrutinizer ignore-call */ 
69
                      coreCreate($index, $store->instanceDir($index));
Loading history...
69
        }
70
71
        $this->getLogger()->info("Done");
72
    }
73
74
    /**
75
     * Get config store
76
     *
77
     * @return SolrConfigStore
78
     * @throws Exception
79
     */
80
    protected function getSolrConfigStore()
81
    {
82
        $options = Solr::solr_options();
83
84
        if (!isset($options['indexstore']) || !($indexstore = $options['indexstore'])) {
85
            throw new Exception('No index configuration for Solr provided', E_USER_ERROR);
86
        }
87
88
        // Find the IndexStore handler, which will handle uploading config files to Solr
89
        $mode = $indexstore['mode'];
90
91
        if ($mode === 'file') {
92
            return new SolrConfigStore_File($indexstore);
93
        }
94
        if ($mode === 'webdav') {
95
            return new SolrConfigStore_WebDAV($indexstore);
96
        }
97
        if ($mode === 'post') {
98
            return new SolrConfigStore_Post($indexstore);
99
        }
100
        if (ClassInfo::exists($mode) && ClassInfo::classImplements($mode, SolrConfigStore::class)) {
101
            return new $mode($indexstore);
102
        }
103
        user_error('Unknown Solr index mode ' . $indexstore['mode'], E_USER_ERROR);
104
    }
105
}
106