vendor/kreait/firebase-php/src/Firebase/ServiceAccount.php line 62

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Kreait\Firebase;
  4. use Kreait\Firebase\Exception\InvalidArgumentException;
  5. use Kreait\Firebase\Util\JSON;
  6. use Throwable;
  7. /**
  8.  * @internal
  9.  */
  10. class ServiceAccount
  11. {
  12.     /**
  13.      * @var array{
  14.      *     project_id: string|null,
  15.      *     client_email: string|null,
  16.      *     private_key: string|null,
  17.      *     type?: string|null
  18.      * }|array<string, string|null> $data
  19.      */
  20.     private array $data = [];
  21.     public function getProjectId(): string
  22.     {
  23.         return $this->data['project_id'] ?? '';
  24.     }
  25.     public function getClientEmail(): string
  26.     {
  27.         return $this->data['client_email'] ?? '';
  28.     }
  29.     public function getPrivateKey(): string
  30.     {
  31.         return $this->data['private_key'] ?? '';
  32.     }
  33.     /**
  34.      * @return array{
  35.      *     project_id: string|null,
  36.      *     client_email: string|null,
  37.      *     private_key: string|null,
  38.      *     type: string|null
  39.      * }|array<string, string|null>
  40.      */
  41.     public function asArray(): array
  42.     {
  43.         $array $this->data;
  44.         $array['type'] ??= 'service_account';
  45.         return $array;
  46.     }
  47.     /**
  48.      * @param self|string|array|mixed $value
  49.      *
  50.      * @throws InvalidArgumentException
  51.      */
  52.     public static function fromValue($value): self
  53.     {
  54.         if ($value instanceof self) {
  55.             return $value;
  56.         }
  57.         if (\is_string($value)) {
  58.             try {
  59.                 if (\str_starts_with($value'{')) {
  60.                     return self::fromJson($value);
  61.                 }
  62.                 return self::fromJsonFile($value);
  63.             } catch (Throwable $e) {
  64.                 throw new InvalidArgumentException('Invalid service account: '.$e->getMessage(), $e->getCode(), $e);
  65.             }
  66.         }
  67.         if (\is_array($value)) {
  68.             try {
  69.                 return self::fromArray($value);
  70.             } catch (Throwable $e) {
  71.                 throw new InvalidArgumentException('Invalid service account: '.$e->getMessage(), $e->getCode(), $e);
  72.             }
  73.         }
  74.         throw new InvalidArgumentException('Invalid service account: Unsupported value');
  75.     }
  76.     /**
  77.      * @param array<string, string> $data
  78.      */
  79.     private static function fromArray(array $data): self
  80.     {
  81.         $type $data['type'] ?? '';
  82.         if ($type !== 'service_account') {
  83.             throw new InvalidArgumentException(
  84.                 'A Service Account specification must have a field "type" with "service_account" as its value.'
  85.                 .' Please make sure you download the Service Account JSON file from the Service Accounts tab'
  86.                 .' in the Firebase Console, as shown in the documentation on'
  87.                 .' https://firebase.google.com/docs/admin/setup#add_firebase_to_your_app'
  88.             );
  89.         }
  90.         $serviceAccount = new self();
  91.         $serviceAccount->data $data;
  92.         return $serviceAccount;
  93.     }
  94.     private static function fromJson(string $json): self
  95.     {
  96.         $config JSON::decode($jsontrue);
  97.         return self::fromArray($config);
  98.     }
  99.     private static function fromJsonFile(string $filePath): self
  100.     {
  101.         try {
  102.             $file = new \SplFileObject($filePath);
  103.             $json = (string) $file->fread($file->getSize());
  104.         } catch (Throwable $e) {
  105.             throw new InvalidArgumentException("{$filePath} can not be read: {$e->getMessage()}");
  106.         }
  107.         try {
  108.             $serviceAccount self::fromJson($json);
  109.         } catch (Throwable $e) {
  110.             throw new InvalidArgumentException(\sprintf('%s could not be parsed to a Service Account: %s'$filePath$e->getMessage()));
  111.         }
  112.         return $serviceAccount;
  113.     }
  114. }