Completed
Push — master ( cbeceb...e8fccc )
by Andreas
17:05
created

midcom_services_rcs::is_field_showable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author tarjei huse
4
 * @package midcom.services.rcs
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * The RCS service gives a write only interface to different services wanting to save changes to objects.
11
 *
12
 * The RCS service will try to initialize the backend based on GNU RCS, but, if that fails, fall back
13
 * to the nullrcs handler. The nullrcs handler does not save anything at all.
14
 *
15
 * <b>Configuration parameters that are in use by this service:</b>
16
 * * string midcom_services_rcs_bin_dir - the prefix for the rcs utilities (normally /usr/bin)
17
 * * string midcom_services_rcs_root - the directory where the rcs files get placed.
18
 * * boolean midcom_services_rcs_enable - if set, midcom will fail hard if the rcs service is not operational.
19
 *
20
 * @package midcom.services.rcs
21
 */
22
class midcom_services_rcs
23
{
24
    /**
25
     * The configuration object for the rcs service.
26
     *
27
     * @var midcom_services_rcs_config
28
     */
29
    private $config;
30
31
    /**
32
     * Constructor
33
     */
34 3
    public function __construct(midcom_config $config)
35
    {
36 3
        $this->config = new midcom_services_rcs_config($config);
37 3
    }
38
39
    /**
40
     * Factory function for the handler object.
41
     */
42 80
    public function load_backend($object) : midcom_services_rcs_backend
43
    {
44 80
        if (!$object->guid) {
45 1
            $class = midcom_services_rcs_backend_null::class;
46
        } else {
47 80
            $class = $this->config->get_backend_class();
48
        }
49 80
        return new $class($object, $this->config);
50
    }
51
52
    /**
53
     * Create or update the RCS file for the object.
54
     *
55
     * @param object $object the midgard object to be saved
56
     * @param string $message the update message to save (optional)
57
     */
58 73
    public function update($object, $message = null) : bool
59
    {
60 73
        if (!$this->config->use_rcs()) {
61
            return true;
62
        }
63 73
        $backend = $this->load_backend($object);
64
        try {
65 73
            $backend->update($message);
66 73
            return true;
67
        } catch (midcom_error $e) {
68
            debug_add('RCS: Could not save file!');
69
            $e->log();
70
            return false;
71
        }
72
    }
73
74
    /**
75
     * Determine if we should display a particular field in the diff or preview states
76
     */
77 4
    public static function is_field_showable($field) : bool
78
    {
79 4
        return ($field !== 'id' && $field !== 'guid');
80
    }
81
}
82