FBUser::checkConfig()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 3
eloc 2
nc 4
nop 1
crap 3
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)
28
    {
29
30 28
        $this->client = $client;
31
32 28
        if ($this->checkConfig($config) && !empty($client)) {
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
}