Passed
Push — sheepy/elevation-configuration ( bdeab0...bcf7bc )
by Marco
18:34
created

PostConfigStore   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 27
c 1
b 0
f 0
dl 0
loc 90
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPath() 0 3 1
A __construct() 0 14 4
A uploadString() 0 22 2
A uploadFile() 0 3 1
A instanceDir() 0 3 1
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Stores;
5
6
use Firesphere\SolrSearch\Interfaces\ConfigStore;
7
use GuzzleHttp\Client;
8
use Psr\Http\Message\ResponseInterface;
9
use Solarium\Exception\RuntimeException;
10
11
class PostConfigStore implements ConfigStore
12
{
13
    protected static $extensions = [
14
        'xml' => 'text/xml',
15
        'txt' => 'text/plain',
16
    ];
17
    /**
18
     * @var array
19
     */
20
    protected $config;
21
22
    /**
23
     * FileConfigStore constructor.
24
     * @param array $config
25
     */
26
    public function __construct($config)
27
    {
28
        if (empty($config)) {
29
            throw new RuntimeException('No config defined', 1);
30
        }
31
        if (!isset($config['path'])) {
32
            $config['path'] = '/';
33
        }
34
35
        if (substr($config['path'], -1) !== '/') {
36
            $config['path'] .= '/';
37
        }
38
39
        $this->config = $config;
40
    }
41
42
    /**
43
     * Upload a file to Solr for index $index
44
     * @param $index string - The name of an index (which is also used as the name of the Solr core for the index)
45
     * @param $file string - A path to a file to upload. The base name of the file will be used on the remote side
46
     * @return ResponseInterface
47
     */
48
    public function uploadFile($index, $file)
49
    {
50
        return $this->uploadString($index, $file, file_get_contents($file));
51
    }
52
53
    /**
54
     * Upload a file to Solr from a string for index $index
55
     * @param string $index - The name of an index (which is also used as the name of the Solr core for the index)
56
     * @param string $filename - The base name of the file to use on the remote side
57
     * @param string $string - The content to upload
58
     * @return ResponseInterface
59
     */
60
    public function uploadString($index, $filename, $string)
61
    {
62
        $info = pathinfo($filename);
63
        $clientConfig = [
64
            'base_uri' => $this->config['uri'],
65
            'headers'  => [
66
                'Content-Type' => static::$extensions[$info['extension']]
67
            ]
68
        ];
69
        // Add auth to the post if needed
70
        if (isset($this->config['auth'])) {
71
            $clientConfig['auth'] = [
72
                $this->config['auth']['username'],
73
                $this->config['auth']['password'],
74
            ];
75
        }
76
77
        $client = new Client($clientConfig);
78
79
        $path = sprintf('%s%s', $this->getPath(), $index);
80
81
        return $client->post($path, ['body' => $string]);
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getPath()
88
    {
89
        return $this->config['path'];
90
    }
91
92
    /**
93
     * Get the instanceDir to tell Solr to use for index $index
94
     * @param string|null $index string - The name of an index
95
     *          (which is also used as the name of the Solr core for the index)
96
     * @return null|string
97
     */
98
    public function instanceDir($index)
99
    {
100
        return $index;
101
    }
102
}
103