|
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\ApnsMessage; |
|
13
|
|
|
use LinkValue\JarvisBundle\Client\ApnsClient; |
|
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
|
|
|
* ApnsPushCommand. |
|
22
|
|
|
* |
|
23
|
|
|
* @package JarvisBundle |
|
24
|
|
|
* @author Oliver Thebault <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class ApnsPushCommand extends ContainerAwareCommand |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Command configuration. |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function configure() |
|
32
|
|
|
{ |
|
33
|
|
|
$this |
|
34
|
|
|
->setName('link_value_jarvis:apns:push') |
|
35
|
|
|
->setDescription('APNS 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('badge', 'b', InputOption::VALUE_REQUIRED, 'Notification badge', 0) |
|
39
|
|
|
; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Command execution. |
|
44
|
|
|
*/ |
|
45
|
|
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$message = (new ApnsMessage()) |
|
48
|
|
|
->setSimpleAlert($input->getArgument('message')) |
|
49
|
|
|
->setBadge($input->getOption('badge')) |
|
50
|
|
|
->addToken($token = $input->getArgument('token')) |
|
51
|
|
|
; |
|
52
|
|
|
|
|
53
|
|
|
$apnsClients = $this->getContainer()->get('link_value_jarvis.clients')->getApnsClients(); |
|
54
|
|
|
|
|
55
|
|
|
if ($apnsClients->count() == 0) { |
|
56
|
|
|
throw new \RuntimeException('You must configure at least one APNS client to be able to push messages with this command.'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$apnsClients->forAll(function ($key, ApnsClient $client) use ($message, $output) { |
|
60
|
|
|
$output->writeln(sprintf('Sending message "%s" using APNS [%s] client...', $message->getSimpleAlert(), $key)); |
|
61
|
|
|
$client->push($message); |
|
62
|
|
|
|
|
63
|
|
|
return true; |
|
64
|
|
|
}); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
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.