Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PubSubClientFactory
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 create
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 getPubSubFilePath
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
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 General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2022 (original work) Open Assessment Technologies SA.
19 */
20
21declare(strict_types=1);
22
23namespace oat\tao\model\Observer\GCP;
24
25use ErrorException;
26use Google\Cloud\PubSub\PubSubClient;
27
28class PubSubClientFactory
29{
30    /** @var array */
31    private $environmentVars;
32
33    public function __construct(array $environmentVars = null)
34    {
35        $this->environmentVars = $environmentVars ?? $_ENV;
36    }
37
38    /**
39     * @return \Google\Cloud\PubSub\PubSubClient
40     *
41     * @throws ErrorException
42     */
43    public function create(array $config = [])
44    {
45        if (!class_exists(PubSubClient::class) || !$this->getPubSubFilePath()) {
46            throw new ErrorException('PubSubClient requirements missing');
47        }
48
49        return new PubSubClient(
50            [
51                'keyFilePath' => $this->getPubSubFilePath(),
52            ]
53        );
54    }
55
56    private function getPubSubFilePath(): ?string
57    {
58        return $this->environmentVars['GOOGLE_APPLICATION_CREDENTIALS'] ?? null;
59    }
60}