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

ICanBoogie / ActiveRecord / 11645408333

02 Nov 2024 07:56PM UTC coverage: 84.054% (+0.008%) from 84.046%
11645408333

push

github

olvlvl
Add ModelAccessor

13 of 13 new or added lines in 2 files covered. (100.0%)

3 existing lines in 2 files now uncovered.

1381 of 1643 relevant lines covered (84.05%)

21.75 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
namespace ICanBoogie\ActiveRecord;
4

5
use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;
6
use PDOException;
7

8
use function ICanBoogie\iterable_to_dictionary;
9

10
/**
11
 * A collection of connections.
12
 */
13
class ConnectionCollection implements ConnectionProvider, ConnectionIterator
14
{
15
    /**
16
     * @var array<string, ConnectionDefinition>
17
     *     Where _key_ is a connection identifier.
18
     */
19
    public readonly array $definitions;
20

21
    /**
22
     * Established connections.
23
     *
24
     * @var array<string, Connection>
25
     *     Where _key_ is a connection identifier.
26
     */
27
    private array $established = [];
28

29
    /**
30
     * @param ConnectionDefinition[] $definitions
31
     */
32
    public function __construct(iterable $definitions)
33
    {
34
        // @phpstan-ignore-next-line
35
        $this->definitions = iterable_to_dictionary($definitions, fn(ConnectionDefinition $d) => $d->id);
63✔
36
    }
37

38
    public function connection_for_id(string $id): Connection
39
    {
40
        return $this->established[$id] ??= $this->new_connection($id);
60✔
41
    }
42

43
    private function new_connection(string $id): Connection
44
    {
45
        $definition = $this->definitions[$id]
60✔
UNCOV
46
            ?? throw new ConnectionNotDefined($id);
×
47

48
        #
49
        # we catch connection exceptions and rethrow them in order to avoid displaying sensible
50
        # information such as the username or password.
51
        #
52

53
        try {
54
            return new Connection($definition);
60✔
55
        } catch (PDOException $e) {
1✔
56
            throw new ConnectionNotEstablished(
1✔
57
                $id,
1✔
58
                "Connection not established: {$e->getMessage()}."
1✔
59
            );
1✔
60
        }
61
    }
62

63
    public function connection_iterator(): iterable
64
    {
65
        foreach ($this->definitions as $id => $definition) {
1✔
66
            yield $id => new ConnectionAccessor(
1✔
67
                $definition,
1✔
68
                isset($this->established[$id]),
1✔
69
                $this
1✔
70
            );
1✔
71
        }
72
    }
73
}
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