Completed
Push — master ( e19cd6...b62b09 )
by Jan
04:07
created

ValidPartLotValidator::validate()   C

Complexity

Conditions 13
Paths 48

Size

Total Lines 46
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 23
c 1
b 0
f 0
nc 48
nop 2
dl 0
loc 46
rs 6.6166

How to fix   Complexity   

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
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\Validator\Constraints;
33
34
35
use App\Entity\Parts\PartLot;
36
use Doctrine\ORM\EntityManagerInterface;
37
use Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass;
38
use Symfony\Component\Form\Exception\UnexpectedTypeException;
39
use Symfony\Component\Validator\Constraint;
40
use Symfony\Component\Validator\ConstraintValidator;
41
42
class ValidPartLotValidator extends ConstraintValidator
43
{
44
    protected $em;
45
46
    public function __construct(EntityManagerInterface $em)
47
    {
48
        $this->em = $em;
49
    }
50
51
    /**
52
     * Checks if the passed value is valid.
53
     *
54
     * @param mixed $value The value that should be validated
55
     * @param Constraint $constraint The constraint for the validation
56
     */
57
    public function validate($value, Constraint $constraint)
58
    {
59
        if (!$constraint instanceof ValidPartLot) {
60
            throw new UnexpectedTypeException($constraint, ValidPartLot::class);
61
        }
62
63
        if (!$value instanceof PartLot) {
64
            throw new UnexpectedTypeException($value, PartLot::class);
65
        }
66
67
        //We can only validate the values if we know the storelocation
68
        if ($value->getStorageLocation()) {
69
            $parts = $value->getStorageLocation()->getParts();
70
71
            //Check for isFull() attribute
72
            if ($value->getStorageLocation()->isFull()) {
73
                //Compare with saved amount value
74
                $db_lot = $this->em->getUnitOfWork()->getOriginalEntityData($value);
75
76
                //Amount increasment is not allowed
77
                if ($db_lot && $value->getAmount() > $db_lot['amount']) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $db_lot of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
78
                    $this->context->buildViolation('validator.part_lot.location_full.no_increasment')
79
                        ->setParameter('{{ old_amount }}', $db_lot['amount'])
80
                        ->atPath('amount')->addViolation();
81
                }
82
83
                if (!$parts->contains($value->getPart())) {
84
                    $this->context->buildViolation('validator.part_lot.location_full')
85
                        ->atPath('storage_location')->addViolation();
86
                }
87
            }
88
89
90
            //Check for onlyExisting
91
            if ($value->getStorageLocation()->isLimitToExistingParts()) {
92
                if (!$parts->contains($value->getPart())) {
93
                    $this->context->buildViolation('validator.part_lot.only_existing')
94
                        ->atPath('storage_location')->addViolation();
95
                }
96
            }
97
98
            //Check for only single part
99
            if ($value->getStorageLocation()->isLimitToExistingParts()) {
100
                if (($parts->count() > 0) && !$parts->contains($value->getPart())) {
101
                    $this->context->buildViolation('validator.part_lot.single_part')
102
                        ->atPath('storage_location')->addViolation();
103
                }
104
            }
105
        }
106
    }
107
}