Passed
Pull Request — master (#14)
by Michael
04:13
created

Files::getPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace XoopsModules\Oledrion;
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
 * oledrion
17
 *
18
 * @copyright   {@link https://xoops.org/ XOOPS Project}
19
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
20
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
21
 */
22
23
use XoopsModules\Oledrion;
24
25
/**
26
 * Gestion des fichies attachés aux produits
27
 */
28
29
/**
30
 * Class Files
31
 */
32
class Files extends OledrionObject
33
{
34
    /**
35
     * constructor
36
     *
37
     * normally, this is called from child classes only
38
     */
39
    public function __construct()
40
    {
41
        $this->initVar('file_id', XOBJ_DTYPE_INT, null, false);
42
        $this->initVar('file_product_id', XOBJ_DTYPE_INT, null, false);
43
        $this->initVar('file_filename', XOBJ_DTYPE_TXTBOX, null, false);
44
        $this->initVar('file_description', XOBJ_DTYPE_TXTBOX, null, false);
45
        $this->initVar('file_mimetype', XOBJ_DTYPE_TXTBOX, null, false);
46
    }
47
48
    /**
49
     * Supprime un fichier
50
     */
51
    public function deleteAttachedFile()
52
    {
53
        if (!defined('OLEDRION_ATTACHED_FILES_PATH')) {
54
            include OLEDRION_PATH . 'config.php';
55
        }
56
        if (false === @unlink(OLEDRION_ATTACHED_FILES_PATH . '/' . $this->getVar('file_filename'))) {
57
            throw new \RuntimeException('The file ' . OLEDRION_ATTACHED_FILES_PATH . '/' . $this->getVar('file_filename') . ' could not be deleted.');
58
        }
59
    }
60
61
    /**
62
     * Indique si le fichier courant est un fichier MP3
63
     * @return bool
64
     */
65
    public function isMP3()
66
    {
67
        return 'audio/mpeg' === mb_strtolower($this->getVar('file_mimetype'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('file_mimetype') can also be of type array and array; however, parameter $str of mb_strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

67
        return 'audio/mpeg' === mb_strtolower(/** @scrutinizer ignore-type */ $this->getVar('file_mimetype'));
Loading history...
68
    }
69
70
    /**
71
     * Indique si le fichier attaché existe physiquement sur le site
72
     * @return bool
73
     */
74
    public function fileExists()
75
    {
76
        if (!defined('OLEDRION_ATTACHED_FILES_PATH')) {
77
            include OLEDRION_PATH . 'config.php';
78
        }
79
80
        return file_exists(OLEDRION_ATTACHED_FILES_PATH . '/' . $this->getVar('file_filename'));
81
    }
82
83
    /**
84
     * Retourne l'url pour accéder au fichier
85
     * @return string
86
     */
87
    public function getURL()
88
    {
89
        if (!defined('OLEDRION_ATTACHED_FILES_URL')) {
90
            include OLEDRION_PATH . 'config.php';
91
        }
92
93
        return OLEDRION_ATTACHED_FILES_URL . '/' . $this->getVar('file_filename');
94
    }
95
96
    /**
97
     * Retourne le chemin physique pour accéder au fichier
98
     * @return string
99
     */
100
    public function getPath()
101
    {
102
        if (!defined('OLEDRION_ATTACHED_FILES_URL')) {
103
            include OLEDRION_PATH . 'config.php';
104
        }
105
106
        return OLEDRION_ATTACHED_FILES_PATH . '/' . $this->getVar('file_filename');
107
    }
108
109
    /**
110
     * @param  string $format
111
     * @return array
112
     */
113
    public function toArray($format = 's')
114
    {
115
        $ret                      = parent::toArray($format);
116
        $ret['file_is_mp3']       = $this->isMP3();
117
        $ret['file_download_url'] = $this->getURL();
118
119
        return $ret;
120
    }
121
}
122