Issues (806)

src/midcom/dba/attachments.php (1 issue)

Labels
Severity
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 2
    public function get_attachment(string $name) : ?midcom_db_attachment
28
    {
29 2
        if (!$this->id) {
30
            debug_add('Cannot retrieve attachments on a non-persistent object.', MIDCOM_LOG_WARN);
31
            return null;
32
        }
33
34
        // Locate attachment
35 2
        $qb = $this->get_attachment_qb();
36 2
        $qb->add_constraint('name', '=', $name);
37 2
        return $qb->execute()[0] ?? null;
38
    }
39
40
    /**
41
     * Delete an attachment by its name.
42
     * If multiple attachments match the name (should not happen in reality), the
43
     * first match will be deleted.
44
     */
45 2
    public function delete_attachment(string $name) : bool
46
    {
47 2
        $attachment = $this->get_attachment($name);
48
49 2
        if (!$attachment) {
50 2
            debug_add("Tried to delete the attachment {$name} at the object " . static::class . " {$this->guid}, but it did not exist. Failing silently.");
51 2
            return false;
52
        }
53
54
        if (   !$this->can_do('midgard:update')
0 ignored issues
show
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

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