Completed
Push — master ( 8d3dbf...7592af )
by Paweł
62:36
created

getPageAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Facebook Instant Articles Bundle.
7
 *
8
 * Copyright 2016 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2016 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\FacebookInstantArticlesBundle\Manager;
18
19
use Facebook;
20
21
class FacebookInstantArticlesManager implements FacebookInstantArticlesManagerInterface
22
{
23
    /**
24
     * @var FacebookManagerInterface
25
     */
26
    protected $facebookManager;
27
28
    /**
29
     * FacebookInstantArticlesManager constructor.
30
     *
31
     * @param FacebookManagerInterface $facebookManager
32
     */
33 14
    public function __construct(FacebookManagerInterface $facebookManager)
34
    {
35 14
        $this->facebookManager = $facebookManager;
36 14
    }
37
38
    /**
39
     * @return FacebookManagerInterface
40
     */
41
    public function getFacebookManager()
42
    {
43
        return $this->facebookManager;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getPageAccessToken(Facebook\Facebook $facebook, $pageId)
50
    {
51
        return $this->loopThroughPagesAndFindOneById($facebook, $this->getPagesAndTokens($facebook), $pageId);
0 ignored issues
show
Bug introduced by
It seems like $this->getPagesAndTokens($facebook) can be null; however, loopThroughPagesAndFindOneById() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getPagesAndTokens(Facebook\Facebook $facebook)
58
    {
59
        $userAccessToken = $facebook->getRedirectLoginHelper()->getAccessToken();
60
        $helper = new Facebook\InstantArticles\Client\Helper($facebook);
61
62
        return $helper->getPagesAndTokens($userAccessToken);
63
    }
64
65
    /**
66
     * @param Facebook\Facebook             $facebook
67
     * @param Facebook\GraphNodes\GraphEdge $pagesAndAccessTokens
68
     * @param string                        $pageId
69
     *
70
     * @return string|null
71
     */
72
    private function loopThroughPagesAndFindOneById($facebook, $pagesAndAccessTokens, $pageId)
73
    {
74
        foreach ($pagesAndAccessTokens as $page) {
75
            /** @var Facebook\GraphNodes\GraphNode $page */
76
            $page = $page->asArray();
77
78
            if ($page['id'] == $pageId) {
79
                return $page['access_token'];
80
            }
81
        }
82
83
        if (null !== $nextPagesAndAccessTokens = $facebook->next($pagesAndAccessTokens)) {
84
            return $this->loopThroughPagesAndFindOneById($facebook, $nextPagesAndAccessTokens, $pageId);
85
        }
86
87
        return;
88
    }
89
}
90