Completed
Push — master ( 00e474...9d3fbd )
by Michael
04:26
created

OledrionOledrion_filesHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A deleteAttachedFile() 0 8 2
A getProductFiles() 0 8 1
A getProductMP3Count() 0 8 1
A getProductFilesCount() 0 6 1
A deleteProductFiles() 0 12 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * oledrion
14
 *
15
 * @copyright   {@link http://xoops.org/ XOOPS Project}
16
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
17
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
18
 */
19
20
/**
21
 * Gestion des fichies attachés aux produits
22
 */
23
24
require __DIR__ . '/classheader.php';
25
26
/**
27
 * Class Oledrion_files
28
 */
29
class Oledrion_files extends Oledrion_Object
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
30
{
31
    /**
32
     * constructor
33
     *
34
     * normally, this is called from child classes only
35
     *
36
     * @access public
37
     */
38
    public function __construct()
39
    {
40
        $this->initVar('file_id', XOBJ_DTYPE_INT, null, false);
41
        $this->initVar('file_product_id', XOBJ_DTYPE_INT, null, false);
42
        $this->initVar('file_filename', XOBJ_DTYPE_TXTBOX, null, false);
43
        $this->initVar('file_description', XOBJ_DTYPE_TXTBOX, null, false);
44
        $this->initVar('file_mimetype', XOBJ_DTYPE_TXTBOX, null, false);
45
    }
46
47
    /**
48
     * Supprime un fichier
49
     */
50 View Code Duplication
    public function deleteAttachedFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        if (!defined('OLEDRION_ATTACHED_FILES_PATH')) {
53
            include OLEDRION_PATH . 'config.php';
54
        }
55
        @unlink(OLEDRION_ATTACHED_FILES_PATH . '/' . $this->getVar('file_filename'));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
56
    }
57
58
    /**
59
     * Indique si le fichier courant est un fichier MP3
60
     * @return boolean
61
     */
62
    public function isMP3()
63
    {
64
        return strtolower($this->getVar('file_mimetype')) === 'audio/mpeg' ? true : false;
65
    }
66
67
    /**
68
     * Indique si le fichier attaché existe physiquement sur le site
69
     * @return boolean
70
     */
71 View Code Duplication
    public function fileExists()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        if (!defined('OLEDRION_ATTACHED_FILES_PATH')) {
74
            include OLEDRION_PATH . 'config.php';
75
        }
76
77
        return file_exists(OLEDRION_ATTACHED_FILES_PATH . '/' . $this->getVar('file_filename'));
78
    }
79
80
    /**
81
     * Retourne l'url pour accéder au fichier
82
     * @return string
83
     */
84 View Code Duplication
    public function getURL()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        if (!defined('OLEDRION_ATTACHED_FILES_URL')) {
87
            include OLEDRION_PATH . 'config.php';
88
        }
89
90
        return OLEDRION_ATTACHED_FILES_URL . '/' . $this->getVar('file_filename');
91
    }
92
93
    /**
94
     * Retourne le chemin physique pour accéder au fichier
95
     * @return string
96
     */
97 View Code Duplication
    public function getPath()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        if (!defined('OLEDRION_ATTACHED_FILES_URL')) {
100
            include OLEDRION_PATH . 'config.php';
101
        }
102
103
        return OLEDRION_ATTACHED_FILES_PATH . '/' . $this->getVar('file_filename');
104
    }
105
106
    /**
107
     * @param  string $format
108
     * @return array
109
     */
110
    public function toArray($format = 's')
111
    {
112
        $ret                      = parent::toArray($format);
113
        $ret['file_is_mp3']       = $this->isMP3();
114
        $ret['file_download_url'] = $this->getURL();
115
116
        return $ret;
117
    }
118
}
119
120
/**
121
 * Class OledrionOledrion_filesHandler
122
 */
123
class OledrionOledrion_filesHandler extends Oledrion_XoopsPersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
124
{
125
    /**
126
     * OledrionOledrion_filesHandler constructor.
127
     * @param XoopsDatabase|null $db
128
     */
129
    public function __construct(XoopsDatabase $db)
130
    { //                            Table           Classe          Id          Libellé
131
        parent::__construct($db, 'oledrion_files', 'oledrion_files', 'file_id', 'file_filename');
132
    }
133
134
    /**
135
     * Supprime un fichier (son fichier joint ET l'enregistrement dans la base de données)
136
     *
137
     * @param  oledrion_files $file
138
     * @return boolean        Le résultat de la suppression
139
     */
140
    public function deleteAttachedFile(Oledrion_files $file)
141
    {
142
        if ($file->fileExists()) {
143
            $file->deleteAttachedFile();
144
        }
145
146
        return $this->delete($file, true);
147
    }
148
149
    /**
150
     * Retourne les fichiers attachés à un produit
151
     *
152
     * @param  integer $file_product_id L'Id du produit
153
     * @param  integer $start           Position de départ
154
     * @param  integer $limit           Nombre maxi de produits à retourner
155
     * @return array   tableau d'objets de type oledrion_files
156
     */
157
    public function getProductFiles($file_product_id, $start = 0, $limit = 0)
158
    {
159
        $criteria = new Criteria('file_product_id', $file_product_id, '=');
160
        $criteria->setStart($start);
161
        $criteria->setLimit($limit);
162
163
        return $this->getObjects($criteria);
164
    }
165
166
    /**
167
     * Retourne le nombre de fichiers attachés à un produit qui sont des MP3
168
     *
169
     * @param  integer $file_product_id L'Id du produit
170
     * @return integer le nombre de fichiers MP3
171
     */
172
    public function getProductMP3Count($file_product_id)
173
    {
174
        $criteria = new CriteriaCompo();
175
        $criteria->add(new Criteria('file_product_id', $file_product_id, '='));
176
        $criteria->add(new Criteria('file_mimetype', 'audio/mpeg', '='));
177
178
        return $this->getCount($criteria);
179
    }
180
181
    /**
182
     * Retourne le nombre de fichiers attachés à un produit
183
     *
184
     * @param  integer $file_product_id L'Id du produit
185
     * @return integer le nombre de fichiers
186
     */
187
    public function getProductFilesCount($file_product_id)
188
    {
189
        $criteria = new Criteria('file_product_id', $file_product_id, '=');
190
191
        return $this->getCount($criteria);
192
    }
193
194
    /**
195
     * Supprime les fichiers attachés à un produit
196
     *
197
     * @param  integer $file_product_id L'Id du produit
198
     * @return void
199
     */
200
    public function deleteProductFiles($file_product_id)
201
    {
202
        $files    = array();
0 ignored issues
show
Unused Code introduced by
$files is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
203
        $criteria = new Criteria('file_product_id', $file_product_id, '=');
204
        $files    = $this->getObjects($criteria);
205
        if (count($files) > 0) {
206
            foreach ($files as $file) {
207
                $file->deleteAttachedFile();
208
                $this->delete($file, true);
209
            }
210
        }
211
    }
212
}
213