Completed
Push — master ( 895481...66b796 )
by Łukasz
07:18
created

CartExistsValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

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