ApnsPushCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
crap 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