Completed
Pull Request — master (#17)
by Oliver
09:46
created

ApnsPushCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 21
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 21
loc 21
rs 9.3142
cc 2
eloc 12
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the MobileNotifBundle 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\MobileNotifBundle\Command;
11
12
use LinkValue\MobileNotif\Model\ApnsMessage;
13
use LinkValue\MobileNotifBundle\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 MobileNotifBundle
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_mobile_notif: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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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_mobile_notif.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