ImagesHandler::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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

88
        return parent::getAll(/** @scrutinizer ignore-type */ $crAllImages);
Loading history...
89
    }
90
91
    /**
92
     * Get Criteria Images
93
     * @param        $crImages
94
     * @param        $albId
95
     * @param int    $start
96
     * @param int    $limit
97
     * @param string $sort
98
     * @param string $order
99
     * @return int
100
     */
101
    private function getImagesCriteria($crImages, $albId, $start, $limit, $sort, $order)
102
    {
103
        if ($albId > 0) {
104
            $crImages->add(new \Criteria('img_albid', $albId));
105
        }
106
        $crImages->setStart($start);
107
        $crImages->setLimit($limit);
108
        $crImages->setSort($sort);
109
        $crImages->setOrder($order);
110
        return $crImages;
111
    }
112
113
    /**
114
     * delete all copies of a specific image
115
     * @param $imageName
116
     * @return bool
117
     */
118
    public function unlinkImages($imageName)
119
    {
120
        \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...
121
        if (\is_file(\constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_PATH') . '/large/' . $imageName)) {
122
            return false;
123
        }
124
        \unlink(\constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_PATH') . '/medium/' . $imageName);
125
        if (\is_file(\constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_PATH') . '/medium/' . $imageName)) {
126
            return false;
127
        }
128
        \unlink(\constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_PATH') . '/thumbs/' . $imageName);
129
        if (\is_file(\constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_PATH') . '/thumbs/' . $imageName)) {
130
            return false;
131
        }
132
        return true;
133
    }
134
}
135