Adding a task/command in Symfony2

I recently took the Symfony2 plunge and started working on a little fun side project (more on that later).

Anyway, this particular project involves sending out daily text messages using the rather awesome Twilio API so I decided to use a Symfony2 task for this. The documentation on how to actually add your own task is a bit sparse so I figured I’d share.

The process is actually pretty straight forward:

  1. In your bundle create a directory named “Command” (without the quotes).
  2. Create a file that extends ContainerAwareCommand
  3. Create a protected function configure - “protected function configure()” to allow you to configure the name of your task and add any options or arguments you might need.
  4. Create a protected function execute - “protected function execute(InputInterface $input, OutputInterface $output)” to actually do whatever needs to be done.
  5. Thats it! Now you can run app/console and you’ll see your task.

Here is the code for mine:

<?php

namespace Setfive\SextDejourBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class SendTextsCommand extends ContainerAwareCommand
{

    protected function configure()
    {
        parent::configure();

        $this->setName('sj:sendTexts')
             ->setDescription('Sends the day\'s sexts to everyone.');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {

         $ph = $this->getContainer()->get('doctrine')
                    ->getRepository("SextDejourBundle:Phonenumber")
                    ->createQueryBuilder("u")
                    ->getQuery()
                    ->getResult();

         $msg = $this->getContainer()->get('doctrine')
                    ->getRepository("SextDejourBundle:Message")
                    ->createQueryBuilder("u")
                    ->where("u.is_used = false")
                    ->setMaxResults(1)
                    ->getQuery()
                    ->getResult();
         $msg = array_pop( $msg );

         $msg->setIsUsed( true );
         // $this->getContainer()->get('doctrine')->getEntityManager()->flush();

         $accountId = $this->getContainer()->getParameter("twilio_app_id");
         $authToken = $this->getContainer()->getParameter("twilio_token");
         $myNumber = $this->getContainer()->getParameter("twilio_number");

         $client = new \Services_Twilio($accountId, $authToken);

         foreach( $ph as $p ){

           $sms = $p->getGender() == 0 ? $msg->getGuyText() : $msg->getGirlText();

           $res = $client->account->sms_messages->create ( $myNumber, $p->getPhoneNumber(), $sms );

           $output->writeln( $p->getPhoneNumber() . " => " . $sms );

         }

         return 0;
    }
}

Questions or comments about this? Email us at contact@setfive.com.

Read more