|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the JarvisBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace LinkValue\JarvisBundle\Command; |
|
11
|
|
|
|
|
12
|
|
|
use LinkValue\MobileNotif\Model\GcmMessage; |
|
13
|
|
|
use LinkValue\JarvisBundle\Client\GcmClient; |
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* GcmPushCommand. |
|
22
|
|
|
* |
|
23
|
|
|
* @package JarvisBundle |
|
24
|
|
|
* @author Oliver Thebault <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class GcmPushCommand extends ContainerAwareCommand |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Command configuration. |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function configure() |
|
32
|
|
|
{ |
|
33
|
|
|
$this |
|
34
|
|
|
->setName('link_value_mobile_notif:gcm:push') |
|
35
|
|
|
->setDescription('GCM Push notification command.') |
|
36
|
|
|
->addArgument('token', InputArgument::REQUIRED, 'Token of the device which will receive the notification.') |
|
37
|
|
|
->addArgument('message', InputArgument::REQUIRED, 'Notification message.') |
|
38
|
|
|
->addOption('title', 't', InputOption::VALUE_REQUIRED, 'Notification title.', 'Title') |
|
39
|
|
|
->addOption('icon', 'i', InputOption::VALUE_REQUIRED, 'Notification icon', 'myicon') |
|
40
|
|
|
; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Command execution. |
|
45
|
|
|
*/ |
|
46
|
|
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$message = (new GcmMessage()) |
|
49
|
|
|
->setNotificationBody($input->getArgument('message')) |
|
50
|
|
|
->setNotificationTitle($input->getOption('title')) |
|
51
|
|
|
->setNotificationIcon($input->getOption('myicon')) |
|
52
|
|
|
->addToken($token = $input->getArgument('token')) |
|
53
|
|
|
; |
|
54
|
|
|
|
|
55
|
|
|
$gcmClients = $this->getContainer()->get('link_value_mobile_notif.clients')->getGcmClients(); |
|
56
|
|
|
|
|
57
|
|
|
if ($gcmClients->count() == 0) { |
|
58
|
|
|
throw new \RuntimeException('You must configure at least one GCM client to be able to push messages with this command.'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$gcmClients->forAll(function ($key, GcmClient $client) use ($message, $output) { |
|
62
|
|
|
$output->writeln(sprintf('Sending message "%s" using GCM [%s] client...', $message->getNotificationBody(), $key)); |
|
63
|
|
|
$client->push($message); |
|
64
|
|
|
|
|
65
|
|
|
return true; |
|
66
|
|
|
}); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.