Completed
Push — master ( 932d72...8b424c )
by Sergey
03:15 queued 41s
created

SendsMessages::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 4
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Traits;
4
5
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
6
use seregazhuk\PinterestBot\Exceptions\InvalidRequest;
7
8
/**
9
 * Trait SendsMessages
10
 *
11
 * @property $messageEntityName
12
 *
13
 * @package seregazhuk\PinterestBot\Api\Traits
14
 */
15
trait SendsMessages
16
{
17
    use HandlesRequest;
18
19
    /**
20
     * @param array|int $userIds
21
     * @param array|string $emails
22
     *
23
     * @param array $data
24
     * @return bool
25
     */
26
    protected function callSendMessage($userIds, $emails, array $data)
27
    {
28
        $userIds = is_array($userIds) ? $userIds : [$userIds];
29
        $emails = is_array($emails) ? $emails : [$emails];
30
31
        $this->guardAgainstEmptyData($userIds, $emails);
32
33
        $requestOptions = array_merge([
34
                'emails'   => $emails,
35
                'user_ids' => $userIds,
36
            ],
37
            $data);
38
39
        return $this->execPostRequest($requestOptions, UrlBuilder::RESOURCE_SEND_MESSAGE);
40
    }
41
42
    /**
43
     * @param string $text
44
     * @param string $entityId
45
     * @return array
46
     */
47
    protected function buildMessageData($text = null, $entityId = null)
48
    {
49
        $entityName = $this->getMessageEntityName();
50
51
        return [
52
            $entityName => $entityId,
53
            'text'      => $text,
54
        ];
55
    }
56
57
    /**
58
     * Send item with message or by email.
59
     *
60
     * @param string $entityId
61
     * @param string $text
62
     * @param array|string $userIds
63
     * @param array|string $emails
64
     * @return bool
65
     */
66
    public function send($entityId, $text, $userIds, $emails)
67
    {
68
        $messageData = $this->buildMessageData($text, $entityId);
69
70
        return $this->callSendMessage($userIds, $emails, $messageData);
0 ignored issues
show
Bug introduced by
It seems like $userIds defined by parameter $userIds on line 66 can also be of type string; however, seregazhuk\PinterestBot\...ages::callSendMessage() does only seem to accept array|integer, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and 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...
71
    }
72
73
    /**
74
     * Send item with messages.
75
     * @codeCoverageIgnore
76
     * @param int $entityId
77
     * @param string $text
78
     * @param array|string $userIds
79
     * @return bool
80
     */
81
    public function sendWithMessage($entityId, $text, $userIds)
82
    {
83
        return $this->send($entityId, $text, $userIds, []);
84
    }
85
86
    /**
87
     * Send entity with emails.
88
     *
89
     * @codeCoverageIgnore
90
     * @param int $entityId
91
     * @param string $text
92
     * @param array|string $emails
93
     * @return bool
94
     */
95
    public function sendWithEmail($entityId, $text, $emails)
96
    {
97
        return $this->send($entityId, $text, [], $emails);
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    protected function getMessageEntityName()
104
    {
105
        return property_exists($this, 'messageEntityName') ? $this->messageEntityName : '';
0 ignored issues
show
Bug introduced by
The property messageEntityName does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
106
    }
107
108
    /**
109
     * @param $userId
110
     * @param array $emails
111
     * @throws InvalidRequest
112
     */
113
    protected function guardAgainstEmptyData($userId, array $emails)
114
    {
115
        if (empty($userId) && empty($emails)) {
116
            throw new InvalidRequest('You must specify user_ids or emails to send message.');
117
        }
118
    }
119
}