Completed
Pull Request — master (#137)
by
unknown
12:08
created

FacebookUserProvider::getUser()   B

Complexity

Conditions 3
Paths 7

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 19
nc 7
nop 1
dl 0
loc 31
rs 8.8571
c 1
b 0
f 0
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\Exception\ValidatorException;
11
use Symfony\Component\Validator\Validator\RecursiveValidator;
12
13
class FacebookUserProvider
14
{
15
    /**
16
     * @var RecursiveValidator
17
     */
18
    protected $validator;
19
20
    protected $serializer;
21
22
    public function __construct(RecursiveValidator $validator, Serializer $serializer)
23
    {
24
        $this->validator = $validator;
25
        $this->serializer = $serializer;
26
    }
27
    /**
28
     * @param string $accessToken
29
     *
30
     * @return 
31
     */
32
    public function getUser($accessToken)
33
    {
34
        try {
35
            $client = new Client();
36
            $result = $client->get(
37
                'https://graph.facebook.com/v2.8/me',
38
                ['query' => [
39
                    'access_token' => $accessToken,
40
                    'fields' => 'id, email, first_name, last_name', ],
41
                ]
42
            );
43
            // todo: Add validation facebook response
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% 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...
44
//            $userFacebook = json_decode($result->getBody(), true);
45
            $userFacebook = $this->serializer->deserialize(
46
                $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...
47
                FacebookResponse::class,
48
                'json'
49
            );
50
            $errors = $this->validator->validate($userFacebook);
51
52
            if (count($errors) > 0) {
53
                $errorsString = (string) $errors;
54
55
                throw new ValidatorException(sprintf('There are errors "%s"', $errorsString));
56
            }
57
58
            return $userFacebook;
59
        } catch (TransferException $e) {
60
            throw new HttpException(400, 'Social login error');
61
        }
62
    }
63
}
64