Completed
Push — master ( dc7b66...2f29ec )
by Maxime
03:23
created

Message::getClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Distilleries\Messenger\Helpers;
4
5
/**
6
 * Created by PhpStorm.
7
 * User: mfrancois
8
 * Date: 31/07/2016
9
 * Time: 19:50
10
 */
11
12
use Distilleries\Messenger\Exceptions\ConfigException;
13
use Distilleries\Messenger\Exceptions\MessengerException;
14
use GuzzleHttp\Client;
15
use GuzzleHttp\Exception\ClientException;
16
17
class Message
18
{
19
20
    protected $config = [];
21
    protected $client = null;
22
23
    /**
24
     * Message constructor.
25
     * @param array $config
26
     */
27 44 View Code Duplication
    public function __construct(array $config, Client $client)
1 ignored issue
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...
28
    {
29 44
        $this->client = $client;
30
31 44
        if ($this->checkConfig($config)) {
32 40
            $this->config = $config;
33 30
        } else {
34 4
            throw new ConfigException(trans('messenger::errors.config_not_valid'));
35
        }
36
37 30
    }
38
39 44
    protected function checkConfig(array $config)
40
    {
41 44
        return (empty($config['uri_bot']) || empty($config['page_access_token'])) ? false : true;
42
    }
43
44
    /**
45
     * @return array
46
     */
47 4
    public function getConfig()
48
    {
49 4
        return $this->config;
50
    }
51
52
    /**
53
     * @return Client|null
54
     */
55 12
    public function getClient()
56
    {
57 12
        return $this->client;
58
    }
59
60
61 8
    public function sendTextMessage($recipientId, $messageText)
62
    {
63
        $messageData = [
64 8
            'recipient' => ['id' => $recipientId],
65 8
            'message'   => ['text' => $messageText]
66 6
        ];
67
68 8
        return $this->callSendAPI($messageData);
69
    }
70
71
72 4 View Code Duplication
    public function sendImageMessage($recipientId, $picture)
1 ignored issue
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...
73
    {
74
75
        $messageData = [
76 4
            'recipient' => ['id' => $recipientId],
77
            'message'   => [
78
                'attachment' => [
79 4
                    'type'    => 'image',
80
                    'payload' =>
81
                        [
82 1
                            'url' => $picture
83 3
                        ]
84 3
                ]
85 3
            ]
86 3
        ];
87
88 4
        return $this->callSendAPI($messageData);
89
    }
90
91 4 View Code Duplication
    public function sendCard($recipientId, $card)
1 ignored issue
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...
92
    {
93
94
        $messageData = [
95 4
            'recipient' => ['id' => $recipientId],
96
            'message'   => [
97
                'attachment' => [
98 4
                    'type'    => 'template',
99 1
                    'payload' => $card
100 3
                ]
101 3
            ]
102 3
        ];
103
104 4
        return $this->callSendAPI($messageData);
105
    }
106
107
108 20
    public function callSendAPI($messageData)
109
    {
110
        try {
111 20
            $res = $this->client->request('POST', $this->config['uri_bot'], [
112 20
                'query' => ['access_token' => $this->config['page_access_token']],
113 5
                'json'  => $messageData
114 15
            ]);
115
116 16
            return $res->getBody()->getContents();
117
118 4
        } catch (ClientException $e) {
119
120 4
            throw new MessengerException(trans('messenger::errors.unable_send_message'), 0, $e);
121
        }
122
    }
123
124
125 4
    public function persistMenu($menu)
126
    {
127
128
        $messageData = [
129 4
            "setting_type"    => "call_to_actions",
130 4
            "thread_state"    => "existing_thread",
131 1
            "call_to_actions" => $menu
132 3
        ];
133
134 4
        return $this->callSendAPI($messageData);
135
    }
136
137 8
    public function getCurrentUserProfile($uid, $fields = null)
138
    {
139 8
        if (empty($fields)) {
140 4
            return (new FBUser($this->config, $this->getClient()))->getProfile($uid);
0 ignored issues
show
Bug introduced by
It seems like $this->getClient() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
141
        }
142
143 4
        return (new FBUser($this->config, $this->getClient()))->getProfile($uid, $fields);
0 ignored issues
show
Bug introduced by
It seems like $this->getClient() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
144
145
    }
146
}