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

Oledrion_caddy_attributes::addOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 28 and the first side effect is on line 23.

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 options (attributs) produits dans les commandes
22
 */
23
require __DIR__ . '/classheader.php';
24
25
/**
26
 * Class Oledrion_caddy_attributes
27
 */
28
class Oledrion_caddy_attributes 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...
29
{
30
    /**
31
     * constructor
32
     *
33
     * normally, this is called from child classes only
34
     *
35
     * @access public
36
     */
37
    public function __construct()
38
    {
39
        $this->initVar('ca_id', XOBJ_DTYPE_INT, null, false);
40
        $this->initVar('ca_cmd_id', XOBJ_DTYPE_INT, null, false);
41
        $this->initVar('ca_caddy_id', XOBJ_DTYPE_INT, null, false);
42
        $this->initVar('ca_attribute_id', XOBJ_DTYPE_INT, null, false);
43
        $this->initVar('ca_attribute_values', XOBJ_DTYPE_TXTAREA, null, false);
44
        $this->initVar('ca_attribute_names', XOBJ_DTYPE_TXTAREA, null, false);
45
        $this->initVar('ca_attribute_prices', XOBJ_DTYPE_TXTAREA, null, false);
46
    }
47
48
    /**
49
     * Retourne une option de l'attribut
50
     *
51
     * @param  string $valueToGet
52
     * @param  string $format
53
     * @return array
54
     * @since 2.3.2009.03.11
55
     */
56 View Code Duplication
    public function getOption($valueToGet, $format = 'e')
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...
57
    {
58
        $names = array();
59
        if (xoops_trim($this->getVar($valueToGet, $format)) != '') {
60
            $names = explode(OLEDRION_ATTRIBUTE_SEPARATOR, $this->getVar($valueToGet, $format));
61
        }
62
63
        return $names;
64
    }
65
66
    /**
67
     * Ajout d'une option à l'attribut (soit une option vide soit une option valorisée)
68
     *
69
     * @param  string $name
70
     * @param  string $value
71
     * @param  string $price
72
     * @return boolean
73
     * @since 2.3.2009.03.16
74
     */
75
    private function appendOption($name, $value, $price = '')
76
    {
77
        $names  = $values = $prices = array();
0 ignored issues
show
Unused Code introduced by
$values 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...
Unused Code introduced by
$names 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...
78
        $format = 'e';
79
        $names  = $this->getOption('ca_attribute_names', $format);
80
        $values = $this->getOption('ca_attribute_values', $format);
81
        if (Oledrion_utils::getModuleOption('use_price')) {
82
            $prices = $this->getOption('ca_attribute_prices', $format);
83
        }
84
        $names[]  = $name;
85
        $values[] = $value;
86
        if (Oledrion_utils::getModuleOption('use_price')) {
87
            $prices[] = $price;
88
        }
89
        $this->setVar('ca_attribute_names', implode(OLEDRION_ATTRIBUTE_SEPARATOR, $names));
90
        $this->setVar('ca_attribute_values', implode(OLEDRION_ATTRIBUTE_SEPARATOR, $values));
91
        if (Oledrion_utils::getModuleOption('use_price')) {
92
            $this->setVar('ca_attribute_prices', implode(OLEDRION_ATTRIBUTE_SEPARATOR, $prices));
93
        }
94
95
        return true;
96
    }
97
98
    /**
99
     * Ajoute une nouvelle option à l'attribut
100
     *
101
     * @param  string $name
102
     * @param  string $value
103
     * @param  string $price
104
     * @return boolean
105
     * @since 2.3.2009.03.16
106
     */
107
    public function addOption($name, $value, $price = '')
108
    {
109
        return $this->appendOption($name, $value, $price);
110
    }
111
112
    /**
113
     * Retourne les informations formatées de l'attribut pour affichage dans la facture
114
     *
115
     * @param  oledrion_products $product Le produit concerné par l'attribut
116
     * @param  string            $format
117
     * @return array
118
     * @since 2.3.2009.03.23
119
     */
120
    public function renderForInvoice(Oledrion_products $product, $format = 's')
121
    {
122
        $names = $prices = $ret = array();
0 ignored issues
show
Unused Code introduced by
$names 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...
123
        $names = $this->getOption('ca_attribute_names', $format);
124
        if (Oledrion_utils::getModuleOption('use_price')) {
125
            $prices = $this->getOption('ca_attribute_prices', $format);
126
        }
127
128
        $oledrion_Currency = Oledrion_Currency::getInstance();
129
        $counter           = 0;
130
        foreach ($names as $name) {
131
            $price = 0;
132
            if (Oledrion_utils::getModuleOption('use_price')) {
133
                if (isset($prices[$counter])) {
134
                    $price = Oledrion_utils::getAmountWithVat((float)$prices[$counter], $product->getVar('product_vat_id'));
0 ignored issues
show
Documentation introduced by
(double) $prices[$counter] is of type double, but the function expects a object<floatval>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
135
                    $price = $oledrion_Currency->amountForDisplay($price);
136
                }
137
            }
138
            $ret[] = array('ca_attribute_name' => $name, 'ca_attribute_price_formated' => $price);
139
            ++$counter;
140
        }
141
142
        return $ret;
143
    }
144
}
145
146
/**
147
 * Class OledrionOledrion_caddy_attributesHandler
148
 */
149
class OledrionOledrion_caddy_attributesHandler 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...
150
{
151
    /**
152
     * OledrionOledrion_caddy_attributesHandler constructor.
153
     * @param XoopsDatabase|null $db
154
     */
155
    public function __construct(XoopsDatabase $db)
156
    { //                                Table                   Classe                      Id
157
        parent::__construct($db, 'oledrion_caddy_attributes', 'oledrion_caddy_attributes', 'ca_id');
158
    }
159
160
    /**
161
     * Retourne le nombre d'attributs liés à un caddy
162
     *
163
     * @param  integer $ca_caddy_id L'ID du caddy concerné
164
     * @return integer
165
     * @since 2.3.2009.03.23
166
     */
167
    public function getAttributesCountForCaddy($ca_caddy_id)
168
    {
169
        return $this->getCount(new Criteria('ca_caddy_id', $ca_caddy_id, '='));
170
    }
171
172
    /**
173
     * Retourne la liste formatée des attributs liés à un caddy
174
     *
175
     * @param  integer                  $ca_caddy_id L'identifiant de caddy
176
     * @param  object|oledrion_products $product     Le produit concerné par le caddy
177
     * @return array
178
     * @since 2.3.2009.03.23
179
     */
180
    public function getFormatedAttributesForCaddy($ca_caddy_id, Oledrion_products $product)
181
    {
182
        $handlers   = OledrionHandler::getInstance();
183
        $attributes = $ret = array();
0 ignored issues
show
Unused Code introduced by
$attributes 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...
184
        $attributes = $this->getObjects(new Criteria('ca_caddy_id', $ca_caddy_id, '='));
185
        if (count($attributes) == 0) {
186
            return $ret;
187
        }
188
        foreach ($attributes as $caddyAttribute) {
189
            $data      = array();
190
            $attribute = null;
0 ignored issues
show
Unused Code introduced by
$attribute 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...
191
            $attribute = $handlers->h_oledrion_attributes->get($caddyAttribute->getVar('ca_attribute_id'));
192
            if (is_object($attribute)) {
193
                $data = $attribute->toArray();
194
            }
195
            $data['attribute_options'] = $caddyAttribute->renderForInvoice($product);
196
            $ret[]                     = $data;
197
        }
198
199
        return $ret;
200
    }
201
202
    /**
203
     * Retourne le nombre de caddy attributs liés à un attribut
204
     *
205
     * @param  integer $ca_attribute_id L'Identifiant de l'attribut concerné
206
     * @return integer
207
     * @since 2.3.2009.03.23
208
     */
209
    public function getCaddyCountFromAttributeId($ca_attribute_id)
210
    {
211
        return $this->getCount(new Criteria('ca_attribute_id', $ca_attribute_id, '='));
212
    }
213
214
    /**
215
     * Retourne la liste des numéros de commandes "liés" à un attribut
216
     *
217
     * @param  integer $ca_attribute_id
218
     * @return array
219
     */
220
    public function getCommandIdFromAttribute($ca_attribute_id)
221
    {
222
        $ret       = $ordersIds = array();
0 ignored issues
show
Unused Code introduced by
$ordersIds 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...
223
        $criteria  = new Criteria('ca_attribute_id', $ca_attribute_id, '=');
224
        $ordersIds = $this->getObjects($criteria, false, true, 'ca_cmd_id', false);
225
        foreach ($ordersIds as $order) {
226
            $ret[] = $order->ca_cmd_id;
227
        }
228
229
        return $ret;
230
    }
231
232
    /**
233
     * Supprime les caddies associés à une commande
234
     *
235
     * @param $ca_cmd_id
236
     * @return bool
237
     * @internal param int $caddy_cmd_id
238
     */
239
    public function removeCartsFromOrderId($ca_cmd_id)
240
    {
241
        $ca_cmd_id = (int)$ca_cmd_id;
242
243
        return $this->deleteAll(new criteria('ca_cmd_id', $ca_cmd_id, '='));
244
    }
245
}
246