Completed
Push — master ( 686084...a2bb6b )
by
unknown
9s
created

CwpSolrConfigStore   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 65
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A uploadFile() 0 3 1
A uploadString() 0 9 1
A instanceDir() 0 3 2
1
<?php
2
3
namespace CWP\Search\Solr;
4
5
use SilverStripe\FullTextSearch\Solr\Solr;
6
use SilverStripe\FullTextSearch\Solr\Stores\SolrConfigStore;
7
8
/**
9
 * Class CwpSolrConfigStore
10
 *
11
 * Uploads configuration to Solr via the PHP proxy CWP uses to filter requests
12
 */
13
class CwpSolrConfigStore implements SolrConfigStore
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $remote = '';
19
20
    /**
21
     * @var string
22
     */
23
    protected $url = '';
24
25
    /**
26
     * @param array $config
27
     */
28
    public function __construct($config)
29
    {
30
        $options = Solr::solr_options();
31
32
        $this->url = implode('', [
33
            'http://',
34
            isset($config['auth']) ? $config['auth'] . '@' : '',
35
            $options['host'] . ':' . $options['port'],
36
            $config['path']
37
        ]);
38
        $this->remote = $config['remotepath'];
39
    }
40
41
    /**
42
     * @param string $index
43
     * @param string $file
44
     * @return void
45
     */
46
    public function uploadFile($index, $file)
47
    {
48
        $this->uploadString($index, basename($file), file_get_contents($file));
49
    }
50
51
    /**
52
     *
53
     * @param string $index
54
     * @param string $filename
55
     * @param string $string
56
     * @return void
57
     */
58
    public function uploadString($index, $filename, $string)
59
    {
60
        $targetDir = "{$this->url}/config/$index";
61
62
        file_get_contents($targetDir . '/' . $filename, false, stream_context_create([
63
            'http' => [
64
                'method' => 'POST',
65
                'header' => 'Content-type: application/octet-stream',
66
                'content' => (string) $string
67
            ]
68
        ]));
69
    }
70
71
    /**
72
     * @param string $index
73
     * @return string
74
     */
75
    public function instanceDir($index)
76
    {
77
        return $this->remote ? "{$this->remote}/$index" : $index;
78
    }
79
}
80