Completed
Push — symfony3-wololo-packages ( fecf70...6bf04d )
by Kamil
28:46 queued 11:35
created

HasAllPricesDefinedValidator::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.2
cc 4
eloc 10
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\CoreBundle\Validator\Constraints;
13
14
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
15
use Sylius\Component\Core\Model\ChannelPricingInterface;
16
use Symfony\Component\Validator\Constraint;
17
use Symfony\Component\Validator\ConstraintValidator;
18
use Webmozart\Assert\Assert;
19
20
/**
21
 * @author Mateusz Zalewski <[email protected]>
22
 */
23
final class HasAllPricesDefinedValidator extends ConstraintValidator
24
{
25
    /**
26
     * @var ChannelRepositoryInterface
27
     */
28
    private $channelRepository;
29
30
    /**
31
     * @param ChannelRepositoryInterface $channelRepository
32
     */
33
    public function __construct(ChannelRepositoryInterface $channelRepository)
34
    {
35
        $this->channelRepository = $channelRepository;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function validate($productVariant, Constraint $constraint)
42
    {
43
        Assert::isInstanceOf($constraint, HasAllPricesDefined::class);
44
45
        $channels = $productVariant->getProduct()->getChannels();
46
47
        foreach ($channels as $channel) {
48
            /** @var ChannelPricingInterface $channelPricing */
49
            $channelPricing = $productVariant->getChannelPricingForChannel($channel);
50
            if (null === $channelPricing || null === $channelPricing->getPrice()) {
51
                $this->context->buildViolation($constraint->message)
52
                    ->atPath('channelPricings')
53
                    ->addViolation()
54
                ;
55
56
                return;
57
            }
58
        }
59
    }
60
}
61