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

FBUser::getProfile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: mfrancois
5
 * Date: 02/08/2016
6
 * Time: 10:39
7
 */
8
9
namespace Distilleries\Messenger\Helpers;
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 FBUser
18
{
19
20
    protected $config = [];
21
    protected $client = null;
22
23
    /**
24
     * Message constructor.
25
     * @param array $config
26
     */
27 28 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
30 28
        $this->client = $client;
31
32 28
        if ($this->checkConfig($config)) {
33 24
            $this->config = $config;
34 18
        } else {
35 4
            throw new ConfigException(trans('messenger::errors.config_not_valid'));
36
        }
37
38 18
    }
39
40
    /**
41
     * @return array
42
     */
43 4
    public function getConfig()
44
    {
45 4
        return $this->config;
46
    }
47
48
    /**
49
     * @return Client|null
50
     */
51 4
    public function getClient()
52
    {
53 4
        return $this->client;
54
    }
55
56
57 28
    protected function checkConfig(array $config)
58
    {
59 28
        return (empty($config['page_access_token']) || empty($config['uri_open_graph'])) ? false : true;
60
    }
61
62
63 16
    public function getProfile($uid, $fields = ['fields' => 'first_name,last_name,profile_pic,locale,timezone,gender'])
64
    {
65
66 16
        return $this->callSendAPI($uid, $fields);
67
    }
68
69
70 16
    public function callSendAPI($uid, $data)
71
    {
72
73 16
        $data = $data + ['access_token' => $this->config['page_access_token']];
74
75
        try {
76 16
            $res = $this->client->request('GET', $this->config['uri_open_graph'] . $uid, [
77 4
                'query' => $data
78 12
            ]);
79
80 12
            return \GuzzleHttp\json_decode($res->getBody()->getContents());
81
82 4
        } catch (ClientException $e) {
83 4
            throw new MessengerException(trans('messenger::errors.unable_to_load_user_profile'), 0, $e);
84
        }
85
86
    }
87
88
}