Passed
Push — master ( 3340d7...94d7f4 )
by Jan
07:25
created

ValidProjectBuildRequestValidator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 48
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildViolationForLot() 0 5 1
B validate() 0 39 11
1
<?php
2
/*
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 *  Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 *  This program is free software: you can redistribute it and/or modify
8
 *  it under the terms of the GNU Affero General Public License as published
9
 *  by the Free Software Foundation, either version 3 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU Affero General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU Affero General Public License
18
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace App\Validator\Constraints\ProjectSystem;
22
23
use App\Entity\Parts\PartLot;
24
use App\Helpers\Projects\ProjectBuildRequest;
25
use Symfony\Component\Validator\Constraint;
26
use Symfony\Component\Validator\ConstraintValidator;
27
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
28
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
29
30
class ValidProjectBuildRequestValidator extends ConstraintValidator
31
{
32
    private function buildViolationForLot(PartLot $partLot, string $message): ConstraintViolationBuilderInterface
33
    {
34
        return $this->context->buildViolation($message)
35
            ->atPath('lot_' . $partLot->getID())
36
            ->setParameter('{{ lot }}', $partLot->getName());
37
    }
38
39
    public function validate($value, Constraint $constraint)
40
    {
41
        if (!$constraint instanceof ValidProjectBuildRequest) {
42
            throw new UnexpectedTypeException($constraint, ValidProjectBuildRequest::class);
43
        }
44
45
        if (null === $value || '' === $value) {
46
            return;
47
        }
48
49
        if (!$value instanceof ProjectBuildRequest) {
50
            throw new UnexpectedTypeException($value, ProjectBuildRequest::class);
51
        }
52
53
        foreach ($value->getPartBomEntries() as $bom_entry) {
54
            $withdraw_sum = $value->getWithdrawAmountSum($bom_entry);
55
            $needed_amount = $value->getNeededAmountForBOMEntry($bom_entry);
56
57
            foreach ($value->getPartLotsForBOMEntry($bom_entry) as $lot) {
58
                $withdraw_amount = $value->getLotWithdrawAmount($lot);
59
60
                if ($withdraw_amount < 0) {
61
                   $this->buildViolationForLot($lot, 'validator.project_build.lot_must_not_smaller_0')
62
                        ->addViolation();
63
                }
64
65
                if ($withdraw_amount > $lot->getAmount()) {
66
                    $this->buildViolationForLot($lot, 'validator.project_build.lot_must_not_bigger_than_stock')
67
                        ->addViolation();
68
                }
69
70
                if ($withdraw_sum > $needed_amount) {
71
                    $this->buildViolationForLot($lot, 'validator.project_build.lot_bigger_than_needed')
72
                        ->addViolation();
73
                }
74
75
                if ($withdraw_sum < $needed_amount) {
76
                    $this->buildViolationForLot($lot, 'validator.project_build.lot_smaller_than_needed')
77
                        ->addViolation();
78
                }
79
            }
80
        }
81
    }
82
}