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

ICanBoogie / ActiveRecord / 6362433236

30 Sep 2023 11:14AM UTC coverage: 85.731% (+5.6%) from 80.178%
6362433236

push

github

olvlvl
Rename StaticModelProvider methods

1436 of 1675 relevant lines covered (85.73%)

29.41 hits per line

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

96.15
/lib/ActiveRecord/ModelCollection.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\Accessor\AccessorTrait;
15
use ICanBoogie\ActiveRecord;
16
use ICanBoogie\ActiveRecord\Config\ModelDefinition;
17
use LogicException;
18
use Throwable;
19

20
/**
21
 * Model collection.
22
 *
23
 * @property-read array<class-string<ActiveRecord>, Model> $instances
24
 */
25
class ModelCollection implements ModelProvider, ModelIterator
26
{
27
    /**
28
     * @uses get_instances
29
     */
30
    use AccessorTrait;
31

32
    /**
33
     * Instantiated models.
34
     *
35
     * @var array<class-string<ActiveRecord>, Model>
36
     */
37
    private array $instances = [];
38

39
    /**
40
     * @return array<class-string<ActiveRecord>, Model>
41
     */
42
    private function get_instances(): array
43
    {
44
        return $this->instances;
1✔
45
    }
46

47
    /**
48
     * @param array<class-string<ActiveRecord>, ModelDefinition> $definitions
49
     */
50
    public function __construct(
51
        public readonly ConnectionProvider $connections,
52
        public readonly array $definitions,
53
    ) {
54
    }
83✔
55

56
    public function model_for_record(string $activerecord_class): Model
57
    {
58
        return $this->instances[$activerecord_class] ??= $this->instantiate_model($activerecord_class);
82✔
59
    }
60

61
    public function model_iterator(): iterable
62
    {
63
        foreach ($this->definitions as $activerecord_class => $definition) {
61✔
64
            // @phpstan-ignore-next-line
65
            yield $activerecord_class => fn() => $this->model_for_record($definition->activerecord_class);
61✔
66
        }
67
    }
68

69
    /**
70
     * @param class-string<ActiveRecord> $activerecord_class
71
     */
72
    private function instantiate_model(string $activerecord_class): Model
73
    {
74
        $definition = $this->definitions[$activerecord_class]
82✔
75
            ?? throw new LogicException("No model definition for '$activerecord_class'");
×
76

77
        return new $definition->model_class(
82✔
78
            $this->connections->connection_for_id($definition->connection),
82✔
79
            $this,
82✔
80
            $definition
82✔
81
        );
82✔
82
    }
83

84
    /**
85
     * Install all the models.
86
     *
87
     * @throws Throwable
88
     */
89
    public function install(): void
90
    {
91
        foreach ($this->model_iterator() as $get) {
60✔
92
            $model = $get();
60✔
93

94
            if ($model->is_installed()) {
60✔
95
                continue;
1✔
96
            }
97

98
            $model->install();
60✔
99
        }
100
    }
101

102
    /**
103
     * Uninstall all the models.
104
     *
105
     * @throws Throwable
106
     */
107
    public function uninstall(): void
108
    {
109
        foreach ($this->model_iterator() as $get) {
1✔
110
            $model = $get();
1✔
111

112
            if (!$model->is_installed()) {
1✔
113
                continue;
1✔
114
            }
115

116
            $model->uninstall();
1✔
117
        }
118
    }
119

120
    /**
121
     * Check if models are installed.
122
     *
123
     * @return array<class-string<Model>, bool>
124
     *     An array of key/value pair where _key_ is a model class and
125
     *     _value_ `true` if the model is installed, `false` otherwise.
126
     */
127
    public function is_installed(): array
128
    {
129
        $rc = [];
2✔
130

131
        foreach ($this->model_iterator() as $activerecord_class => $get) {
2✔
132
            $rc[$activerecord_class] = $get()->is_installed();
2✔
133
        }
134

135
        return $rc;
2✔
136
    }
137
}
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

© 2026 Coveralls, Inc