示例#1
0
 /**
  * @param Restaurant $restaurant
  * @return bool
  */
 public function validateRestaurant(Restaurant $restaurant)
 {
     if (null === $restaurant->getCommand()) {
         throw new \InvalidArgumentException('The command can\'t be null.');
     }
     if (!preg_match('/^\\/([a-z]+)$/', $restaurant->getCommand())) {
         throw new \InvalidArgumentException('Invalid command name format: /bagel, only letters are allowed and must start by /');
     }
     if (mb_strlen($restaurant->getName()) > 255) {
         throw new \InvalidArgumentException('The restaurant name must be less than 255 characters.');
     }
     if (null === $restaurant->getExample()) {
         throw new \InvalidArgumentException('The order example can\'t be null.');
     }
     if (null === $restaurant->getPhoneNumber()) {
         throw new \InvalidArgumentException('The restaurant phone number can\'t be null.');
     }
     if (!preg_match('/^[0-9]{10}$/', $restaurant->getPhoneNumber())) {
         throw new \InvalidArgumentException('Invalid restaurant phone number format: Only numbers are allowed.');
     }
     if (!preg_match('/^[0-9]{2}:[0-9]{2}$/', $restaurant->getStartHour())) {
         throw new \InvalidArgumentException('Invalid start hour format.');
     }
     if (!preg_match('/^[0-9]{2}:[0-9]{2}$/', $restaurant->getEndHour())) {
         throw new \InvalidArgumentException('Invalid end hour format.');
     }
     if (null === $restaurant->getToken()) {
         throw new \InvalidArgumentException('The token can\'t be null.');
     }
     if ($restaurant->sendOrderByEmail() && null === $restaurant->getEndHour()) {
         throw new \InvalidArgumentException('You must set the restaurant email if you want to send order by email');
     }
     if ($restaurant->sendOrderByEmail() && null === $restaurant->getSenderEmail()) {
         throw new \InvalidArgumentException('You must set the sender email if you want to send order by email');
     }
     return true;
 }
 /**
  * @param string $name
  * @param array $params
  * @return array
  */
 public function send($name, $params)
 {
     /** @var OrderRepository $orderRepository */
     $orderRepository = $this->em->getRepository('SlackOrder\\Entity\\Order');
     try {
         $orderEntity = $orderRepository->getFirstOrderNotSentToday($this->restaurant);
     } catch (NoResultException $e) {
         return ['text' => $this->translator->trans('order.send.noOrderPlacedToday'), 'mrkdwn' => true];
     }
     if ($this->inTime()) {
         return ['text' => $this->translator->trans('order.send.toEarly', ['%orderEndHour%' => $this->restaurant->getEndHour()]), 'mrkdwn' => true];
     }
     if ($orderEntity->getName() !== $name) {
         return ['text' => $this->translator->trans('order.send.notAuthorizedToSendOrders', ['%name%' => $orderEntity->getName()]), 'mrkdwn' => true];
     }
     $hour = $params[1];
     if (!preg_match('/^[0-9]{2}:[0-9]{2}$/', $hour)) {
         return ['text' => $this->translator->trans('order.send.invalidHourFormat'), 'mrkdwn' => true];
     }
     unset($params[1]);
     $phoneNumber = implode('', $params);
     if (!preg_match('/^[0-9]{10}$/', $phoneNumber)) {
         return ['text' => $this->translator->trans('order.send.invalidPhoneNumberFormat'), 'mrkdwn' => true];
     }
     /** @var OrderRepository $orderRepository */
     $orderRepository = $this->em->getRepository('SlackOrder\\Entity\\Order');
     $date = new \DateTime();
     $date->setTime(0, 0, 0);
     $orders = $orderRepository->findBy(['date' => $date, 'sent' => false, 'restaurant' => $this->restaurant]);
     if ($this->restaurant->sendOrderByEmail() === false) {
         $orderRepository->setOrderAsSent($this->restaurant);
         return ['text' => $this->translator->trans('order.send.orderByEmailNotActivated', ['%restaurantPhoneNumber%' => $this->restaurant->getPhoneNumber()])];
     }
     if ($this->sendEmail($hour, $phoneNumber, $name, $orders) === 0) {
         return ['text' => $this->translator->trans('order.send.fail', ['%restaurantPhoneNumber%' => $this->restaurant->getPhoneNumber()])];
     }
     $orderRepository->setOrderAsSent($this->restaurant);
     return ['text' => $this->translator->trans('order.send.success'), 'mrkdwn' => true, 'attachments' => [['fallback' => 'Fail ?', 'text' => $this->translator->trans('order.send.successConfirm', ['%name%' => ucfirst($orderEntity->getName()), '%restaurantPhoneNumber%' => $this->restaurant->getPhoneNumber()]), 'color' => 'danger']]];
 }