Completed
Push — master ( a44397...1b91d9 )
by Michał
55:03 queued 40:14
created

ZoneBasedConfigurationType::getZones()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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\Form\Type\Pricing;
13
14
use Sylius\Component\Resource\Repository\RepositoryInterface;
15
use Symfony\Component\Form\AbstractType;
16
use Symfony\Component\Form\FormBuilderInterface;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
19
/**
20
 * @author Paweł Jędrzejewski <[email protected]>
21
 */
22
class ZoneBasedConfigurationType extends AbstractType
23
{
24
    /**
25
     * @var RepositoryInterface
26
     */
27
    protected $zoneRepository;
28
29
    /**
30
     * @param RepositoryInterface $zoneRepository
31
     */
32
    public function __construct(RepositoryInterface $zoneRepository)
33
    {
34
        $this->zoneRepository = $zoneRepository;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function buildForm(FormBuilderInterface $builder, array $options)
41
    {
42
        foreach ($this->getZones($options) as $zone) {
43
            $builder
44
                ->add($zone->getId(), 'sylius_money', [
45
                    'label' => $zone->getName(),
46
                    'required' => false,
47
                ])
48
            ;
49
        }
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function configureOptions(OptionsResolver $resolver)
56
    {
57
        $resolver
58
            ->setDefaults([
59
                'data_class' => null,
60
                'scope' => null,
61
            ])
62
        ;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getName()
69
    {
70
        return 'sylius_price_calculator_zone_based';
71
    }
72
73
    /**
74
     * @param array $options
75
     *
76
     * @return array
77
     */
78
    private function getZones(array $options)
79
    {
80
        if (isset($options['scope'])) {
81
            return $this->zoneRepository->findBy(['scope' => $options['scope']]);
82
        }
83
84
        return $this->zoneRepository->findAll();
85
    }
86
}
87