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

FBUser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 12
loc 72
wmc 10
lcom 1
cbo 4
ccs 23
cts 23
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 12 12 2
A getConfig() 0 4 1
A getClient() 0 4 1
A checkConfig() 0 4 3
A getProfile() 0 5 1
A callSendAPI() 0 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}