Passed
Pull Request — master (#88)
by Michael
02:56
created

ImagesHandler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 122
rs 10
c 2
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllImages() 0 7 1
A getCountImages() 0 7 1
A getImagesCriteria() 0 15 2
A get() 0 3 1
A unlinkImages() 0 21 4
A __construct() 0 3 1
A getInsertId() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Tdmdownloads\Common;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * @copyright      2020 XOOPS Project (https://xoops.org)
17
 * @license        GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
18
 * @link           https://xoops.org
19
 * @author         Wedega - Email:<[email protected]> - Website:<https://wedega.com>
20
 */
21
22
/**
23
 * Class Object Handler Images
24
 */
25
class ImagesHandler extends \XoopsPersistableObjectHandler
26
{
27
    /**
28
     * Constructor
29
     * @param \XoopsDatabase|null $db
30
     */
31
    public function __construct(?\XoopsDatabase $db = null)
32
    {
33
        parent::__construct($db, 'tdmdownloads_images', Images::class, 'img_id', 'img_name');
34
    }
35
36
    /**
37
     * retrieve a field
38
     *
39
     * @param int        $i field id
40
     * @param null|mixed $fields
41
     * @return mixed reference to the {@link Get} object
42
     */
43
    public function get($i = null, $fields = null)
44
    {
45
        return parent::get($i, $fields);
46
    }
47
48
    /**
49
     * get inserted id
50
     *
51
     * @param null
52
     * @return int reference to the {@link Get} object
53
     */
54
    public function getInsertId()
55
    {
56
        return $this->db->getInsertId();
57
    }
58
59
    /**
60
     * Get Count Images in the database
61
     * @param int    $albId
62
     * @param int    $start
63
     * @param int    $limit
64
     * @param string $sort
65
     * @param string $order
66
     * @return int
67
     */
68
    public function getCountImages($albId = 0, $start = 0, $limit = 0, $sort = 'img_id ASC, img_name', $order = 'ASC')
69
    {
70
        $crCountImages = new \CriteriaCompo();
71
72
        $crCountImages = $this->getImagesCriteria($crCountImages, $albId, $start, $limit, $sort, $order);
73
74
        return parent::getCount($crCountImages);
0 ignored issues
show
Bug introduced by
$crCountImages 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

74
        return parent::getCount(/** @scrutinizer ignore-type */ $crCountImages);
Loading history...
75
    }
76
77
    /**
78
     * Get All Images in the database
79
     * @param int    $start
80
     * @param int    $limit
81
     * @param string $sort
82
     * @param string $order
83
     * @return array
84
     */
85
    public function getAllImages($start = 0, $limit = 0, $sort = 'img_id ASC, img_name', $order = 'ASC')
86
    {
87
        $crAllImages = new \CriteriaCompo();
88
89
        $crAllImages = $this->getImagesCriteria($crAllImages, 0, $start, $limit, $sort, $order);
90
91
        return parent::getAll($crAllImages);
0 ignored issues
show
Bug introduced by
$crAllImages 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

91
        return parent::getAll(/** @scrutinizer ignore-type */ $crAllImages);
Loading history...
92
    }
93
94
    /**
95
     * Get Criteria Images
96
     * @param        $crImages
97
     * @param        $albId
98
     * @param int    $start
99
     * @param int    $limit
100
     * @param string $sort
101
     * @param string $order
102
     * @return int
103
     */
104
    private function getImagesCriteria($crImages, $albId, $start, $limit, $sort, $order)
105
    {
106
        if ($albId > 0) {
107
            $crImages->add(new \Criteria('img_albid', $albId));
108
        }
109
110
        $crImages->setStart($start);
111
112
        $crImages->setLimit($limit);
113
114
        $crImages->setSort($sort);
115
116
        $crImages->setOrder($order);
117
118
        return $crImages;
119
    }
120
121
    /**
122
     * delete all copies of a specific image
123
     * @param $imageName
124
     * @return bool
125
     */
126
    public function unlinkImages($imageName)
127
    {
128
        \unlink(\constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_PATH') . '/large/' . $imageName);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $moduleDirNameUpper seems to be never defined.
Loading history...
129
130
        if (\file_exists(\constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_PATH') . '/large/' . $imageName)) {
131
            return false;
132
        }
133
134
        \unlink(\constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_PATH') . '/medium/' . $imageName);
135
136
        if (\file_exists(\constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_PATH') . '/medium/' . $imageName)) {
137
            return false;
138
        }
139
140
        \unlink(\constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_PATH') . '/thumbs/' . $imageName);
141
142
        if (\file_exists(\constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_PATH') . '/thumbs/' . $imageName)) {
143
            return false;
144
        }
145
146
        return true;
147
    }
148
}
149