CwpSolrConfigStore::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
c 2
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 2
nop 1
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
 * @deprecated 1.2.0 Use SolrConfigStore_Post in silverstripe/fulltextsearch instead
10
 */
11
class CwpSolrConfigStore implements SolrConfigStore
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $remote = '';
17
18
    /**
19
     * @var string
20
     */
21
    protected $url = '';
22
23
    /**
24
     * @param array $config
25
     */
26
    public function __construct($config)
27
    {
28
        $options = Solr::solr_options();
29
30
        $this->url = implode('', [
31
            'http://',
32
            isset($config['auth']) ? $config['auth'] . '@' : '',
33
            $options['host'] . ':' . $options['port'],
34
            $config['path']
35
        ]);
36
37
        if (isset($config['remotepath'])) {
38
            $this->remote = $config['remotepath'];
39
        }
40
    }
41
42
    /**
43
     * @param string $index
44
     * @param string $file
45
     * @return void
46
     */
47
    public function uploadFile($index, $file)
48
    {
49
        $this->uploadString($index, basename($file), file_get_contents($file));
50
    }
51
52
    /**
53
     *
54
     * @param string $index
55
     * @param string $filename
56
     * @param string $string
57
     * @return void
58
     */
59
    public function uploadString($index, $filename, $string)
60
    {
61
        $targetDir = "{$this->url}/config/$index";
62
63
        file_get_contents($targetDir . '/' . $filename, false, stream_context_create([
64
            'http' => [
65
                'method' => 'POST',
66
                'header' => 'Content-type: application/octet-stream',
67
                'content' => (string) $string
68
            ]
69
        ]));
70
    }
71
72
    /**
73
     * @param string $index
74
     * @return string
75
     */
76
    public function instanceDir($index)
77
    {
78
        return $this->remote ? "{$this->remote}/$index" : $index;
79
    }
80
}
81