rpicms

A CMS for the Raspberry Pi
git clone git://archive.git.mtrnord.blog/RpicmsTeam/rpicms.git
Log | Files | Refs | README | LICENSE

TokenResponse.php (3519B)


      1 <?php
      2 
      3 /**
      4  *  This program is free software: you can redistribute it and/or modify
      5  *  it under the terms of the GNU Lesser General Public License as published by
      6  *  the Free Software Foundation, either version 3 of the License, or
      7  *  (at your option) any later version.
      8  *
      9  *  This program is distributed in the hope that it will be useful,
     10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  *  GNU Lesser General Public License for more details.
     13  *
     14  *  You should have received a copy of the GNU Lesser General Public License
     15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16  */
     17 
     18 namespace fkooman\OAuth\Client;
     19 
     20 use fkooman\OAuth\Client\Exception\TokenResponseException;
     21 use fkooman\OAuth\Common\Scope;
     22 
     23 class TokenResponse
     24 {
     25     private $accessToken;
     26     private $tokenType;
     27     private $expiresIn;
     28     private $refreshToken;
     29     private $scope;
     30 
     31     public function __construct(array $data)
     32     {
     33         foreach (array('access_token', 'token_type') as $key) {
     34             if (!array_key_exists($key, $data)) {
     35                 throw new TokenResponseException(sprintf("missing field '%s'", $key));
     36             }
     37         }
     38         $this->setAccessToken($data['access_token']);
     39         $this->setTokenType($data['token_type']);
     40 
     41         $this->expiresIn = null;
     42         $this->refreshToken = null;
     43         $this->scope = null;
     44         if (array_key_exists('expires_in', $data)) {
     45             $this->setExpiresIn($data['expires_in']);
     46         }
     47         if (array_key_exists('refresh_token', $data)) {
     48             $this->setRefreshToken($data['refresh_token']);
     49         }
     50         if (array_key_exists('scope', $data)) {
     51             $this->setScope($data['scope']);
     52         }
     53     }
     54 
     55     public function setAccessToken($accessToken)
     56     {
     57         if (!is_string($accessToken) || 0 >= strlen($accessToken)) {
     58             throw new TokenResponseException("access_token needs to be a non-empty string");
     59         }
     60         $this->accessToken = $accessToken;
     61     }
     62 
     63     public function getAccessToken()
     64     {
     65         return $this->accessToken;
     66     }
     67 
     68     public function setTokenType($tokenType)
     69     {
     70         if (!is_string($tokenType) || 0 >= strlen($tokenType)) {
     71             throw new TokenResponseException("token_type needs to be a non-empty string");
     72         }
     73         $this->tokenType = $tokenType;
     74     }
     75 
     76     public function getTokenType()
     77     {
     78         return $this->tokenType;
     79     }
     80 
     81     public function setExpiresIn($expiresIn)
     82     {
     83         if (!is_int($expiresIn) || 0 >= $expiresIn) {
     84             throw new TokenResponseException("expires_in needs to be a positive integer");
     85         }
     86         $this->expiresIn = $expiresIn;
     87     }
     88 
     89     public function getExpiresIn()
     90     {
     91         return $this->expiresIn;
     92     }
     93 
     94     public function setRefreshToken($refreshToken)
     95     {
     96         if (!is_string($refreshToken) || 0 >= strlen($refreshToken)) {
     97             throw new TokenResponseException("refresh_token needs to be a non-empty string");
     98         }
     99         $this->refreshToken = $refreshToken;
    100     }
    101 
    102     public function getRefreshToken()
    103     {
    104         return $this->refreshToken;
    105     }
    106 
    107     public function setScope($scope)
    108     {
    109         $scope = Scope::fromString($scope);
    110         if ($scope->isEmpty()) {
    111             throw new TokenResponseException("scope must be non empty");
    112         }
    113         $this->scope = $scope;
    114     }
    115 
    116     public function getScope()
    117     {
    118         return $this->scope;
    119     }
    120 }