-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathEnvironmentStore.php
More file actions
80 lines (64 loc) · 2.25 KB
/
Copy pathEnvironmentStore.php
File metadata and controls
80 lines (64 loc) · 2.25 KB
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
74
75
76
77
78
79
80
<?php
declare(strict_types=1);
namespace PHPCensor\Store;
use Exception;
use PDO;
use PHPCensor\Exception\HttpException;
use PHPCensor\Model\Build;
use PHPCensor\Model\Environment;
use PHPCensor\Store;
/**
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class EnvironmentStore extends Store
{
protected string $tableName = 'environments';
protected string $modelName = Environment::class;
/**
* Get a single Environment by Name.
*
* @throws HttpException
*/
public function getByNameAndProjectId(string $name, int $projectId, string $useConnection = 'read'): ?Environment
{
if (\is_null($name)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{' . $this->tableName . '}} WHERE {{name}} = :name AND {{project_id}} = :project_id LIMIT 1';
$stmt = $this->databaseManager->getConnection($useConnection)->prepare($query);
$stmt->bindValue(':name', $name);
$stmt->bindValue(':project_id', $projectId);
if ($stmt->execute()) {
if ($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
return new Environment($this->storeRegistry, $data);
}
}
return null;
}
/**
* Get multiple Environment by Project id.
*
* @throws Exception
*/
public function getByProjectId(int $projectId, string $useConnection = 'read'): array
{
if (\is_null($projectId)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{' . $this->tableName . '}} WHERE {{project_id}} = :project_id';
$stmt = $this->databaseManager->getConnection($useConnection)->prepare($query);
$stmt->bindValue(':project_id', $projectId);
if ($stmt->execute()) {
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$map = fn ($item) => new Environment($this->storeRegistry, $item);
$rtn = \array_map($map, $res);
$count = \count($rtn);
return ['items' => $rtn, 'count' => $count];
} else {
return ['items' => [], 'count' => 0];
}
}
}