backup_version()   C
last analyzed

Complexity

Conditions 14
Paths 45

Size

Total Lines 58
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 0
Metric Value
cc 14
eloc 29
nc 45
nop 0
dl 0
loc 58
ccs 0
cts 30
cp 0
crap 210
rs 6.2666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package org.openpsa.documents
4
 * @author Nemein Oy http://www.nemein.com/
5
 * @copyright Nemein Oy http://www.nemein.com/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
use midcom\datamanager\storage\blobs;
10
11
/**
12
 * DBA class for org_openpsa_document
13
 *
14
 * Implements parameter and attachment methods for DM compatibility
15
 *
16
 * @property integer $author
17
 * @property integer $topic
18
 * @property integer $nextVersion
19
 * @property string $title
20
 * @property string $abstract
21
 * @property string $keywords
22
 * @property integer $docStatus For status flags like: DRAFT, etc, could even be a bitmask stored as integer
23
	        	status seems to be a reserved word in some layer between DM -> DB
24
 * @property string $content plaintext representation of content, non-ML
25
 * @property integer $orgOpenpsaAccesstype Shortcut for various ACL scenarios
26
 * @property string $orgOpenpsaOwnerWg The "owner" workgroup of this object
27
 * @package org.openpsa.documents
28
 */
29
class org_openpsa_documents_document_dba extends midcom_core_dbaobject
30
{
31
    public string $__midcom_class_name__ = __CLASS__;
32
    public string $__mgdschema_class_name__ = 'org_openpsa_document';
33
34
    public array $autodelete_dependents = [
35
        self::class => 'nextVersion'
36
    ];
37
38
    const STATUS_DRAFT = 4000;
39
    const STATUS_FINAL = 4001;
40
    const STATUS_REVIEW = 4002;
41
42 5
    public function _on_loaded()
43
    {
44 5
        if ($this->title == "") {
45 5
            $this->title = "Document #{$this->id}";
46
        }
47
48 5
        if (!$this->docStatus) {
49 5
            $this->docStatus = self::STATUS_DRAFT;
50
        }
51
    }
52
53 1
    public function _on_creating() : bool
54
    {
55 1
        if (!$this->author) {
56 1
            $user = midcom::get()->auth->user->get_storage();
0 ignored issues
show
Bug introduced by
The method get_storage() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
            /** @scrutinizer ignore-call */ 
57
            $user = midcom::get()->auth->user->get_storage();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57 1
            $this->author = $user->id;
58
        }
59 1
        return true;
60
    }
61
62 1
    public function _on_created()
63
    {
64 1
        $this->_update_directory_timestamp();
65
    }
66
67 1
    public function _on_updated()
68
    {
69 1
        $this->_update_directory_timestamp();
70
71
        // Sync the object's ACL properties into MidCOM ACL system
72 1
        $sync = new org_openpsa_core_acl_synchronizer();
73 1
        $sync->write_acls($this, $this->orgOpenpsaOwnerWg, $this->orgOpenpsaAccesstype);
74
    }
75
76 1
    public function _on_deleted()
77
    {
78 1
        $this->_update_directory_timestamp();
79
    }
80
81 1
    private function _update_directory_timestamp()
82
    {
83 1
        if ($this->nextVersion != 0) {
84
            return;
85
        }
86 1
        $parent = $this->get_parent();
87 1
        if (   $parent
88 1
            && $parent->component == 'org.openpsa.documents') {
0 ignored issues
show
Bug Best Practice introduced by
The property component does not exist on midcom_core_dbaobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
89
            midcom::get()->auth->request_sudo('org.openpsa.documents');
90
91
            $parent = new org_openpsa_documents_directory($parent);
92
            $parent->_use_rcs = false;
93
            $parent->update();
94
95
            midcom::get()->auth->drop_sudo();
96
        }
97
    }
98
99
    public function get_label() : string
100
    {
101
        return $this->title;
102
    }
103
104
    /**
105
     * Load the document's attachment
106
     */
107 1
    public function load_attachment() : ?midcom_db_attachment
108
    {
109 1
        if (!$this->guid) {
110
            // Non-persistent object will not have attachments
111
            return null;
112
        }
113
114 1
        $attachments = blobs::get_attachments($this, 'document');
115 1
        if (empty($attachments)) {
116 1
            return null;
117
        }
118
119
        if (count($attachments) > 1) {
120
            debug_add("Multiple attachments have been found for document #" . $this->id . ", returning only the first.", MIDCOM_LOG_INFO);
121
        }
122
123
        return reset($attachments);
124
    }
125
126
    public function backup_version() : bool
127
    {
128
        // Instantiate the backup object
129
        $backup = new org_openpsa_documents_document_dba();
130
        $properties = $this->get_properties();
131
        // Copy current properties
132
        foreach ($properties as $key) {
133
            if (!in_array($key, ['guid', 'id', 'metadata'])) {
134
                $backup->$key = $this->{$key};
135
            }
136
        }
137
138
        $backup->nextVersion = $this->id;
139
        if (!$backup->create()) {
140
            return false;
141
        }
142
143
        // Copy parameters
144
        if ($params = $this->list_parameters()) {
145
            foreach ($params as $domain => $array) {
146
                foreach ($array as $name => $value) {
147
                    $backup->set_parameter($domain, $name, $value);
148
                }
149
            }
150
        }
151
152
        // Find the attachments
153
        foreach ($this->list_attachments() as $original_attachment) {
154
            $backup_attachment = $backup->create_attachment($original_attachment->name, $original_attachment->title, $original_attachment->mimetype);
155
156
            $original_handle = $original_attachment->open('r');
157
            if (   !$backup_attachment
158
                || !$original_handle) {
159
                // Failed to copy the attachment, abort
160
                return $backup->delete();
161
            }
162
163
            // Copy the contents
164
            $backup_handle = $backup_attachment->open('w');
165
166
            stream_copy_to_stream($original_handle, $backup_handle);
0 ignored issues
show
Bug introduced by
$backup_handle of type false is incompatible with the type resource expected by parameter $to of stream_copy_to_stream(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

166
            stream_copy_to_stream($original_handle, /** @scrutinizer ignore-type */ $backup_handle);
Loading history...
167
168
            $original_attachment->close();
169
170
            // Copy attachment parameters
171
            if ($params = $original_attachment->list_parameters()) {
172
                foreach ($params as $domain => $array) {
173
                    foreach ($array as $name => $value) {
174
                        if ($name == 'identifier') {
175
                            $value = md5(time() . $backup_attachment->name);
176
                            $backup->set_parameter('midcom.helper.datamanager2.type.blobs', 'guids_document', $value . ":" . $backup_attachment->guid);
177
                        }
178
                        $backup_attachment->set_parameter($domain, $name, $value);
179
                    }
180
                }
181
            }
182
        }
183
        return true;
184
    }
185
}
186