SolrConfigStore_Post   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 19
c 3
b 0
f 0
dl 0
loc 68
rs 10
wmc 7

4 Methods

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