Completed
Push — master ( 5f8c85...f8b859 )
by Andreas
19:27
created

midcom_services_rcs_config::use_rcs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * The class containing the configuration options for RCS.
4
 *
5
 * @author tarjei huse
6
 * @package midcom.services.rcs
7
 * @copyright The Midgard Project, http://www.midgard-project.org
8
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
9
 */
10
11
/**
12
 * The config class is used to generate the RCS configuration.
13
 *
14
 * @see midcom_services_rcs for an overview of the options
15
 * @package midcom.services.rcs
16
 */
17
class midcom_services_rcs_config
18
{
19
    /**
20
     * The array of configuration options
21
     *
22
     * @var midcom_config
23
     */
24
    private $config;
25
26
    /**
27
     * Constructor
28
     */
29 8
    public function __construct(midcom_config $config)
30
    {
31 8
        $this->config = $config;
32 8
    }
33
34
    /**
35
     * Returns the root of the directory containing the RCS files.
36
     */
37 83
    public function get_rcs_root() : string
38
    {
39 83
        if (empty($this->config['midcom_services_rcs_root'])) {
40
            $basedir = dirname(midgard_connection::get_instance()->config->sharedir);
41
            $this->config['midcom_services_rcs_root'] = $basedir . '/rcs';
42
        }
43
44 83
        return $this->config['midcom_services_rcs_root'];
45
    }
46
47
    /**
48
     * If the RCS service is enabled
49
     * (set by midcom_services_rcs_enable)
50
     *
51
     * @return boolean true if it is enabled
52
     */
53 83
    public function use_rcs() : bool
54
    {
55 83
        return !empty($this->config['midcom_services_rcs_enable']);
56
    }
57
58
    /**
59
     * Returns the prefix for the rcs utilities.
60
     */
61 84
    public function get_bin_prefix() : string
62
    {
63 84
        return $this->config['midcom_services_rcs_bin_dir'] . '/';
64
    }
65
66
    /**
67
     * Returns the backend classname.
68
     */
69 82
    public function get_backend_class() : string
70
    {
71 82
        if ($this->use_rcs()) {
72 82
            $this->test_rcs_config();
73 82
            return midcom_services_rcs_backend_rcs::class;
74
        }
75
76 1
        return midcom_services_rcs_backend_null::class;
77
    }
78
79
    /**
80
     * Checks if the basic rcs service is usable.
81
     */
82 83
    public function test_rcs_config()
83
    {
84 83
        if (!is_writable($this->get_rcs_root())) {
85
            throw new midcom_error("The root RCS directory {$this->config['midcom_services_rcs_root']} is not writable!");
86
        }
87
88 83
        $prefix = $this->get_bin_prefix();
89
90 83
        if (!is_executable("{$prefix}ci")) {
91
            throw new midcom_error("Cannot execute {$prefix}ci.");
92
        }
93 83
    }
94
}
95