File::renameAtFS()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xhelp;
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    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @author       Eric Juden <[email protected]>
19
 * @author       XOOPS Development Team
20
 */
21
22
if (!\defined('XHELP_CLASS_PATH')) {
23
    exit();
24
}
25
26
// require_once XHELP_CLASS_PATH . '/BaseObjectHandler.php';
27
28
/**
29
 * File class
30
 *
31
 * @author  Eric Juden <[email protected]>
32
 */
33
class File extends \XoopsObject
34
{
35
    /**
36
     * File constructor.
37
     * @param int|array|null $id
38
     */
39
    public function __construct($id = null)
40
    {
41
        $this->initVar('id', \XOBJ_DTYPE_INT, null, false);
42
        $this->initVar('filename', \XOBJ_DTYPE_TXTBOX, null, true, 255);
43
        $this->initVar('ticketid', \XOBJ_DTYPE_INT, null, true);
44
        $this->initVar('responseid', \XOBJ_DTYPE_INT, null, false);
45
        $this->initVar('mimetype', \XOBJ_DTYPE_TXTBOX, null, true, 255);
46
47
        if (null !== $id) {
48
            if (\is_array($id)) {
49
                $this->assignVars($id);
50
            }
51
        } else {
52
            $this->setNew();
53
        }
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getFilePath(): string
60
    {
61
        $path = XHELP_UPLOAD_PATH . '/' . $this->getVar('filename');
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Xhelp\XHELP_UPLOAD_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
62
63
        return $path;
64
    }
65
66
    /**
67
     * @param int $ticketid
68
     * @param int $responseid
69
     * @return bool
70
     */
71
    public function rename(int $ticketid, int $responseid = 0): bool
72
    {
73
        $helper         = Helper::getInstance();
74
        $ticketid       = $ticketid;
75
        $responseid     = $responseid;
76
        $old_ticketid   = $this->getVar('ticketid');
77
        $old_responseid = $this->getVar('responseid');
78
79
        $filename    = $this->getVar('filename');
80
        $newFilename = '';
81
        if ((0 != $old_responseid) && (0 != $responseid)) {   // Was a response and is going to be a response
82
            $newFilename = \str_replace('_' . $old_responseid . '_', '_' . $responseid . '_', $filename);
83
            $newFilename = \str_replace($old_ticketid . '_', $ticketid . '_', $newFilename);
84
        } elseif ((0 != $old_responseid) && (0 == $responseid)) { // Was a response and is part of the ticket now
85
            $newFilename = \str_replace('_' . $old_responseid . '_', '_', $filename);
86
            $newFilename = \str_replace($old_ticketid . '_', $ticketid . '_', $newFilename);
87
        } elseif ((0 == $old_responseid) && (0 != $responseid)) {  // Was part of the ticket, now going to a response
88
            $newFilename = \str_replace($old_ticketid . '_', $ticketid . '_' . $responseid . '_', $filename);
89
        } elseif ((0 == $old_responseid)
90
                  && (0 == $responseid)) {  // Was part of the ticket, and is part of the ticket now
91
            $newFilename = \str_replace($old_ticketid . '_', $ticketid . '_', $filename);
92
        }
93
94
        /** @var \XoopsModules\Xhelp\FileHandler $fileHandler */
95
        $fileHandler = $helper->getHandler('File');
96
        $this->setVar('filename', $newFilename);
97
        $this->setVar('ticketid', $ticketid);
98
        $this->setVar('responseid', $responseid);
99
        if ($fileHandler->insert($this, true)) {
100
            $success = true;
101
        } else {
102
            $success = false;
103
        }
104
105
        $ret = false;
106
        if ($success) {
107
            $ret = $this->renameAtFS($filename, $newFilename);
108
        }
109
110
        return $ret;
111
    }
112
113
    /**
114
     * @param string $oldName
115
     * @param string $newName
116
     * @return bool
117
     */
118
    public function renameAtFS(string $oldName, string $newName): bool
119
    {
120
        $ret = \rename(\XHELP_UPLOAD_PATH . '/' . $oldName, \XHELP_UPLOAD_PATH . '/' . $newName);
0 ignored issues
show
Bug introduced by
The constant XHELP_UPLOAD_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
121
122
        return $ret;
123
    }
124
}   //end of class
125