Completed
Pull Request — master (#290)
by
unknown
36:49 queued 34:03
created

CartExistsValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A validate() 6 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\SyliusShopApiPlugin\Validator;
6
7
use Sylius\Component\Core\Model\OrderInterface;
8
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
9
use Symfony\Component\Validator\Constraint;
10
use Symfony\Component\Validator\ConstraintValidator;
11
12 View Code Duplication
final class CartExistsValidator extends ConstraintValidator
13
{
14
    /**
15
     * @var OrderRepositoryInterface
16
     */
17
    private $orderRepository;
18
19
    /**
20
     * @param OrderRepositoryInterface $orderRepository
21
     */
22
    public function __construct(OrderRepositoryInterface $orderRepository)
23
    {
24
        $this->orderRepository = $orderRepository;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function validate($token, Constraint $constraint)
31
    {
32
        if (null === $this->orderRepository->findOneBy(['tokenValue' => $token, 'state' => OrderInterface::STATE_CART])) {
33
            $this->context->addViolation($constraint->message);
34
        }
35
    }
36
}
37