Test Setup Failed
Push — master ( 6e2987...bf1336 )
by
unknown
03:38
created

unique-product-prices.js ➔ define   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
dl 0
loc 69
rs 9.2083
c 0
b 0
f 0
nop 1

2 Functions

Rating   Name   Duplication   Size   Complexity  
B unique-product-prices.js ➔ ... ➔ getStringifiedValues 0 19 9
A unique-product-prices.js ➔ ... ➔ 0 20 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
define(function(require) {
2
    'use strict';
3
4
    var _ = require('underscore');
5
    var $ = require('jquery');
6
    var __ = require('orotranslation/js/translator');
7
    var messenger = require('oroui/js/messenger');
8
9
    var defaultParam = {
10
        message: 'All product prices should be unique.'
11
    };
12
13
    /**
14
     * @param {HTMLElement} element
15
     * @return {string}
16
     */
17
    function getStringifiedValues(element) {
18
        var price = $(element);
19
20
        var priceList = price.find('input[name$="[priceList]"]').val();
21
        var quantity = price.find('input[name$="[quantity]"]').val();
22
        var unit = price.find('select[name$="[unit]"] option:selected').val();
23
        var currency = price.find('select[name$="[currency]"] option:selected').val();
24
25
        if (
26
            !priceList || !priceList.trim() ||
27
            !quantity || !quantity.trim() ||
28
            !unit || !unit.trim() ||
29
            !currency || !currency.trim()
30
        ) {
31
            return '';
32
        }
33
34
        return [priceList, parseFloat(quantity), unit, currency].join(':');
35
    }
36
37
    /**
38
     * @export oropricing/js/validator/unique-product-prices
39
     */
40
    return [
41
        'Oro\\Bundle\\PricingBundle\\Validator\\Constraints\\UniqueProductPrices',
42
        function(value, element) {
43
            var processedPrices = [];
44
            var $container = $(element).closest('.oro-item-collection');
45
46
            var duplicate = _.find($container.find('.oro-multiselect-holder'), function(price) {
47
                var stringifiedPrice = getStringifiedValues(price);
48
49
                if (stringifiedPrice === '') {
50
                    return;
51
                }
52
                if (processedPrices.indexOf(stringifiedPrice) !== -1) {
53
                    // duplicates are found
54
                    return true;
55
                } else {
56
                    processedPrices.push(stringifiedPrice);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
57
                }
58
            });
59
60
            return duplicate === void 0;
0 ignored issues
show
Coding Style introduced by
Consider using undefined instead of void(0). It is equivalent and more straightforward to read.
Loading history...
61
        },
62
        function(param) {
63
            param = _.extend({}, defaultParam, param);
64
            messenger.notificationFlashMessage('error', __(param.message, {}));
65
66
            return false;
67
        }
68
    ];
69
});
70