GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AddressTypeExtension::onPostSetData()   B
last analyzed

Complexity

Conditions 7
Paths 19

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 28
nc 19
nop 1
dl 0
loc 45
rs 8.5386
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Odiseo\SyliusGeoPlugin\Form\Extension;
6
7
use Odiseo\SyliusGeoPlugin\Context\GeoContextInterface;
8
use Sylius\Bundle\AddressingBundle\Form\Type\AddressType;
9
use Sylius\Bundle\AddressingBundle\Form\Type\CountryCodeChoiceType;
10
use Sylius\Component\Addressing\Model\CountryInterface;
11
use Sylius\Component\Core\Model\AddressInterface;
12
use Sylius\Component\Resource\Repository\RepositoryInterface;
13
use Symfony\Component\Form\AbstractTypeExtension;
14
use Symfony\Component\Form\Extension\Core\Type\TextType;
15
use Symfony\Component\Form\FormBuilderInterface;
16
use Symfony\Component\Form\FormEvent;
17
use Symfony\Component\Form\FormEvents;
18
19
final class AddressTypeExtension extends AbstractTypeExtension
20
{
21
    /** @var GeoContextInterface */
22
    private $geoContext;
23
24
    /** @var RepositoryInterface */
25
    private $countryRepository;
26
27
    /** @var bool */
28
    private $enabledCityName;
29
30
    /** @var bool */
31
    private $enabledPostalCode;
32
33
    public function __construct(
34
        GeoContextInterface $geoContext,
35
        RepositoryInterface $countryRepository,
36
        bool $enabledCityName,
37
        bool $enabledPostalCode
38
    ) {
39
        $this->geoContext = $geoContext;
40
        $this->countryRepository = $countryRepository;
41
        $this->enabledCityName = $enabledCityName;
42
        $this->enabledPostalCode = $enabledPostalCode;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function buildForm(FormBuilderInterface $builder, array $options): void
49
    {
50
        $builder->addEventListener(
51
            FormEvents::POST_SET_DATA,
52
            [$this, 'onPostSetData']
53
        );
54
    }
55
56
    /**
57
     * @param FormEvent $event
58
     */
59
    public function onPostSetData(FormEvent $event): void
60
    {
61
        $address = $event->getData();
62
        $form = $event->getForm();
63
64
        if (!$address instanceof AddressInterface) {
65
            $countryCode = $this->geoContext->getCountryCode();
66
67
            $country = $this->countryRepository->findOneBy([
68
                'code' => $countryCode
69
            ]);
70
71
            if ($country instanceof CountryInterface) {
72
                $form
73
                    ->remove('countryCode')
74
                    ->add('countryCode', CountryCodeChoiceType::class, [
75
                        'label' => 'sylius.form.address.country',
76
                        'data' => $countryCode
77
                    ])
78
                ;
79
            }
80
81
            if ($this->enabledCityName) {
82
                $cityName = $this->geoContext->getCityName();
83
84
                if (null !== $cityName) {
85
                    $form
86
                        ->remove('city')
87
                        ->add('city', TextType::class, [
88
                            'label' => 'sylius.form.address.city',
89
                            'data' => $cityName
90
                        ])
91
                    ;
92
                }
93
            }
94
95
            if ($this->enabledPostalCode) {
96
                $postalCode = $this->geoContext->getPostalCode();
97
98
                if (null !== $postalCode) {
99
                    $form
100
                        ->remove('postcode')
101
                        ->add('postcode', TextType::class, [
102
                            'label' => 'sylius.form.address.postcode',
103
                            'data' => $postalCode
104
                        ])
105
                    ;
106
                }
107
            }
108
        }
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    public static function getExtendedTypes(): iterable
115
    {
116
        return [AddressType::class];
117
    }
118
}
119