FacebookGroup   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 111
Duplicated Lines 23.42 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 5
dl 26
loc 111
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A post() 14 51 3
A getGroup() 0 23 2
A getStats() 0 4 1
A getMembersCount() 12 12 1

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
namespace Borfast\Socializr\Connectors;
4
5
use Borfast\Socializr\Exceptions\GenericPostingException;
6
use Borfast\Socializr\Post;
7
use Borfast\Socializr\Group;
8
use Borfast\Socializr\Response;
9
10
// use \Requests;
11
12
class FacebookGroup extends Facebook
13
{
14
    /** @var Group */
15
    protected $group = null;
16
17
    public function post(Post $post)
18
    {
19
        $group = $this->getGroup();
20
21
        if (empty($post->media)) {
22
            $path = '/'.$group->id.'/feed';
23
24
            $msg  = $post->title;
25
            $msg .= "\n\n";
26
            $msg .= $post->body;
27
            $msg = trim($msg);
28
29
            $params = [
30
                // 'caption' => $post->title,
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
31
                'description' => '',
32
                'link' => $post->url,
33
                'message' => $msg
34
            ];
35 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
36
            $path = '/'.$group->id.'/photos';
37
38
            $msg  = $post->title;
39
            $msg .= "\n\n";
40
            $msg .= $post->body;
41
            $msg .= "\n";
42
            $msg .= $post->url;
43
44
            $params = [
45
                'url' => $post->media[0],
46
                'caption' => $msg
47
            ];
48
        }
49
50
        $method = 'POST';
51
52
        $result = $this->request($path, $method, $params);
53
        $json_result = json_decode($result, true);
54
55
        // If there's no ID, the post didn't go through
56
        if (!isset($json_result['id'])) {
57
            $msg = "Unknown error posting to Facebook group.";
58
            throw new GenericPostingException($msg, 1);
59
        }
60
61
        $response = new Response;
62
        $response->setRawResponse($result); // This is already JSON.
63
        $response->setProvider('Facebook');
64
        $response->setPostId($json_result['id']);
65
66
        return $response;
67
    }
68
69
    public function getGroup()
70
    {
71
        if (is_null($this->group)) {
72
            $path = '/' . $this->id . '?fields=id,name,icon';
73
            $result = $this->request($path);
74
            $json_result = json_decode($result, true);
75
76
            $mapping = [
77
                'id' => 'id',
78
                'name' => 'name',
79
                'picture' => 'icon'
80
            ];
81
82
            $this->group = Group::create($mapping, $json_result);
83
            $this->group->picture = $json_result['icon'];
84
            $this->group->link = 'https://www.facebook.com/groups/' . $json_result['id'];
85
            $this->group->can_post = true;
86
            $this->group->provider = static::$provider;
87
            $this->group->raw_response = $result;
88
        }
89
90
        return $this->group;
91
    }
92
93
    /**
94
     * Get the number of memebers this group has.
95
     */
96
    public function getStats()
97
    {
98
        return $this->getMembersCount();
99
    }
100
101
102
    /***************************************************************************
103
     *
104
     * From here on these are FacebookGroup-specific methods that should not be
105
     * accessed from other classes.
106
     *
107
     **************************************************************************/
108
109 View Code Duplication
    protected function getMembersCount()
0 ignored issues
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...
110
    {
111
        $group = $this->getGroup();
112
113
        $path = '/'.$group->id.'/members';
114
        $result = $this->request($path);
115
116
        $response = json_decode($result);
117
        $response = count($response->data);
118
119
        return $response;
120
    }
121
122
}
123