From e9ffdb1f035dcdb7db4243ae46ed61f98fab3a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 2 Mar 2022 14:11:47 +0100 Subject: [PATCH] fix cs --- .../DependencyInjection/Configuration.php | 20 +- .../Phonenumber/PhonenumberHelper.php | 6 +- .../Phonenumber/Templating.php | 3 - .../Normalizer/PhonenumberNormalizer.php | 46 ++--- .../Phonenumber/PhonenumberHelperTest.php | 174 +++++++++--------- .../ChillPersonBundle/Form/PersonType.php | 7 +- ...dentialCourseMustHaveReferrerValidator.php | 9 + .../migrations/Version20220215135509.php | 102 +++++----- 8 files changed, 192 insertions(+), 175 deletions(-) diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/Configuration.php b/src/Bundle/ChillMainBundle/DependencyInjection/Configuration.php index 942ea60f1..be8baf79a 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/Configuration.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/Configuration.php @@ -89,16 +89,16 @@ class Configuration implements ConfigurationInterface ->end() ->end() // end of notifications ->arrayNode('phone_helper') - ->canBeUnset() - ->children() - ->scalarNode('twilio_sid') - ->defaultNull() - ->end() - ->scalarNode('twilio_secret') - ->defaultNull() - ->end() - ->scalarNode('default_carrier_code') - ->defaultNull() + ->canBeUnset() + ->children() + ->scalarNode('twilio_sid') + ->defaultNull() + ->end() + ->scalarNode('twilio_secret') + ->defaultNull() + ->end() + ->scalarNode('default_carrier_code') + ->defaultNull() ->end() ->end() ->end() diff --git a/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php b/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php index 1f0827bde..26f6b6e2a 100644 --- a/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php +++ b/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php @@ -43,10 +43,10 @@ final class PhonenumberHelper implements PhoneNumberHelperInterface private LoggerInterface $logger; - private Client $twilioClient; - private PhonenumberUtil $phoneNumberUtil; + private Client $twilioClient; + public function __construct( CacheItemPoolInterface $cacheUserData, ParameterBagInterface $parameterBag, @@ -80,7 +80,7 @@ final class PhonenumberHelper implements PhoneNumberHelperInterface /** * @param string $phoneNumber A national phone number starting with + - * @return string + * * @throws NumberParseException */ public function format(PhoneNumber $phoneNumber): string diff --git a/src/Bundle/ChillMainBundle/Phonenumber/Templating.php b/src/Bundle/ChillMainBundle/Phonenumber/Templating.php index 4f134c8ff..c07d8c5a8 100644 --- a/src/Bundle/ChillMainBundle/Phonenumber/Templating.php +++ b/src/Bundle/ChillMainBundle/Phonenumber/Templating.php @@ -16,9 +16,6 @@ use Twig\TwigFilter; class Templating extends AbstractExtension { - /** - * @var PhonenumberHelper - */ protected PhonenumberHelper $phonenumberHelper; public function __construct(PhonenumberHelper $phonenumberHelper) diff --git a/src/Bundle/ChillMainBundle/Serializer/Normalizer/PhonenumberNormalizer.php b/src/Bundle/ChillMainBundle/Serializer/Normalizer/PhonenumberNormalizer.php index 88422115d..ac4f3fa61 100644 --- a/src/Bundle/ChillMainBundle/Serializer/Normalizer/PhonenumberNormalizer.php +++ b/src/Bundle/ChillMainBundle/Serializer/Normalizer/PhonenumberNormalizer.php @@ -1,47 +1,40 @@ defaultCarrierCode = $parameterBag->get('chill_main')['phone_helper']['default_carrier_code']; $this->phoneNumberUtil = PhoneNumberUtil::getInstance(); } - public function normalize($object, string $format = null, array $context = []): string - { - return $this->phoneNumberUtil->formatOutOfCountryCallingNumber($object, $this->defaultCarrierCode); - } - - public function supportsNormalization($data, string $format = null) - { - return $data instanceof PhoneNumber; - } - /** - * {@inheritdoc} - * * @throws UnexpectedValueException */ - public function denormalize($data, $class, $format = null, array $context = array()) + public function denormalize($data, $class, $format = null, array $context = []) { try { return $this->phoneNumberUtil->parse($data, $this->defaultCarrierCode); @@ -50,11 +43,18 @@ class PhonenumberNormalizer implements NormalizerInterface, DenormalizerInterfac } } - /** - * {@inheritdoc} - */ + public function normalize($object, ?string $format = null, array $context = []): string + { + return $this->phoneNumberUtil->formatOutOfCountryCallingNumber($object, $this->defaultCarrierCode); + } + public function supportsDenormalization($data, $type, $format = null) { - return $type === 'libphonenumber\PhoneNumber'; + return 'libphonenumber\PhoneNumber' === $type; + } + + public function supportsNormalization($data, ?string $format = null) + { + return $data instanceof PhoneNumber; } } diff --git a/src/Bundle/ChillMainBundle/Tests/Phonenumber/PhonenumberHelperTest.php b/src/Bundle/ChillMainBundle/Tests/Phonenumber/PhonenumberHelperTest.php index 606fe13e3..d344af3ed 100644 --- a/src/Bundle/ChillMainBundle/Tests/Phonenumber/PhonenumberHelperTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Phonenumber/PhonenumberHelperTest.php @@ -12,30 +12,90 @@ declare(strict_types=1); namespace Chill\MainBundle\Tests\Routing\Loader; use Chill\MainBundle\Phonenumber\PhonenumberHelper; -use Chill\MainBundle\Phonenumber\PhoneNumberHelperInterface; use Psr\Log\NullLogger; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\Cache\Adapter\ArrayAdapter; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; +/** + * @internal + * @coversNothing + */ final class PhonenumberHelperTest extends KernelTestCase { - /** - * @dataProvider normalizePhonenumbers - */ - public function testNormalizePhonenumbers(string $defaultCarrierCode, string $phoneNumber, string $expected) + public function denormalizePhonenumbers() { - $subject = new PhonenumberHelper( - new ArrayAdapter(), - new ParameterBag([ - 'chill_main.phone_helper' => [ - 'default_carrier_code' => $defaultCarrierCode, - ], - ]), - new NullLogger() - ); + yield [ + 'BE', + '+3281136917', + '+3281136917', + ]; - $this->assertEquals($expected, $subject->normalize($phoneNumber)); + yield [ + 'BE', + '+33 6 23 12 45 54', + '+33623124554', + ]; + } + + public function formatPhonenumbers() + { + yield [ + 'BE', + '+3281136917', + '081 13 69 17', + ]; + + yield [ + 'FR', + '+33 6 23 12 45 54', + '06 23 12 45 54', + ]; + + yield [ + 'FR', + '+32 81 13 69 17', + '081 13 69 17', + ]; + + yield [ + 'BE', + '+33 6 23 12 45 54', + '06 23 12 45 54', + ]; + } + + public function normalizePhonenumbers() + { + yield [ + 'BE', + '081136917', + '+3281136917', + ]; + + yield [ + 'BE', + '003281136917', + '+3281136917', + ]; + + yield [ + 'BE', + '0032478123456', + '+32478123456', + ]; + + yield [ + 'BE', + '0478123456', + '+32478123456', + ]; + + yield [ + 'FR', + '0623124554', + '+33623124554', + ]; } /** @@ -74,75 +134,21 @@ final class PhonenumberHelperTest extends KernelTestCase $this->assertEquals($expected, $subject->format($phoneNumber)); } - public function formatPhonenumbers() { - yield [ - 'BE', - '+3281136917', - '081 13 69 17', - ]; + /** + * @dataProvider normalizePhonenumbers + */ + public function testNormalizePhonenumbers(string $defaultCarrierCode, string $phoneNumber, string $expected) + { + $subject = new PhonenumberHelper( + new ArrayAdapter(), + new ParameterBag([ + 'chill_main.phone_helper' => [ + 'default_carrier_code' => $defaultCarrierCode, + ], + ]), + new NullLogger() + ); - yield [ - 'FR', - '+33 6 23 12 45 54', - '06 23 12 45 54', - ]; - - yield [ - 'FR', - '+32 81 13 69 17', - '081 13 69 17', - ]; - - yield [ - 'BE', - '+33 6 23 12 45 54', - '06 23 12 45 54', - ]; - } - - public function normalizePhonenumbers() { - yield [ - 'BE', - '081136917', - '+3281136917', - ]; - - yield [ - 'BE', - '003281136917', - '+3281136917', - ]; - - yield [ - 'BE', - '0032478123456', - '+32478123456', - ]; - - yield [ - 'BE', - '0478123456', - '+32478123456', - ]; - - yield [ - 'FR', - '0623124554', - '+33623124554', - ]; - } - - public function denormalizePhonenumbers() { - yield [ - 'BE', - '+3281136917', - '+3281136917', - ]; - - yield [ - 'BE', - '+33 6 23 12 45 54', - '+33623124554', - ]; + $this->assertEquals($expected, $subject->normalize($phoneNumber)); } } diff --git a/src/Bundle/ChillPersonBundle/Form/PersonType.php b/src/Bundle/ChillPersonBundle/Form/PersonType.php index 05f5aa562..804757986 100644 --- a/src/Bundle/ChillPersonBundle/Form/PersonType.php +++ b/src/Bundle/ChillPersonBundle/Form/PersonType.php @@ -28,13 +28,10 @@ use Chill\PersonBundle\Form\Type\GenderType; use Chill\PersonBundle\Form\Type\PersonAltNameType; use Chill\PersonBundle\Form\Type\PersonPhoneType; use Chill\PersonBundle\Form\Type\Select2MaritalStatusType; -use Doctrine\ORM\EntityRepository; -use Doctrine\ORM\QueryBuilder; use libphonenumber\PhoneNumberFormat; use libphonenumber\PhoneNumberType as LibphonenumberPhoneNumberType; use libphonenumber\PhoneNumberUtil; use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType; -use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\CallbackTransformer; @@ -153,7 +150,7 @@ class PersonType extends AbstractType 'format' => PhoneNumberFormat::NATIONAL, 'required' => false, 'attr' => [ - 'placeholder' => PhoneNumberUtil::getInstance()->format($examplePhoneNumber, PhoneNumberFormat::NATIONAL) + 'placeholder' => PhoneNumberUtil::getInstance()->format($examplePhoneNumber, PhoneNumberFormat::NATIONAL), ], ] ); @@ -175,7 +172,7 @@ class PersonType extends AbstractType 'format' => PhoneNumberFormat::NATIONAL, 'required' => false, 'attr' => [ - 'placeholder' => PhoneNumberUtil::getInstance()->format($examplePhoneNumber, PhoneNumberFormat::NATIONAL) + 'placeholder' => PhoneNumberUtil::getInstance()->format($examplePhoneNumber, PhoneNumberFormat::NATIONAL), ], ] ) diff --git a/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/ConfidentialCourseMustHaveReferrerValidator.php b/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/ConfidentialCourseMustHaveReferrerValidator.php index bc2cd3256..2e19488bc 100644 --- a/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/ConfidentialCourseMustHaveReferrerValidator.php +++ b/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/ConfidentialCourseMustHaveReferrerValidator.php @@ -1,5 +1,14 @@ container + ->getParameter('chill_main')['phone_helper']['default_carrier_code']; + + if (null === $carrier_code) { + throw new RuntimeException('no carrier code'); + } + + $this->addSql('ALTER TABLE chill_person_person ALTER phonenumber TYPE TEXT'); + $this->addSql('ALTER TABLE chill_person_person ALTER phonenumber DROP DEFAULT'); + $this->addSql('ALTER TABLE chill_person_person ALTER phonenumber DROP NOT NULL'); + $this->addSql('COMMENT ON COLUMN chill_person_person.phonenumber IS NULL'); + + $this->addSql('ALTER TABLE chill_person_person ALTER mobilenumber TYPE TEXT'); + $this->addSql('ALTER TABLE chill_person_person ALTER mobilenumber DROP DEFAULT'); + $this->addSql('ALTER TABLE chill_person_person ALTER mobilenumber DROP NOT NULL'); + $this->addSql('COMMENT ON COLUMN chill_person_person.mobilenumber IS NULL'); + + $this->addSql( + 'UPDATE chill_person_person SET ' . + $this->buildMigrationPhonenumberClause($carrier_code, 'phonenumber') . + ', ' . + $this->buildMigrationPhoneNumberClause($carrier_code, 'mobilenumber') + ); + + $this->addSql('ALTER TABLE chill_person_phone ALTER phonenumber TYPE TEXT'); + $this->addSql('ALTER TABLE chill_person_phone ALTER phonenumber DROP DEFAULT'); + $this->addSql('ALTER TABLE chill_person_phone ALTER phonenumber DROP NOT NULL'); + $this->addSql('COMMENT ON COLUMN chill_person_phone.phonenumber IS NULL'); + + $this->addSql( + 'UPDATE chill_person_phone SET ' . + $this->buildMigrationPhoneNumberClause($carrier_code, 'phonenumber') + ); + } + private function buildMigrationPhoneNumberClause(string $defaultCarriercode, string $field): string { $util = PhoneNumberUtil::getInstance(); @@ -30,49 +83,4 @@ final class Version20220215135509 extends AbstractMigration implements Container ELSE replace(replace(%s, \'(0)\', \'\'),\' \', \'\') END', $field, $field, $countryCode, $field, $field); } - - public function getDescription(): string - { - return 'Update phone numbers for person'; - } - - public function up(Schema $schema): void - { - $carrier_code = $this->container - ->getParameter('chill_main')['phone_helper']['default_carrier_code']; - - if (null === $carrier_code) { - throw new \RuntimeException("no carrier code"); - } - - $this->addSql('ALTER TABLE chill_person_person ALTER phonenumber TYPE TEXT'); - $this->addSql('ALTER TABLE chill_person_person ALTER phonenumber DROP DEFAULT'); - $this->addSql('ALTER TABLE chill_person_person ALTER phonenumber DROP NOT NULL'); - $this->addSql('COMMENT ON COLUMN chill_person_person.phonenumber IS NULL'); - - $this->addSql('ALTER TABLE chill_person_person ALTER mobilenumber TYPE TEXT'); - $this->addSql('ALTER TABLE chill_person_person ALTER mobilenumber DROP DEFAULT'); - $this->addSql('ALTER TABLE chill_person_person ALTER mobilenumber DROP NOT NULL'); - $this->addSql('COMMENT ON COLUMN chill_person_person.mobilenumber IS NULL'); - - $this->addSql('UPDATE chill_person_person SET '. - $this->buildMigrationPhonenumberClause($carrier_code, 'phonenumber'). - ', '. - $this->buildMigrationPhoneNumberClause($carrier_code, 'mobilenumber') - ); - - $this->addSql('ALTER TABLE chill_person_phone ALTER phonenumber TYPE TEXT'); - $this->addSql('ALTER TABLE chill_person_phone ALTER phonenumber DROP DEFAULT'); - $this->addSql('ALTER TABLE chill_person_phone ALTER phonenumber DROP NOT NULL'); - $this->addSql('COMMENT ON COLUMN chill_person_phone.phonenumber IS NULL'); - - $this->addSql('UPDATE chill_person_phone SET '. - $this->buildMigrationPhoneNumberClause($carrier_code, 'phonenumber') - ); - } - - public function down(Schema $schema): void - { - throw new Exception('You should not do that.'); - } }