Code Duplication    Length = 23-25 lines in 2 locations

models/Product.php 2 locations

@@ 426-450 (lines=25) @@
423
     * @throws \October\Rain\Database\ModelException
424
     * @return void
425
     */
426
    protected function validateInventories()
427
    {
428
        if (! is_array($this->optionsInventories) ||
429
            ! is_array($this->optionsInventories['options'])) {
430
            return;
431
        }
432
433
        $takenValueCombinations = [];
434
        foreach ($this->optionsInventories['inventories'] as $inventory) {
435
            // validate the inventory
436
            $model = new Inventory($inventory);
437
            $model->validate();
438
439
            // validate that the value combinations are unique
440
            sort($inventory['valueIds']);
441
            $valueCombination = json_encode($inventory['valueIds']);
442
443
            if (in_array($valueCombination, $takenValueCombinations)) {
444
                Flash::error(Lang::get('bedard.shop::lang.products.form.duplicate_inventories_error'));
445
                throw new ModelException($this);
446
            }
447
448
            $takenValueCombinations[] = $valueCombination;
449
        }
450
    }
451
452
    /**
453
     * Validate options.
@@ 458-480 (lines=23) @@
455
     * @throws \October\Rain\Database\ModelException
456
     * @return void
457
     */
458
    protected function validateOptions()
459
    {
460
        if (! is_array($this->optionsInventories) ||
461
            ! is_array($this->optionsInventories['options'])) {
462
            return;
463
        }
464
465
        $names = [];
466
        foreach ($this->optionsInventories['options'] as $option) {
467
            // validate the option
468
            $model = new Option($option);
469
            $model->validate();
470
471
            // validate that names are unique
472
            $name = strtolower(trim($option['name']));
473
474
            if (in_array($name, $names)) {
475
                Flash::error(Lang::get('bedard.shop::lang.products.form.duplicate_options_error'));
476
                throw new ModelException($this);
477
            }
478
479
            $names[] = $name;
480
        }
481
    }
482
}
483