SolrConfigStore_Post::uploadFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
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