SolrConfigStore_File::uploadFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Solr\Stores;
4
5
/**
6
 * Class SolrConfigStore_File
7
 *
8
 * A ConfigStore that uploads files to a Solr instance on a locally accessible filesystem
9
 * by just using file copies
10
 */
11
class SolrConfigStore_File implements SolrConfigStore
12
{
13
    public function __construct($config)
14
    {
15
        $this->local = $config['path'];
0 ignored issues
show
Bug Best Practice introduced by
The property local does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
16
        $this->remote = isset($config['remotepath']) ? $config['remotepath'] : $config['path'];
0 ignored issues
show
Bug Best Practice introduced by
The property remote does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
    }
18
19
    public function getTargetDir($index)
20
    {
21
        $targetDir = "{$this->local}/{$index}/conf";
22
23
        if (!is_dir($targetDir)) {
24
            $worked = @mkdir($targetDir, 0770, true);
25
26
            if (!$worked) {
27
                throw new \RuntimeException(
28
                    sprintf('Failed creating target directory %s, please check permissions', $targetDir)
29
                );
30
            }
31
        }
32
33
        return $targetDir;
34
    }
35
36
    public function uploadFile($index, $file)
37
    {
38
        $targetDir = $this->getTargetDir($index);
39
        copy($file, $targetDir . '/' . basename($file));
40
    }
41
42
    public function uploadString($index, $filename, $string)
43
    {
44
        $targetDir = $this->getTargetDir($index);
45
        file_put_contents("$targetDir/$filename", $string);
46
    }
47
48
    public function instanceDir($index)
49
    {
50
        return $this->remote . '/' . $index;
51
    }
52
}
53