Passed
Push — 6.0 ( f3ab72...785731 )
by Olivier
01:35
created

StaticModelProvider::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
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 Closure;
15
use ICanBoogie\ActiveRecord;
16
use LogicException;
17
18
/**
19
 * Provides a {@link Model} instance.
20
 */
21
final class StaticModelProvider
22
{
23
    /**
24
     * @var (Closure(): ModelProvider)|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment (Closure(): ModelProvider)|null at position 1 could not be parsed: Expected ')' at position 1, but found 'Closure'.
Loading history...
25
     */
26
    private static ?Closure $factory = null;
27
28
    private static ?ModelProvider $provider = null;
29
30
    /**
31
     * Sets the {@link ModelProvider} factory.
32
     *
33
     * @param (callable(): ModelProvider) $factory
0 ignored issues
show
Documentation Bug introduced by
The doc comment (callable(): ModelProvider) at position 1 could not be parsed: Expected ')' at position 1, but found 'callable'.
Loading history...
34
     *     The factory is invoked once: the first time {@link model_for_record} is invoked.
35
     *
36
     * @return (callable(): ModelProvider)|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment (callable(): ModelProvider)|null at position 1 could not be parsed: Expected ')' at position 1, but found 'callable'.
Loading history...
37
     *     The previous factory, or `null` if none was defined.
38
     */
39
    public static function set(callable $factory): ?callable
40
    {
41
        $previous = self::$factory;
42
43
        self::$factory = $factory(...);
44
        self::$provider = null;
45
46
        return $previous;
47
    }
48
49
    /**
50
     * Returns the current {@link ModelProvider} factory.
51
     *
52
     * @return (callable(): ModelProvider)|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment (callable(): ModelProvider)|null at position 1 could not be parsed: Expected ')' at position 1, but found 'callable'.
Loading history...
53
     */
54
    public static function get(): ?callable
55
    {
56
        return self::$factory;
57
    }
58
59
    /**
60
     * Unset the {@link ModelProvider} factory.
61
     */
62
    public static function unset(): void
63
    {
64
        self::$factory = null;
65
        self::$provider = null;
66
    }
67
68
    /**
69
     * Returns the Model for an ActiveRecord.
70
     *
71
     * @template T of ActiveRecord
72
     *
73
     * @param class-string<T> $activerecord_class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
74
     *
75
     * @phpstan-return Model<int|non-empty-string|non-empty-string[], T>
76
     **/
77
    public static function model_for_record(string $activerecord_class): Model
78
    {
79
        $factory = self::$factory
80
            ?? throw new LogicException(
81
                "No factory defined yet. Please define one with `StaticModelProvider::define()`"
82
            );
83
84
        return (self::$provider ??= $factory())->model_for_record($activerecord_class);
85
    }
86
}
87