SendTeamInboxMessageCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 57
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 20 1
A execute() 0 27 3
1
<?php
2
3
/*
4
 * This file is part of the Mremi\Flowdock library.
5
 *
6
 * (c) Rémi Marseille <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mremi\Flowdock\Command;
13
14
use Mremi\Flowdock\Api\Push\Push;
15
use Mremi\Flowdock\Api\Push\TeamInboxMessage;
16
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * Sends a mail-like message to the team inbox of a flow
25
 *
26
 * @author Rémi Marseille <[email protected]>
27
 */
28
class SendTeamInboxMessageCommand extends Command
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function configure()
34
    {
35
        $this
36
            ->setName('send-team-inbox-message')
37
            ->setDescription('Sends a mail-like message to the team inbox of a flow')
38
39
            ->addArgument('flow-api-token', InputArgument::REQUIRED, 'The flow API token')
40
            ->addArgument('source',         InputArgument::REQUIRED, 'The human readable identifier of the application that uses the Flowdock Push API')
41
            ->addArgument('from-address',   InputArgument::REQUIRED, 'The email address of the message sender')
42
            ->addArgument('subject',        InputArgument::REQUIRED, 'The subject line of the message')
43
            ->addArgument('content',        InputArgument::REQUIRED, 'The content of the message')
44
45
            ->addOption('from-name', null, InputOption::VALUE_REQUIRED,                               'The name of the message sender')
46
            ->addOption('reply-to',  null, InputOption::VALUE_REQUIRED,                               'The email address for replies')
47
            ->addOption('project',   null, InputOption::VALUE_REQUIRED,                               'The human readable identifier for more detailed message categorization')
48
            ->addOption('format',    null, InputOption::VALUE_REQUIRED,                               'The format of the message content')
49
            ->addOption('link',      null, InputOption::VALUE_REQUIRED,                               'The link associated with the message')
50
            ->addOption('tags',      null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The message tags')
51
            ->addOption('options',   null, InputOption::VALUE_REQUIRED,                               'An array of options used by request');
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59
        $push = new Push($input->getArgument('flow-api-token'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('flow-api-token') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, Mremi\Flowdock\Api\Push\Push::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
60
61
        $message = TeamInboxMessage::create()
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Mremi\Flowdock\Api\Push\BaseMessage as the method setSource() does only exist in the following sub-classes of Mremi\Flowdock\Api\Push\BaseMessage: Mremi\Flowdock\Api\Push\TeamInboxMessage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
62
            ->setSource($input->getArgument('source'))
63
            ->setFromAddress($input->getArgument('from-address'))
64
            ->setSubject($input->getArgument('subject'))
65
            ->setContent($input->getArgument('content'))
66
            ->setFromName($input->getOption('from-name'))
67
            ->setReplyTo($input->getOption('reply-to'))
68
            ->setProject($input->getOption('project'))
69
            ->setFormat($input->getOption('format'))
70
            ->setLink($input->getOption('link'))
71
            ->setTags($input->getOption('tags'));
72
73
        $options = $input->getOption('options') ? json_decode($input->getOption('options'), true) : array();
74
75
        if ($push->sendTeamInboxMessage($message, $options)) {
76
            $output->writeln('<info>Success:</info> the message has been sent');
77
78
            return;
79
        }
80
81
        $output->writeln(sprintf('<error>Failure:</error> %s', $message->getResponseMessage()));
82
        $output->writeln(var_export($message->getResponseErrors(), true));
83
    }
84
}
85