1<?php
2/**
3 * Copyright 2017 Facebook, Inc.
4 *
5 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6 * use, copy, modify, and distribute this software in source code or binary
7 * form for use in connection with the web services and APIs provided by
8 * Facebook.
9 *
10 * As with any software that integrates with the Facebook platform, your use
11 * of this software is subject to the Facebook Developer Principles and
12 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13 * shall be included in all copies or substantial portions of the software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 */
24namespace Facebook\Helpers;
25
26use Facebook\FacebookApp;
27use Facebook\FacebookClient;
28
29/**
30 * Class FacebookPageTabHelper
31 *
32 * @package Facebook
33 */
34class FacebookPageTabHelper extends FacebookCanvasHelper
35{
36    /**
37     * @var array|null
38     */
39    protected $pageData;
40
41    /**
42     * Initialize the helper and process available signed request data.
43     *
44     * @param FacebookApp    $app          The FacebookApp entity.
45     * @param FacebookClient $client       The client to make HTTP requests.
46     * @param string|null    $graphVersion The version of Graph to use.
47     */
48    public function __construct(FacebookApp $app, FacebookClient $client, $graphVersion = null)
49    {
50        parent::__construct($app, $client, $graphVersion);
51
52        if (!$this->signedRequest) {
53            return;
54        }
55
56        $this->pageData = $this->signedRequest->get('page');
57    }
58
59    /**
60     * Returns a value from the page data.
61     *
62     * @param string     $key
63     * @param mixed|null $default
64     *
65     * @return mixed|null
66     */
67    public function getPageData($key, $default = null)
68    {
69        if (isset($this->pageData[$key])) {
70            return $this->pageData[$key];
71        }
72
73        return $default;
74    }
75
76    /**
77     * Returns true if the user is an admin.
78     *
79     * @return boolean
80     */
81    public function isAdmin()
82    {
83        return $this->getPageData('admin') === true;
84    }
85
86    /**
87     * Returns the page id if available.
88     *
89     * @return string|null
90     */
91    public function getPageId()
92    {
93        return $this->getPageData('id');
94    }
95}
96