upgrade phonenumber on 3party: migrations

This commit is contained in:
Julien Fastré 2022-03-02 15:50:01 +01:00
parent eb6ec8a4af
commit 6a3c8017f0
3 changed files with 77 additions and 10 deletions

View File

@ -21,6 +21,7 @@ use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Iterator;
use libphonenumber\PhoneNumberUtil;
use Nelmio\Alice\Loader\NativeLoader;
use Nelmio\Alice\ObjectSet;
@ -29,6 +30,13 @@ use function count;
class LoadThirdParty extends Fixture implements DependentFixtureInterface
{
private PhoneNumberUtil $phoneNumberUtil;
public function __construct()
{
$this->phoneNumberUtil = PhoneNumberUtil::getInstance();
}
public function getDependencies()
{
return [
@ -66,7 +74,7 @@ class LoadThirdParty extends Fixture implements DependentFixtureInterface
Address::class => [
'address1' => [
'name' => '<fr_FR:company()>',
'telephone' => '<fr_FR:phonenumber()>',
'telephone' => $this->phoneNumberUtil->getExampleNumber('FR'),
'email' => '<email()>',
'comment' => '<fr_FR:realTextBetween(10, 500)>',
],
@ -116,7 +124,7 @@ class LoadThirdParty extends Fixture implements DependentFixtureInterface
ThirdParty::class => [
'thirdparty{1..75}' => [
'name' => '<fr_FR:company()>',
'telephone' => '<fr_FR:phonenumber()>',
'telephone' => $this->phoneNumberUtil->getExampleNumber('FR'),
'email' => '<email()>',
'comment' => '<fr_FR:realTextBetween(10, 500)>',
'address' => '@address<current()>',

View File

@ -24,6 +24,7 @@ use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use libphonenumber\PhoneNumber;
use Symfony\Component\Serializer\Annotation\Context;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
use Symfony\Component\Serializer\Annotation\Groups;
@ -253,14 +254,14 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
private ?ThirdPartyProfession $profession = null;
/**
* @ORM\Column(name="telephone", type="string", length=64, nullable=true)
* @ORM\Column(name="telephone", type="phone_number", nullable=true)
* @Assert\Regex("/^([\+{1}])([0-9\s*]{4,20})$/",
* message="Invalid phone number: it should begin with the international prefix starting with ""+"", hold only digits and be smaller than 20 characters. Ex: +33123456789"
* )
* @PhonenumberConstraint(type="any")
* @Groups({"read", "write", "docgen:read", "docgen:read:3party:parent"})
*/
private ?string $telephone = null;
private ?PhoneNumber $telephone = null;
/**
* @ORM\Column(name="types", type="json", nullable=true)
@ -502,7 +503,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
/**
* Get telephone.
*/
public function getTelephone(): ?string
public function getTelephone(): ?PhoneNumber
{
return $this->telephone;
}
@ -821,12 +822,8 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
/**
* Set telephone.
*
* @param string|null $telephone
*
* @return ThirdParty
*/
public function setTelephone($telephone = null)
public function setTelephone(?PhoneNumber $telephone = null): self
{
$this->telephone = $telephone;

View File

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace Chill\Migrations\ThirdParty;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use libphonenumber\PhoneNumberUtil;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
final class Version20220302143821 extends AbstractMigration implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function getDescription(): string
{
return 'Upgrade phonenumber on third parties';
}
private function buildMigrationPhoneNumberClause(string $defaultCarriercode, string $field): string
{
$util = PhoneNumberUtil::getInstance();
$countryCode = $util->getCountryCodeForRegion($defaultCarriercode);
return sprintf('%s=CASE
WHEN %s = \'\' THEN NULL
WHEN LEFT(%s, 1) = \'0\'
THEN \'%s\' || replace(replace(substr(%s, 2), \'(0)\', \'\'), \' \', \'\')
ELSE replace(replace(%s, \'(0)\', \'\'),\' \', \'\')
END', $field, $field, $field, $countryCode, $field, $field);
}
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_3party.third_party ALTER telephone TYPE VARCHAR(35)');
$this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone DROP DEFAULT');
$this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone TYPE VARCHAR(35)');
$this->addSql('COMMENT ON COLUMN chill_3party.third_party.telephone IS \'(DC2Type:phone_number)\'');
$this->addSql(
'UPDATE chill_3party.third_party SET ' .
$this->buildMigrationPhonenumberClause($carrier_code, 'telephone')
);
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone TYPE VARCHAR(64)');
$this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone DROP DEFAULT');
$this->addSql('COMMENT ON COLUMN chill_3party.third_party.telephone IS NULL');
}
}