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

tempestphp / tempest-framework / 14024978163

23 Mar 2025 05:55PM UTC coverage: 79.391% (-0.05%) from 79.441%
14024978163

push

github

web-flow
feat(view): cache Blade and Twig templates in internal storage (#1061)

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

912 existing lines in 110 files now uncovered.

10478 of 13198 relevant lines covered (79.39%)

91.09 hits per line

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

98.72
/src/Tempest/Database/src/IsDatabaseModel.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Database;
6

7
use Tempest\Database\Builder\ModelQueryBuilder;
8
use Tempest\Database\Builder\TableName;
9
use Tempest\Database\Config\DatabaseConfig;
10
use Tempest\Database\Exceptions\MissingRelation;
11
use Tempest\Database\Exceptions\MissingValue;
12
use Tempest\Reflection\ClassReflector;
13
use Tempest\Reflection\PropertyReflector;
14

15
use function Tempest\get;
16
use function Tempest\make;
17

18
/** @phpstan-require-implements \Tempest\Database\DatabaseModel */
19
trait IsDatabaseModel
20
{
21
    public ?Id $id = null;
22

23
    public function __get(string $name): mixed
3✔
24
    {
25
        $property = PropertyReflector::fromParts($this, $name);
3✔
26

27
        if ($property->hasAttribute(Lazy::class)) {
3✔
28
            $this->load($name);
1✔
29

30
            return $property->getValue($this);
1✔
31
        }
32

33
        $type = $property->getType();
2✔
34

35
        if ($type->isIterable()) {
2✔
UNCOV
36
            throw new MissingRelation($this, $name);
×
37
        }
38

39
        if ($type->isBuiltIn()) {
2✔
40
            throw new MissingValue($this, $name);
1✔
41
        }
42

43
        throw new MissingRelation($this, $name);
1✔
44
    }
45

46
    public function setId(Id $id): self
57✔
47
    {
48
        $this->id = $id;
57✔
49

50
        return $this;
57✔
51
    }
52

53
    public function getId(): Id
19✔
54
    {
55
        return $this->id;
19✔
56
    }
57

58
    public static function table(): TableName
63✔
59
    {
60
        $name = get(DatabaseConfig::class)
63✔
61
            ->namingStrategy->getName(self::class);
63✔
62

63
        return new TableName($name);
63✔
64
    }
65

66
    public static function new(mixed ...$params): self
62✔
67
    {
68
        return make(self::class)->from($params);
62✔
69
    }
70

71
    /**
72
     * @return \Tempest\Database\Builder\ModelQueryBuilder<self>
73
     */
74
    public static function query(): ModelQueryBuilder
49✔
75
    {
76
        return new ModelQueryBuilder(self::class);
49✔
77
    }
78

79
    /** @return self[] */
80
    public static function all(array $relations = []): array
14✔
81
    {
82
        return self::query()
14✔
83
            ->with(...$relations)
14✔
84
            ->all();
14✔
85
    }
86

87
    public static function get(Id $id, array $relations = []): ?self
20✔
88
    {
89
        return self::query()
20✔
90
            ->with(...$relations)
20✔
91
            ->get($id);
20✔
92
    }
93

94
    public static function find(mixed ...$conditions): ModelQueryBuilder
1✔
95
    {
96
        $query = self::query();
1✔
97

98
        array_walk($conditions, fn ($value, $column) => $query->whereField($column, $value));
1✔
99

100
        return $query;
1✔
101
    }
102

103
    public function load(string ...$relations): self
9✔
104
    {
105
        $new = self::get($this->getId(), $relations);
9✔
106

107
        foreach (new ClassReflector($new)->getPublicProperties() as $property) {
9✔
108
            $property->setValue($this, $property->getValue($new));
9✔
109
        }
110

111
        return $this;
9✔
112
    }
113

114
    public static function create(mixed ...$params): self
57✔
115
    {
116
        $model = self::new(...$params);
57✔
117

118
        $id = make(Query::class)->from($model)->execute();
57✔
119

120
        $model->setId($id);
57✔
121

122
        return $model;
57✔
123
    }
124

125
    public static function updateOrNew(array $find, array $update): self
1✔
126
    {
127
        $existing = self::query()->bind(...$find);
1✔
128

129
        foreach ($find as $key => $value) {
1✔
130
            $existing = $existing->where("{$key} = :{$key}");
1✔
131
        }
132

133
        $model = $existing->first() ?? self::new(...$find);
1✔
134

135
        foreach ($update as $key => $value) {
1✔
136
            $model->{$key} = $value;
1✔
137
        }
138

139
        return $model;
1✔
140
    }
141

142
    public static function updateOrCreate(array $find, array $update): self
1✔
143
    {
144
        return self::updateOrNew($find, $update)->save();
1✔
145
    }
146

147
    public function save(): self
39✔
148
    {
149
        $id = make(Query::class)->from($this)->execute();
39✔
150

151
        $this->setId($id);
39✔
152

153
        return $this;
39✔
154
    }
155

156
    public function update(mixed ...$params): self
1✔
157
    {
158
        foreach ($params as $key => $value) {
1✔
159
            $this->{$key} = $value;
1✔
160
        }
161

162
        $query = make(Query::class)->from($this);
1✔
163

164
        $query->execute();
1✔
165

166
        return $this;
1✔
167
    }
168

169
    public function delete(): void
2✔
170
    {
171
        $table = self::table();
2✔
172

173
        $query = new Query(
2✔
174
            sprintf(
2✔
175
                'DELETE FROM %s WHERE `id` = :id',
2✔
176
                $table,
2✔
177
            ),
2✔
178
            [
2✔
179
                'id' => $this->getId()->id,
2✔
180
            ],
2✔
181
        );
2✔
182

183
        $query->execute();
2✔
184
    }
185
}
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