Test Failed
Push — master ( 768d56...d599e6 )
by Andreas
09:54
created

midcom_services_rcs::update()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4.5923

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 1
dl 0
loc 12
ccs 2
cts 3
cp 0.6667
crap 4.5923
rs 9.9666
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
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use midcom\events\dbaevent;
11
12
/**
13
 * The RCS service gives a write only interface to different services wanting to save changes to objects.
14
 *
15
 * The RCS service will try to initialize the backend based on GNU RCS, but, if that fails, fall back
16
 * to the nullrcs handler. The nullrcs handler does not save anything at all.
17
 *
18
 * <b>Configuration parameters that are in use by this service:</b>
19
 * * string midcom_services_rcs_bin_dir - the prefix for the rcs utilities (normally /usr/bin)
20
 * * string midcom_services_rcs_root - the directory where the rcs files get placed.
21
 * * boolean midcom_services_rcs_enable - if set, midcom will fail hard if the rcs service is not operational.
22
 *
23
 * @package midcom.services.rcs
24
 */
25
class midcom_services_rcs implements EventSubscriberInterface
26 2
{
27
    private midcom_services_rcs_config $config;
28 2
29
    public static function getSubscribedEvents()
30
    {
31
        return [
32
            dbaevent::CREATE => ['update'],
33
            dbaevent::UPDATE => ['update'],
34 238
            dbaevent::DELETE => ['update']
35
        ];
36 238
    }
37 1
38
    public function __construct(midcom_config $config)
39 238
    {
40
        $this->config = new midcom_services_rcs_config($config);
41 238
    }
42
43
    /**
44
     * Factory function for the handler object.
45
     */
46
    public function load_backend(midcom_core_dbaobject $object) : midcom_services_rcs_backend
47 234
    {
48
        if (!$object->guid) {
49 234
            $class = midcom_services_rcs_backend_null::class;
50
        } else {
51
            $class = $this->config->get_backend_class();
52 234
        }
53
        return new $class($object, $this->config);
54 234
    }
55 234
56
    /**
57
     * Create or update the RCS file for the object.
58
     */
59
    public function update(dbaevent $event)
60
    {
61
        $object = $event->get_object();
62
        if (!$this->config->use_rcs() || !$object->_use_rcs) {
63
            return;
64
        }
65
        $backend = $this->load_backend($object);
66 4
        try {
67
            $backend->update(midcom::get()->auth->user->id ?? 'NOBODY', $object->get_rcs_message());
68 4
        } catch (midcom_error $e) {
69
            debug_add('RCS: Could not save file!');
70
            $e->log();
71
        }
72
    }
73
74
    /**
75
     * Determine if we should display a particular field in the diff or preview states
76
     */
77
    public static function is_field_showable(string $field) : bool
78
    {
79
        return !in_array($field, ['id', 'guid'], true);
80
    }
81
}
82