Completed
Push — master ( c8d932...0a2e26 )
by Andreas
20:07
created

attachments::delete_attachment()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 6.5971

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 1
dl 0
loc 17
ccs 5
cts 11
cp 0.4545
crap 6.5971
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.dba
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\dba;
10
11
use midcom_connection;
12
use midcom_db_attachment;
13
use midcom;
14
15
/**
16
 * midcom attachment support
17
 *
18
 * @package midcom.dba
19
 */
20
trait attachments
21
{
22
    /**
23
     * Retrieves an attachment by its name.
24
     * If multiple attachments match the name (should not happen in reality), the
25
     * first match will be returned.
26
     *
27
     * @param string $name The name of the attachment to look up.
28
     * @return midcom_db_attachment The attachment found, or false on failure.
29
     */
30 2
    public function get_attachment(string $name) : ?midcom_db_attachment
31
    {
32 2
        if (!$this->id) {
33
            debug_add('Cannot retrieve attachments on a non-persistent object.', MIDCOM_LOG_WARN);
34
            return null;
35
        }
36
37
        // Locate attachment
38 2
        $qb = $this->get_attachment_qb();
39 2
        $qb->add_constraint('name', '=', $name);
40 2
        return $qb->execute()[0] ?? null;
41
    }
42
43
    /**
44
     * Delete an attachment by its name.
45
     * If multiple attachments match the name (should not happen in reality), the
46
     * first match will be deleted.
47
     *
48
     * @param string $name The name of the attachment to delete.
49
     * @return bool Indicating success.
50
     */
51 2
    public function delete_attachment($name) : bool
52
    {
53 2
        $attachment = $this->get_attachment($name);
54
55 2
        if (!$attachment) {
56 2
            debug_add("Tried to delete the attachment {$name} at the object " . get_class($this) . " {$this->guid}, but it did not exist. Failing silently.");
57 2
            return false;
58
        }
59
60
        if (   !$this->can_do('midgard:update')
0 ignored issues
show
Bug introduced by
It seems like can_do() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

60
        if (   !$this->/** @scrutinizer ignore-call */ can_do('midgard:update')
Loading history...
61
            || !$this->can_do('midgard:attachments')) {
62
            debug_add("Failed to set parameters, midgard:update or midgard:attachments on the " . get_class($this) . " {$this->guid} not granted for the current user.",
63
            MIDCOM_LOG_ERROR);
64
            return false;
65
        }
66
67
        return $attachment->delete();
68
    }
69
70
    /**
71
     * Creates a new attachment at the current object and returns it for usage.
72
     */
73 4
    public function create_attachment(string $name, string $title, string $mimetype) : ?midcom_db_attachment
74
    {
75 4
        if (!$this->id) {
76
            debug_add('Cannot create attachments on a non-persistent object.', MIDCOM_LOG_WARN);
77
            return null;
78
        }
79
80 4
        if (   !$this->can_do('midgard:update')
81 4
            || !$this->can_do('midgard:attachments')) {
82
            debug_add("Failed to set parameters, midgard:update or midgard:attachments on the " . get_class($this) . " {$this->guid} not granted for the current user.",
83
            MIDCOM_LOG_ERROR);
84
            return null;
85
        }
86
87 4
        $attachment = new midcom_db_attachment();
88 4
        $attachment->name = $name;
89 4
        $attachment->title = $title;
90 4
        $attachment->mimetype = $mimetype;
91 4
        $attachment->parentguid = $this->guid;
92
93 4
        if (!$attachment->create()) {
94
            debug_add("Could not create the attachment '{$name}' for " . get_class($this) . " {$this->guid}: "  . midcom_connection::get_error_string(),
95
            MIDCOM_LOG_INFO);
96
            return null;
97
        }
98
99 4
        return $attachment;
100
    }
101
102
    /**
103
     * Returns a prepared query builder that is already limited to the attachments of the given
104
     * object.
105
     */
106 180
    public function get_attachment_qb() : ?\midcom_core_querybuilder
107
    {
108 180
        if (!$this->id) {
109
            debug_add('Cannot retrieve attachments on a non-persistent object.', MIDCOM_LOG_WARN);
110
            return null;
111
        }
112
113 180
        $qb = midcom::get()->dbfactory->new_query_builder('midcom_db_attachment');
114 180
        $qb->add_constraint('parentguid', '=', $this->guid);
115
116 180
        return $qb;
117
    }
118
119
    /**
120
     * Returns a complete list of attachments for the current object. If there are no
121
     * attachments, an empty array is returned.
122
     *
123
     * @return midcom_db_attachment[] A list of attachments
124
     */
125 180
    public function list_attachments() : array
126
    {
127 180
        if (!$this->id) {
128
            debug_add('Cannot retrieve attachments on a non-persistent object.', MIDCOM_LOG_INFO);
129
            return [];
130
        }
131
132 180
        return $this->get_attachment_qb()->execute();
133
    }
134
}
135