Beispiel #1
0
 private function registerSubscribe()
 {
     $command = $this->register('subscribe');
     // set description
     $command->setDescription('Adds/updates email related to query');
     // set arguments
     $command->setDefinition(array(new InputArgument('email', InputArgument::REQUIRED, 'Email address'), new InputArgument('id', InputArgument::REQUIRED, 'Query identifier'), new InputOption('message', null, InputOption::VALUE_REQUIRED, 'Email message (first line)')));
     // set action
     $command->setCode(function (InputInterface $input, OutputInterface $output) {
         // get and store ids
         $ids = QueryStorage::getInstance()->getIds();
         // get argument id
         $id = $input->getArgument('id');
         // get argument email
         $email = $input->getArgument('email');
         // email storage
         $emailStorage = EmailStorage::getInstance();
         // validate email
         if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
             $output->writeln("{$email} is not valid");
             return;
         }
         // check if query exists
         if (in_array($id, $ids)) {
             $key = $id . ',' . $email;
             // get email keys
             $emailIds = $emailStorage->getIds();
             // set data
             $emailStorage->set($data = EmailStorage::createEmail($email, $input->getOption('message')), $key);
             // set relations
             $emailStorage->hasOne($key, QueryStorage::getInstance(), $id);
             // save
             $emailStorage->save();
             // output
             $output->writeln(sprintf('%s email: <info>%s</info> to database (Query: <info>%s</info>)', in_array($key, $emailIds) ? 'Updated' : 'Added', EmailStorage::formatEmail($data), $id));
         } else {
             $output->writeln("Query with {$id} not found");
         }
     });
 }