• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ICanBoogie / ActiveRecord / 8587980914

07 Apr 2024 10:12AM UTC coverage: 85.614% (+1.7%) from 83.927%
8587980914

push

github

olvlvl
Tidy Query

13 of 22 new or added lines in 3 files covered. (59.09%)

116 existing lines in 8 files now uncovered.

1345 of 1571 relevant lines covered (85.61%)

24.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.75
/lib/ActiveRecord/ConnectionCollection.php
1
<?php
2

3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace ICanBoogie\ActiveRecord;
13

14
use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;
15
use PDOException;
16

17
use function ICanBoogie\iterable_to_dictionary;
18

19
/**
20
 * A collection of connections.
21
 */
22
class ConnectionCollection implements ConnectionProvider, ConnectionIterator
23
{
24
    /**
25
     * @var array<string, ConnectionDefinition>
26
     *     Where _key_ is a connection identifier.
27
     */
28
    public readonly array $definitions;
29

30
    /**
31
     * Established connections.
32
     *
33
     * @var array<string, Connection>
34
     *     Where _key_ is a connection identifier.
35
     */
36
    private array $established = [];
37

38
    /**
39
     * @param ConnectionDefinition[] $definitions
40
     */
41
    public function __construct(iterable $definitions)
42
    {
43
        // @phpstan-ignore-next-line
44
        $this->definitions = iterable_to_dictionary($definitions, fn(ConnectionDefinition $d) => $d->id);
66✔
45
    }
46

47
    public function connection_for_id(string $id): Connection
48
    {
49
        return $this->established[$id] ??= $this->new_connection($id);
63✔
50
    }
51

52
    private function new_connection(string $id): Connection
53
    {
54
        $definition = $this->definitions[$id]
63✔
UNCOV
55
            ?? throw new ConnectionNotDefined($id);
×
56

57
        #
58
        # we catch connection exceptions and rethrow them in order to avoid displaying sensible
59
        # information such as the username or password.
60
        #
61

62
        try {
63
            return new Connection($definition);
63✔
64
        } catch (PDOException $e) {
1✔
65
            throw new ConnectionNotEstablished(
1✔
66
                $id,
1✔
67
                "Connection not established: {$e->getMessage()}."
1✔
68
            );
1✔
69
        }
70
    }
71

72
    public function connection_iterator(): iterable
73
    {
74
        foreach ($this->definitions as $id => $definition) {
1✔
75
            yield $id => new ConnectionAccessor(
1✔
76
                $definition,
1✔
77
                isset($this->established[$id]),
1✔
78
                $this
1✔
79
            );
1✔
80
        }
81
    }
82
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc