send message from cli through ovh if configured

This commit is contained in:
2022-06-13 21:39:29 +02:00
parent 616be5cc8a
commit 4c0fef4f44
19 changed files with 540 additions and 57 deletions

View File

@@ -1,5 +1,14 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\CalendarBundle\Command;
use Chill\CalendarBundle\Entity\Calendar;
@@ -7,48 +16,44 @@ use Chill\CalendarBundle\Service\ShortMessageNotification\ShortMessageForCalenda
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Phonenumber\PhoneNumberHelperInterface;
use Chill\MainBundle\Repository\UserRepositoryInterface;
use Chill\MainBundle\Service\ShortMessage\ShortMessage;
use Chill\MainBundle\Service\ShortMessage\ShortMessageSenderInterface;
use Chill\MainBundle\Service\ShortMessage\ShortMessageTransporterInterface;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\PersonRepository;
use DateInterval;
use DateTimeImmutable;
use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberType;
use libphonenumber\PhoneNumberUtil;
use libphonenumber\ValidationResult;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use UnexpectedValueException;
use function count;
class SendTestShortMessageOnCalendarCommand extends Command
{
private PersonRepository $personRepository;
private ShortMessageForCalendarBuilderInterface $messageForCalendarBuilder;
private PhoneNumberUtil $phoneNumberUtil;
private PersonRepository $personRepository;
private PhoneNumberHelperInterface $phoneNumberHelper;
private ShortMessageForCalendarBuilderInterface $messageForCalendarBuilder;
private PhoneNumberUtil $phoneNumberUtil;
private ShortMessageSenderInterface $messageSender;
private ShortMessageTransporterInterface $transporter;
private UserRepositoryInterface $userRepository;
/**
* @param PersonRepository $personRepository
* @param PhoneNumberUtil $phoneNumberUtil
* @param PhoneNumberHelperInterface $phoneNumberHelper
* @param ShortMessageForCalendarBuilderInterface $messageForCalendarBuilder
* @param ShortMessageSenderInterface $messageSender
*/
public function __construct(
PersonRepository $personRepository,
PhoneNumberUtil $phoneNumberUtil,
PhoneNumberHelperInterface $phoneNumberHelper,
ShortMessageForCalendarBuilderInterface $messageForCalendarBuilder,
ShortMessageSenderInterface $messageSender,
ShortMessageTransporterInterface $transporter,
UserRepositoryInterface $userRepository
) {
parent::__construct();
@@ -57,11 +62,10 @@ class SendTestShortMessageOnCalendarCommand extends Command
$this->phoneNumberUtil = $phoneNumberUtil;
$this->phoneNumberHelper = $phoneNumberHelper;
$this->messageForCalendarBuilder = $messageForCalendarBuilder;
$this->messageSender = $messageSender;
$this->transporter = $transporter;
$this->userRepository = $userRepository;
}
public function getName()
{
return 'chill:calendar:test-send-short-message';
@@ -81,41 +85,41 @@ class SendTestShortMessageOnCalendarCommand extends Command
$helper = $this->getHelper('question');
// start date
$question = new Question("When will start the appointment ? (default: \"1 hour\") ", '1 hour');
$startDate = new \DateTimeImmutable($helper->ask($input, $output, $question));
$question = new Question('When will start the appointment ? (default: "1 hour") ', '1 hour');
$startDate = new DateTimeImmutable($helper->ask($input, $output, $question));
if (false === $startDate) {
throw new \UnexpectedValueException("could not create a date with this date and time");
throw new UnexpectedValueException('could not create a date with this date and time');
}
$calendar->setStartDate($startDate);
// end date
$question = new Question("How long will last the appointment ? (default: \"PT30M\") ", 'PT30M');
$interval = new \DateInterval($helper->ask($input, $output, $question));
$question = new Question('How long will last the appointment ? (default: "PT30M") ', 'PT30M');
$interval = new DateInterval($helper->ask($input, $output, $question));
if (false === $interval) {
throw new \UnexpectedValueException("could not create the interval");
throw new UnexpectedValueException('could not create the interval');
}
$calendar->setEndDate($calendar->getStartDate()->add($interval));
// a person
$question = new Question("Who will participate ? Give an id for a person. ");
$question = new Question('Who will participate ? Give an id for a person. ');
$question
->setValidator(function ($answer): Person {
if (!is_numeric($answer)) {
throw new \UnexpectedValueException('the answer must be numeric');
throw new UnexpectedValueException('the answer must be numeric');
}
if (0 >= (int) $answer) {
throw new \UnexpectedValueException('the answer must be greater than zero');
throw new UnexpectedValueException('the answer must be greater than zero');
}
$person = $this->personRepository->find((int) $answer);
if (null === $person) {
throw new \UnexpectedValueException("The person is not found");
throw new UnexpectedValueException('The person is not found');
}
return $person;
@@ -124,23 +128,22 @@ class SendTestShortMessageOnCalendarCommand extends Command
$person = $helper->ask($input, $output, $question);
$calendar->addPerson($person);
// a main user
$question = new Question("Who will be the main user ? Give an id for a user. ");
$question = new Question('Who will be the main user ? Give an id for a user. ');
$question
->setValidator(function ($answer): User {
if (!is_numeric($answer)) {
throw new \UnexpectedValueException('the answer must be numeric');
throw new UnexpectedValueException('the answer must be numeric');
}
if (0 >= (int) $answer) {
throw new \UnexpectedValueException('the answer must be greater than zero');
throw new UnexpectedValueException('the answer must be greater than zero');
}
$user = $this->userRepository->find((int) $answer);
if (null === $user) {
throw new \UnexpectedValueException("The user is not found");
throw new UnexpectedValueException('The user is not found');
}
return $user;
@@ -150,21 +153,22 @@ class SendTestShortMessageOnCalendarCommand extends Command
$calendar->setMainUser($user);
// phonenumber
$question = new Question("To which number are we going to send this fake message ?",
null !== $person->getMobilenumber() ?
$this->phoneNumberHelper->format($person->getMobilenumber()):
null
$phonenumberFormatted = null !== $person->getMobilenumber() ?
$this->phoneNumberUtil->format($person->getMobilenumber(), PhoneNumberFormat::E164) : '';
$question = new Question(
sprintf('To which number are we going to send this fake message ? (default to: %s)', $phonenumberFormatted),
$phonenumberFormatted
);
$question->setNormalizer(function ($answer): PhoneNumber {
if (null === $answer) {
throw new \UnexpectedValueException("The person is not found");
throw new UnexpectedValueException('The person is not found');
}
$phone = $this->phoneNumberUtil->parse($answer, 'BE');
if (!$this->phoneNumberUtil->isPossibleNumberForType($phone, PhoneNumberType::MOBILE)) {
throw new \UnexpectedValueException("Phone number si not a mobile");
throw new UnexpectedValueException('Phone number si not a mobile');
}
return $phone;
@@ -172,6 +176,9 @@ class SendTestShortMessageOnCalendarCommand extends Command
$phone = $helper->ask($input, $output, $question);
$question = new ConfirmationQuestion('really send the message to the phone ?');
$reallySend = (bool) $helper->ask($input, $output, $question);
$messages = $this->messageForCalendarBuilder->buildMessageForCalendar($calendar);
if (0 === count($messages)) {
@@ -179,10 +186,13 @@ class SendTestShortMessageOnCalendarCommand extends Command
}
foreach ($messages as $key => $message) {
$output->writeln("The short message for SMS ${key} will be: ");
$output->writeln("The short message for SMS {$key} will be: ");
$output->writeln($message->getContent());
$message->setPhoneNumber($phone);
$this->messageSender->send($message);
if ($reallySend) {
$this->transporter->send($message);
}
}
return 0;

View File

@@ -30,7 +30,7 @@ class Configuration implements ConfigurationInterface
->children()
->arrayNode('short_messages')
->canBeDisabled()
->children()->end()
->children()->end()
->end() // end for short_messages
->arrayNode('remote_calendars_sync')->canBeEnabled()
->children()

View File

@@ -1 +1 @@
Votre travailleur social {{ calendar.mainUser.label }} vous rencontrera le {{ calendar.startDate|format_date('short') }} à {{ calendar.startDate|format_time('medium') }} - LIEU. {% if calendar.location is not null and calendar.location.phonenumber is not null %}En cas dindisponibilité rappelez-nous au {{ calendar.location.phonenumber|chill_format_phonenumber }}.{% endif %}
Votre travailleur social {{ calendar.mainUser.label }} vous rencontrera le {{ calendar.startDate|format_date('short', locale='fr') }} à {{ calendar.startDate|format_time('short', locale='fr') }} - LIEU.{% if calendar.mainUser.mainLocation is not null and calendar.mainUser.mainLocation.phonenumber1 is not null %} En cas d'indisponibilité, appelez-nous au {{ calendar.mainUser.mainLocation.phonenumber1|chill_format_phonenumber }}.{% endif %}

View File

@@ -0,0 +1,68 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\CalendarBundle\Service\ShortMessageNotification;
use DateInterval;
use Monolog\DateTimeImmutable;
use UnexpectedValueException;
/**
* * Lundi => Envoi des rdv du mardi et mercredi.
* * Mardi => Envoi des rdv du jeudi.
* * Mercredi => Envoi des rdv du vendredi
* * Jeudi => envoi des rdv du samedi et dimanche
* * Vendredi => Envoi des rdv du lundi.
*/
class DefaultRangeGenerator implements RangeGeneratorInterface
{
public function generateRange(\DateTimeImmutable $date): array
{
$onMidnight = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $date->format('Y-m-d') . ' 00:00:00');
switch ($dow = (int) $onMidnight->format('w')) {
case 6: // Saturday
case 0: // Sunday
return ['startDate' => null, 'endDate' => null];
case 1: // Monday
// send for Tuesday and Wednesday
$startDate = $onMidnight->add(new DateInterval('P1D'));
$endDate = $startDate->add(new DateInterval('P2D'));
break;
case 2: // tuesday
case 3: // wednesday
$startDate = $onMidnight->add(new DateInterval('P2D'));
$endDate = $startDate->add(new DateInterval('P1D'));
break;
case 4: // thursday
$startDate = $onMidnight->add(new DateInterval('P2D'));
$endDate = $startDate->add(new DateInterval('P2D'));
break;
case 5: // friday
$startDate = $onMidnight->add(new DateInterval('P3D'));
$endDate = $startDate->add(new DateInterval('P1D'));
break;
default:
throw new UnexpectedValueException('a day of a week should not have the value: ' . $dow);
}
return ['startDate' => $startDate, 'endDate' => $endDate];
}
}

View File

@@ -1,10 +1,17 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\CalendarBundle\Service\ShortMessageNotification;
use Chill\BudgetBundle\Templating\Twig;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\MainBundle\Service\Mailer\ShortMessage;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Templating\EngineInterface;
@@ -36,8 +43,8 @@ class DefaultShortMessageForCalendarBuilder implements ShortMessageForCalendarBu
}
$toUsers[] = new \Chill\MainBundle\Service\ShortMessage\ShortMessage(
$this->engine->render('@ChillCalendar/CalendarShortMessage/short_message.txt.twig', ['calendar' => $calendar]),
$person->getMobilenumber()
$this->engine->render('@ChillCalendar/CalendarShortMessage/short_message.txt.twig', ['calendar' => $calendar]),
$person->getMobilenumber()
);
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\CalendarBundle\Service\ShortMessageNotification;
use Chill\CalendarBundle\Repository\CalendarRepository;
use Symfony\Component\Messenger\MessageBusInterface;
class Generator
{
private CalendarRepository $calendarRepository;
private MessageBusInterface $messageBus;
private RangeGeneratorInterface $rangeGenerator;
public function generateShortMessages(): void
{
}
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\CalendarBundle\Service\ShortMessageNotification;
use DateTimeImmutable;
interface RangeGeneratorInterface
{
/**
* @return array<startDate: \DateTimeImmutable, endDate: \DateTimeImmutable>
*/
public function generateRange(DateTimeImmutable $date): array;
}

View File

@@ -1,14 +1,22 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\CalendarBundle\Service\ShortMessageNotification;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\MainBundle\Service\Mailer\ShortMessage;
use Chill\MainBundle\Service\ShortMessage\ShortMessage;
interface ShortMessageForCalendarBuilderInterface
{
/**
* @param Calendar $calendar
* @return array|ShortMessage[]
*/
public function buildMessageForCalendar(Calendar $calendar): array;

View File

@@ -0,0 +1,89 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\CalendarBundle\Tests\Service\ShortMessageNotification;
use Chill\CalendarBundle\Service\ShortMessageNotification\DefaultRangeGenerator;
use DateTimeImmutable;
use Iterator;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
final class DefaultRangeGeneratorTest extends TestCase
{
/**
* * Lundi => Envoi des rdv du mardi et mercredi.
* * Mardi => Envoi des rdv du jeudi.
* * Mercredi => Envoi des rdv du vendredi
* * Jeudi => envoi des rdv du samedi et dimanche
* * Vendredi => Envoi des rdv du lundi.
*/
public function generateData(): Iterator
{
yield [
new DateTimeImmutable('2022-06-13 10:45:00'),
new DateTimeImmutable('2022-06-14 00:00:00'),
new DateTimeImmutable('2022-06-16 00:00:00'),
];
yield [
new DateTimeImmutable('2022-06-14 15:45:00'),
new DateTimeImmutable('2022-06-16 00:00:00'),
new DateTimeImmutable('2022-06-17 00:00:00'),
];
yield [
new DateTimeImmutable('2022-06-15 13:45:18'),
new DateTimeImmutable('2022-06-17 00:00:00'),
new DateTimeImmutable('2022-06-18 00:00:00'),
];
yield [
new DateTimeImmutable('2022-06-16 01:30:55'),
new DateTimeImmutable('2022-06-18 00:00:00'),
new DateTimeImmutable('2022-06-20 00:00:00'),
];
yield [
new DateTimeImmutable('2022-06-17 21:30:55'),
new DateTimeImmutable('2022-06-20 00:00:00'),
new DateTimeImmutable('2022-06-21 00:00:00'),
];
yield [
new DateTimeImmutable('2022-06-18 21:30:55'),
null,
null,
];
yield [
new DateTimeImmutable('2022-06-19 21:30:55'),
null,
null,
];
}
/**
* @dataProvider generateData
*/
public function testGenerateRange(DateTimeImmutable $date, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate)
{
$generator = new DefaultRangeGenerator();
['startDate' => $actualStartDate, 'endDate' => $actualEndDate] = $generator->generateRange($date);
$this->assertEquals($startDate->format(DateTimeImmutable::ATOM), $actualStartDate->format(DateTimeImmutable::ATOM));
$this->assertEquals($endDate->format(DateTimeImmutable::ATOM), $actualEndDate->format(DateTimeImmutable::ATOM));
}
}