MimetypeHandler   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 147
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A __construct() 0 3 1
A get() 0 3 1
A getCountMimetype() 0 5 1
A getAllMimetype() 0 5 1
A getExtensionArray() 0 14 2
A getInsertId() 0 3 1
A getMimetypeArray() 0 28 6
A getMimetypeCriteria() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace XoopsModules\Wgfilemanager;
7
8
/*
9
 You may not change or alter any portion of this comment or credits
10
 of supporting developers from this source code or any supporting source code
11
 which is considered copyrighted (c) material of the original comment or credit authors.
12
13
 This program is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
*/
17
18
/**
19
 * wgFileManager module for xoops
20
 *
21
 * @copyright    2021 XOOPS Project (https://xoops.org)
22
 * @license      GPL 2.0 or later
23
 * @package      wgfilemanager
24
 * @since        1.0
25
 * @min_xoops    2.5.9
26
 * @author       Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com
27
 */
28
29
use XoopsModules\Wgfilemanager;
30
31
32
/**
33
 * Class Object Handler Mimetype
34
 */
35
class MimetypeHandler extends \XoopsPersistableObjectHandler
36
{
37
    /**
38
     * Constructor
39
     *
40
     * @param \XoopsDatabase $db
41
     */
42
    public function __construct(\XoopsDatabase $db)
43
    {
44
        parent::__construct($db, 'wgfilemanager_mimetype', Mimetype::class, 'id', 'extension');
45
    }
46
47
    /**
48
     * @param bool $isNew
49
     *
50
     * @return object
51
     */
52
    public function create($isNew = true)
53
    {
54
        return parent::create($isNew);
55
    }
56
57
    /**
58
     * retrieve a field
59
     *
60
     * @param int $id field id
61
     * @param null $fields fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
62
     * @return \XoopsObject|null reference to the {@link Get} object
63
     */
64
    public function get($id = null, $fields = null)
65
    {
66
        return parent::get($id, $fields);
67
    }
68
69
    /**
70
     * get inserted id
71
     *
72
     * @return int reference to the {@link Get} object
73
     */
74
    public function getInsertId()
75
    {
76
        return $this->db->getInsertId();
77
    }
78
79
    /**
80
     * Get Count Mimetype in the database
81
     * @param int    $start
82
     * @param int    $limit
83
     * @param string $sort
84
     * @param string $order
85
     * @return int
86
     */
87
    public function getCountMimetype($start = 0, $limit = 0, $sort = 'id', $order = 'ASC')
88
    {
89
        $crCountMimetype = new \CriteriaCompo();
90
        $crCountMimetype = $this->getMimetypeCriteria($crCountMimetype, $start, $limit, $sort, $order);
91
        return $this->getCount($crCountMimetype);
0 ignored issues
show
Bug introduced by
$crCountMimetype of type integer is incompatible with the type CriteriaElement|null expected by parameter $criteria of XoopsPersistableObjectHandler::getCount(). ( Ignorable by Annotation )

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

91
        return $this->getCount(/** @scrutinizer ignore-type */ $crCountMimetype);
Loading history...
92
    }
93
94
    /**
95
     * Get All Mimetype in the database
96
     * @param int    $start
97
     * @param int    $limit
98
     * @param string $sort
99
     * @param string $order
100
     * @return array
101
     */
102
    public function getAllMimetype($start = 0, $limit = 0, $sort = 'extension', $order = 'ASC')
103
    {
104
        $crAllMimetype = new \CriteriaCompo();
105
        $crAllMimetype = $this->getMimetypeCriteria($crAllMimetype, $start, $limit, $sort, $order);
106
        return $this->getAll($crAllMimetype);
0 ignored issues
show
Bug introduced by
$crAllMimetype of type integer is incompatible with the type CriteriaElement|null expected by parameter $criteria of XoopsPersistableObjectHandler::getAll(). ( Ignorable by Annotation )

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

106
        return $this->getAll(/** @scrutinizer ignore-type */ $crAllMimetype);
Loading history...
107
    }
108
109
    /**
110
     * Get Criteria Mimetype
111
     * @param        $crMimetype
112
     * @param int    $start
113
     * @param int    $limit
114
     * @param string $sort
115
     * @param string $order
116
     * @return int
117
     */
118
    private function getMimetypeCriteria($crMimetype, $start, $limit, $sort, $order)
119
    {
120
        $crMimetype->setStart($start);
121
        $crMimetype->setLimit($limit);
122
        $crMimetype->setSort($sort);
123
        $crMimetype->setOrder($order);
124
        return $crMimetype;
125
    }
126
127
    /**
128
     * Get Mimetypes
129
     *
130
     * @return array
131
     */
132
    public function getMimetypeArray()
133
    {
134
        $isAdmin = \is_object($GLOBALS['xoopsUser']) && $GLOBALS['xoopsUser']->isAdmin($GLOBALS['xoopsModule']->mid());
135
136
        $crMimetype = new \CriteriaCompo();
137
        if ($isAdmin) {
138
            $crMimetype->add(new \Criteria('admin', '1'));
139
        } else {
140
            $crMimetype->add(new \Criteria('user', '1'));
141
        }
142
        $mimetypeAll = $this->getAll($crMimetype);
143
        $mimetypeArray = [];
144
        foreach (\array_keys($mimetypeAll) as $i) {
145
            $mimetype = $mimetypeAll[$i]->getVar('mimetype');
146
            if (\strpos($mimetype, ' ') > 0) {
147
                //mimetype comtains multiple mimetypes
148
                $multiMime = explode(' ', $mimetype);
149
                foreach ($multiMime as $m) {
150
                    $mimetypeArray[] = $m;
151
                }
152
            } else {
153
                //mimetype comtains single mimetype
154
                $mimetypeArray[] = $mimetype;
155
            }
156
            unset($mimetype);
157
        }
158
159
        return \array_unique(\array_filter($mimetypeArray));
160
161
    }
162
163
    /**
164
     * Get all allowed extensions
165
     *
166
     * @return array
167
     */
168
    public function getExtensionArray()
169
    {
170
        $crMimetype = new \CriteriaCompo();
171
        $crMimetype->add(new \Criteria('admin', '1'));
172
        $crMimetype->add(new \Criteria('user', '1'), 'OR');
173
174
        $mimetypeAll = $this->getAll($crMimetype);
175
        $extensionArray = [];
176
        foreach (\array_keys($mimetypeAll) as $i) {
177
            $extensionArray[$i]['extension'] = $mimetypeAll[$i]->getVar('extension');
178
            $extensionArray[$i]['category'] = $mimetypeAll[$i]->getVar('category');
179
        }
180
181
        return $extensionArray;
182
183
    }
184
}
185