Completed
Push — master ( 028b55...c232a2 )
by Jean
08:31 queued 03:12
created

Notifier::createNotifications()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 19
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 2
crap 20
1
<?php
2
/**
3
 * @author Jean Silva <[email protected]>
4
 * @license MIT
5
 */
6
namespace Jeancsil\FlightSpy\Notifier\Slack;
7
8
use Jeancsil\FlightSpy\Api\DataTransfer\SessionParameters;
9
use Jeancsil\FlightSpy\Service\ElasticSearch\ElasticSearchWriterTrait;
10
use Jeancsil\FlightSpy\Service\ElasticSearch\ElasticSearchRequester;
11
use Jeancsil\FlightSpy\Notifier\Deal;
12
use Jeancsil\FlightSpy\Notifier\NotifiableInterface;
13
use Maknz\Slack\Client;
14
15
class Notifier implements NotifiableInterface
16
{
17
    use ElasticSearchWriterTrait;
18
19
    /**
20
     * @var ElasticSearchRequester
21
     */
22
    private $elasticSearchRequester;
23
    /**
24
     * @var Client
25
     */
26
    private $slackClient;
27
    /**
28
     * @var string
29
     */
30
    private $slackUserName;
31
32
    public function __construct(Client $slackClient, $slackUserName)
33
    {
34
        $this->slackClient = $slackClient;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
35
        $this->slackUserName = $slackUserName;
36
    }
37
38
    /**
39
     * @param Deal[] $deals
40
     * @param SessionParameters $sessionParameters
41
     */
42
    public function notify(array $deals, SessionParameters $sessionParameters)
43
    {
44
        $notifications = $this->createNotifications($deals, $sessionParameters);
0 ignored issues
show
Documentation introduced by
$deals is of type array<integer,object<Jea...ightSpy\Notifier\Deal>>, but the function expects a object<Jeancsil\FlightSp...sfer\SessionParameters>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$sessionParameters is of type object<Jeancsil\FlightSp...sfer\SessionParameters>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
46
        /**
47
         * @var string $identifier
48
         * @var SlackNotification $notification
49
         */
50
        foreach ($notifications as $identifier => $notification) {
51
            $this->slackClient
52
                ->createMessage()
53
                ->send($notification->message);
0 ignored issues
show
Bug introduced by
Accessing message on the interface Jeancsil\FlightSpy\Notifier\Notification suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
54
55
            $this->elasticSearchWriter
56
                ->writeOne([
57
                    'identifier' => $identifier,
58
                    'notified' => $notification->to
0 ignored issues
show
Bug introduced by
Accessing to on the interface Jeancsil\FlightSpy\Notifier\Notification suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
59
                ]);
60
        }
61
    }
62
63
    /** @inheritdoc */
64
    public function wasNotified(Deal $deal, $notifyTo)
65
    {
66
        return $this->elasticSearchRequester
67
            ->wasNotified(
68
                $deal->getIdentifier(),
69
                $notifyTo
70
            );
71
    }
72
73
    /** @inheritdoc */
74
    public function createNotifications(SessionParameters $parameters, array $deals = [])
75
    {
76
        $notifications = [];
77
        /** @var Deal $deal */
78
        foreach ($deals as $deal) {
79
            $to = "@{$this->slackUserName}";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
80
81
            if ($this->wasNotified($deal, $to)) {
82
                continue;
83
            }
84
85
            $notification = new SlackNotification();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
86
            $notification->to = $to;
87
            $message = $deal->getAgentName() . $parameters->currency . number_format($deal->getPrice());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
88
89
            if ($deal->getDeepLinkUrl()) {
90
                $message .=  PHP_EOL . "<{$deal->getDeepLinkUrl()}|Deep Link>";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $deal instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
91
            }
92
93
            $notification->message = $message;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 17 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
94
            $notifications[$deal->getIdentifier()] = $notification;
95
        }
96
97
        return $notifications;
98
    }
99
100
    /**
101
     * @param ElasticSearchRequester $elasticSearchRequester
102
     */
103
    public function setElasticSearchRequester(ElasticSearchRequester $elasticSearchRequester)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $elasticSearchRequester exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
104
    {
105
        $this->elasticSearchRequester = $elasticSearchRequester;
106
    }
107
}
108