This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApi.php
73 lines (57 loc) · 2.95 KB
/
Api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
// Copyright 2021 Peter Beverloo. All rights reserved.
// Use of this source code is governed by the MIT license, a copy of which can
// be found in the LICENSE file.
declare(strict_types=1);
namespace Anime;
use \Anime\Storage\RegistrationDatabase;
use \Anime\Storage\RegistrationDatabaseFactory;
// Implementation of the actual API calls as methods whose input has been validated for syntax, and
// for whom the appropriate environment is already available.
class Api {
private Cache $cache;
private Configuration $configuration;
private Environment $environment;
public function __construct(string $hostname) {
$this->cache = Cache::getInstance();
$this->configuration = Configuration::getInstance();
$this->environment = EnvironmentFactory::createForHostname($this->configuration, $hostname);
if (!$this->environment->isValid())
throw new \Exception('The "' . $hostname . '" is not known as a valid environment.');
}
// ---------------------------------------------------------------------------------------------
// Returns the cache that should be used.
public function getCache(): Cache {
return $this->cache;
}
// Returns the configuration instance for the entire volunteer portal.
public function getConfiguration(): Configuration {
return $this->configuration;
}
// Returns the environment that's applicable to the current hostname.
public function getEnvironment(): Environment {
return $this->environment;
}
// ---------------------------------------------------------------------------------------------
// Returns the RegistrationDatabase instance for the current environment. Immutable by default
// unless the |$writable| argument has been set to TRUE.
public function getRegistrationDatabase(bool $writable = false): ?RegistrationDatabase {
return $this->getRegistrationDatabaseForEnvironment($this->environment, $writable);
}
// Returns the RegistrationDatabase instance for a given |$environment|. Immutable by default
// unless the |$writable| argument has been set to TRUE.
public function getRegistrationDatabaseForEnvironment(
Environment $environment, bool $writable = false): ?RegistrationDatabase {
$settings = $environment->getRegistrationDatabaseSettings();
if (!is_array($settings))
return null; // no data has been specified for this environment
if (!array_key_exists('spreadsheet', $settings) || !array_key_exists('sheet', $settings))
return null; // invalid data has been specified for this environment
$spreadsheetId = $settings['spreadsheet'];
$sheet = $settings['sheet'];
if ($writable)
return RegistrationDatabaseFactory::openReadWrite($this->cache, $spreadsheetId, $sheet);
else
return RegistrationDatabaseFactory::openReadOnly($this->cache, $spreadsheetId, $sheet);
}
}