SolrConfigStore_WebDAV   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A instanceDir() 0 3 2
A uploadString() 0 4 1
A uploadFile() 0 4 1
A __construct() 0 11 3
A getTargetDir() 0 13 3
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Solr\Stores;
4
5
use SilverStripe\FullTextSearch\Solr\Solr;
6
use SilverStripe\FullTextSearch\Utils\WebDAV;
7
8
/**
9
 * Class SolrConfigStore_WebDAV
10
 *
11
 * A ConfigStore that uploads files to a Solr instance via a WebDAV server
12
 */
13
class SolrConfigStore_WebDAV implements SolrConfigStore
14
{
15
    public function __construct($config)
16
    {
17
        $options = Solr::solr_options();
18
19
        $this->url = implode('', array(
0 ignored issues
show
Bug Best Practice introduced by
The property url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
            'http://',
21
            isset($config['auth']) ? $config['auth'] . '@' : '',
22
            $options['host'] . ':' . (isset($config['port']) ? $config['port'] : $options['port']),
23
            $config['path']
24
        ));
25
        $this->remote = $config['remotepath'];
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...
26
    }
27
28
    public function getTargetDir($index)
29
    {
30
        $indexdir = "{$this->url}/$index";
31
        if (!WebDAV::exists($indexdir)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression SilverStripe\FullTextSea...bDAV::exists($indexdir) of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
32
            WebDAV::mkdir($indexdir);
33
        }
34
35
        $targetDir = "{$this->url}/$index/conf";
36
        if (!WebDAV::exists($targetDir)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression SilverStripe\FullTextSea...DAV::exists($targetDir) of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
37
            WebDAV::mkdir($targetDir);
38
        }
39
40
        return $targetDir;
41
    }
42
43
    public function uploadFile($index, $file)
44
    {
45
        $targetDir = $this->getTargetDir($index);
46
        WebDAV::upload_from_file($file, $targetDir . '/' . basename($file));
47
    }
48
49
    public function uploadString($index, $filename, $string)
50
    {
51
        $targetDir = $this->getTargetDir($index);
52
        WebDAV::upload_from_string($string, "$targetDir/$filename");
53
    }
54
55
    public function instanceDir($index)
56
    {
57
        return $this->remote ? "{$this->remote}/$index" : $index;
58
    }
59
}
60