CropTypeAspectRatioValidationConstraintValidator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 9 3
1
<?php
2
3
namespace Drupal\crop\Plugin\Validation\Constraint;
4
5
use Symfony\Component\Validator\Constraint;
6
use Symfony\Component\Validator\ConstraintValidator;
7
8
/**
9
 * Checks if the crop type is valid.
10
 */
11
class CropTypeAspectRatioValidationConstraintValidator extends ConstraintValidator {
12
13
  /**
14
   * Validator 2.5 and upwards compatible execution context.
15
   *
16
   * @var \Symfony\Component\Validator\Context\ExecutionContextInterface
17
   */
18
  protected $context;
19
20
  /**
21
   * {@inheritdoc}
22
   */
23
  public function validate($value, Constraint $constraint) {
24
    /** @var \Drupal\crop\Entity\CropType $value */
25
    $aspect_ratio = $value->getAspectRatio();
26
    if (!empty($aspect_ratio) && !preg_match($value::VALIDATION_REGEXP, $aspect_ratio)) {
27
      $this->context->buildViolation($constraint->message)
28
        ->atPath('aspect_ratio')
29
        ->addViolation();
30
    }
31
  }
32
33
}
34