Passed
Pull Request — master (#246)
by Jake Dale
03:39
created

SolrConfigStore_Post::instanceDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
        $this->remote = $config['remotepath'];
34
    }
35
36
    /**
37
     * @param string $index
38
     * @param string $file
39
     * @return void
40
     */
41
    public function uploadFile($index, $file)
42
    {
43
        $this->uploadString($index, basename($file), file_get_contents($file));
44
    }
45
46
    /**
47
     *
48
     * @param string $index
49
     * @param string $filename
50
     * @param string $string
51
     * @return void
52
     */
53
    public function uploadString($index, $filename, $string)
54
    {
55
        $targetDir = "{$this->url}/config/$index";
56
57
        file_get_contents($targetDir . '/' . $filename, false, stream_context_create([
58
            'http' => [
59
                'method' => 'POST',
60
                'header' => 'Content-type: application/octet-stream',
61
                'content' => (string) $string
62
            ]
63
        ]));
64
    }
65
66
    /**
67
     * @param string $index
68
     * @return string
69
     */
70
    public function instanceDir($index)
71
    {
72
        return $this->remote ? "{$this->remote}/$index" : $index;
73
    }
74
}
75