Completed
Pull Request — master (#6)
by Robbie
03:36 queued 21s
created

CwpSolr::configure()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 22
nc 8
nop 0
1
<?php
2
3
namespace CWP\Core\Search;
4
5
use Exception;
6
use InvalidArgumentException;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Config\Configurable;
9
use SilverStripe\Core\Environment;
10
use SilverStripe\FullTextSearch\Solr\Solr;
0 ignored issues
show
Bug introduced by
The type SilverStripe\FullTextSearch\Solr\Solr 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...
11
12
/**
13
 * CwpSolr configures Solr in a CWP-compatible manner.
14
 */
15
class CwpSolr
16
{
17
    use Configurable;
18
19
    /**
20
     *
21
     * @var array
22
     */
23
    private static $options;
0 ignored issues
show
introduced by
The private property $options is not used, and could be removed.
Loading history...
24
25
    /**
26
     * Configure Solr.
27
     *
28
     * $options - An array consisting of:
29
     *
30
     * 'extraspath' - (String) Where to find Solr core configuartion files.
31
     *     Defaults to '<BASE_PATH>/mysite/conf/extras'.
32
     * 'version' - select the Solr configuration to use when in CWP. One of:
33
     * * 'cwp-4': preferred version, uses secured 4.x service available on CWP
34
     * * 'local-4': this can be use for development using silverstripe-localsolr package, 4.x branch
35
     */
36
    public static function configure()
37
    {
38
        if (!class_exists(Solr::class)) {
39
            return;
40
        }
41
42
        // get options from configuration
43
        $options = static::config()->get('options');
44
45
        // get version specific options
46
        switch ($options['version']) {
47
            case 'cwp-4':
48
                $solrOptions = self::options_for_cwp($options);
49
                break;
50
            case 'local-4':
51
                $solrOptions = self::options_for_local($options);
52
                break;
53
            default:
54
                throw new InvalidArgumentException(sprintf(
55
                    'Solr version "%s" is not supported on CWP. Please use "local-4" on local ' .
56
                        'and "cwp-4" on production. For preferred configuration see ' .
57
                        'https://www.cwp.govt.nz/developer-docs/.',
58
                    $options['version']
59
                ));
60
                break;
61
        }
62
63
        // Allow users to override extras path.
64
        // CAUTION: CWP does not permit usage of customised solrconfig.xml.
65
        if (isset($options['extraspath']) && file_exists($options['extraspath'])) {
66
            $solrOptions['extraspath'] = $options['extraspath'];
67
        } elseif (file_exists(BASE_PATH . '/mysite/conf/extras')) {
68
            $solrOptions['extraspath'] = BASE_PATH . '/mysite/conf/extras';
69
        }
70
71
        Solr::configure_server($solrOptions);
72
    }
73
74
    /**
75
     * @throws Exception
76
     */
77
    public static function options_from_environment()
78
    {
79
        throw new Exception(
80
            'CwpSolr::options_from_environment has been deprecated, in favour of implicit Solr ' .
81
            'configuration provided by the CwpSolr class in the cwp-core module. For preferred configuration see ' .
82
            'https://www.cwp.govt.nz/developer-docs/.'
83
        );
84
    }
85
86
    /**
87
     * @param array $options
88
     * @return array
89
     */
90
    public static function options_for_cwp($options)
91
    {
92
        $version = $options['version'];
0 ignored issues
show
Unused Code introduced by
The assignment to $version is dead and can be removed.
Loading history...
93
94
        return [
95
            'host' => Environment::getEnv('SOLR_SERVER'),
96
            'port' => Environment::getEnv('SOLR_PORT'),
97
            'path' => '/v4/',
98
            'version' => 4,
99
            'indexstore' => [
100
                'mode' => CwpSolrConfigStore::class,
101
                'path' => '/v4',
102
            ],
103
        ];
104
    }
105
106
    /**
107
     *
108
     * @param array $options
109
     * @return array
110
     */
111
    public static function options_for_local($options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

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

111
    public static function options_for_local(/** @scrutinizer ignore-unused */ $options)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
        return [
114
            'host' => Environment::getEnv('SOLR_SERVER') ? Environment::getEnv('SOLR_SERVER') : 'localhost',
115
            'port' => Environment::getEnv('SOLR_PORT') ? Environment::getEnv('SOLR_PORT') : 8983,
116
            'path' => Environment::getEnv('SOLR_PATH') ? Environment::getEnv('SOLR_PATH') : '/solr/',
117
            'version' => 4,
118
            'indexstore' => [
119
                'mode' => Environment::getEnv('SOLR_MODE') ? Environment::getEnv('SOLR_MODE') : 'file',
120
                'auth' => Environment::getEnv('SOLR_AUTH') ? Environment::getEnv('SOLR_AUTH') : null,
121
                // Allow storing the solr index and config data in an arbitrary location,
122
                // e.g. outside of the webroot
123
                'path' => Environment::getEnv('SOLR_INDEXSTORE_PATH')
124
                    ? Environment::getEnv('SOLR_INDEXSTORE_PATH')
125
                    : BASE_PATH . '/.solr',
126
                'remotepath' => Environment::getEnv('SOLR_REMOTE_PATH')
127
                    ? Environment::getEnv('SOLR_REMOTE_PATH')
128
                    : null
129
            ]
130
        ];
131
    }
132
}
133