Completed
Push — master ( c73ac3...acbb41 )
by Serhii
34:37 queued 19:37
created

FacebookUserProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 57
rs 10
wmc 4
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getUser() 0 29 3
1
<?php
2
3
namespace AppBundle\Services;
4
5
use AppBundle\Model\FacebookResponse;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\TransferException;
8
use JMS\Serializer\Serializer;
9
use Symfony\Component\HttpKernel\Exception\HttpException;
10
use Symfony\Component\Validator\Validator\RecursiveValidator;
11
12
class FacebookUserProvider
13
{
14
    /**
15
     * @var Serializer
16
     */
17
    protected $serializer;
18
19
    /**
20
     * @var RecursiveValidator
21
     */
22
    protected $validator;
23
24
    /**
25
     * @param Serializer         $serializer
26
     * @param RecursiveValidator $validator
27
     */
28
    public function __construct(Serializer $serializer, RecursiveValidator $validator)
29
    {
30
        $this->serializer = $serializer;
31
        $this->validator = $validator;
32
    }
33
34
    /**
35
     * @param string $accessToken
36
     *
37
     * @return FacebookResponse
38
     */
39
    public function getUser($accessToken)
40
    {
41
        try {
42
            $client = new Client();
43
            $result = $client->get(
44
                'https://graph.facebook.com/v2.8/me',
45
                ['query' => ['access_token' => $accessToken,
46
                 'fields' => 'id, 
47
                 email, first_name, 
48
                 last_name',
49
                ],
50
                ]
51
            );
52
            $userFacebook = $this->serializer->deserialize(
53
                $result->getBody(),
0 ignored issues
show
Documentation introduced by
$result->getBody() is of type object<GuzzleHttp\Stream\StreamInterface>|null, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
                FacebookResponse::class,
55
                'json'
56
            );
57
            $errors = $this->validator->validate($userFacebook);
58
59
            if (count($errors) > 0) {
60
                throw new HttpException(400, 'Social response validation error');
61
            }
62
63
            return $userFacebook;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $userFacebook; (object|array|integer|double|string|boolean) is incompatible with the return type documented by AppBundle\Services\FacebookUserProvider::getUser of type AppBundle\Model\FacebookResponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
64
        } catch (TransferException $e) {
65
            throw new HttpException(400, 'Social login error');
66
        }
67
    }
68
}
69