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

tochka-developers / queue-promises / 9935584685

15 Jul 2024 07:44AM UTC coverage: 47.259% (-18.3%) from 65.524%
9935584685

push

github

darkdarin
fix: tests

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

431 existing lines in 40 files now uncovered.

612 of 1295 relevant lines covered (47.26%)

2.53 hits per line

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

93.02
/src/Models/Promise.php
1
<?php
2

3
/** @noinspection PhpMissingFieldTypeInspection */
4

5
namespace Tochka\Promises\Models;
6

7
use Carbon\Carbon;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Database\Eloquent\Factories\HasFactory;
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Database\Eloquent\Relations\HasMany;
12
use Illuminate\Support\Collection;
13
use Illuminate\Support\Facades\Config;
14
use Tochka\Promises\Contracts\PromiseHandler;
15
use Tochka\Promises\Core\BasePromise;
16
use Tochka\Promises\Core\Support\ConditionTransition;
17
use Tochka\Promises\Enums\StateEnum;
18
use Tochka\Promises\Models\Casts\ConditionsCast;
19
use Tochka\Promises\Models\Casts\SerializableClassCast;
20
use Tochka\Promises\Models\Factories\PromiseFactory;
21

22
/**
23
 * @api
24
 * @property int $id
25
 * @property int|null $parent_job_id
26
 * @property StateEnum $state
27
 * @property array<ConditionTransition> $conditions
28
 * @property PromiseHandler $promise_handler
29
 * @property Carbon $created_at
30
 * @property Carbon $updated_at
31
 * @property Carbon $watch_at
32
 * @property Carbon $timeout_at
33
 * @property Collection<int, PromiseJob> $jobs
34
 * @property Collection<int, PromiseEvent> $events
35
 *
36
 * @psalm-suppress PropertyNotSetInConstructor
37
 */
38
class Promise extends Model
39
{
40
    use HasFactory;
41

42
    protected $casts = [
43
        'state' => StateEnum::class,
44
        'conditions' => ConditionsCast::class,
45
        'promise_handler' => SerializableClassCast::class,
46
        'watch_at' => 'datetime',
47
        'timeout_at' => 'datetime',
48
    ];
49

50
    private ?BasePromise $basePromise = null;
51
    private ?StateEnum $changedState = null;
52
    private bool $nestedEvents = false;
53

54
    public function setNestedEvents(bool $nestedEvents): void
55
    {
UNCOV
56
        $this->nestedEvents = $nestedEvents;
×
57
    }
58

59
    public function isNestedEvents(): bool
60
    {
61
        return $this->nestedEvents;
3✔
62
    }
63

64
    public function getConnectionName(): ?string
65
    {
66
        /** @noinspection PhpRedundantOptionalArgumentInspection */
67
        return Config::get('promises.database.connection', null);
12✔
68
    }
69

70
    public function getTable(): string
71
    {
72
        return Config::get('promises.database.table_promises', 'promises');
12✔
73
    }
74

75
    /**
76
     * @return HasMany
77
     */
78
    public function jobs(): HasMany
79
    {
UNCOV
80
        return $this->hasMany(PromiseJob::class, 'promise_id', 'id');
×
81
    }
82

83
    /**
84
     * @return HasMany
85
     */
86
    public function events(): HasMany
87
    {
UNCOV
88
        return $this->hasMany(PromiseEvent::class, 'promise_id', 'id');
×
89
    }
90

91
    public function scopeInStates(Builder $query, array $states): Builder
92
    {
93
        return $query->whereIn('state', $states);
3✔
94
    }
95

96
    public function scopeForWatch(Builder $query): Builder
97
    {
98
        return $query->where(
3✔
99
            function (Builder $query) {
3✔
100
                $query->where('watch_at', '<=', Carbon::now())
3✔
101
                    ->orWhere('timeout_at', '<=', Carbon::now());
3✔
102
            },
3✔
103
        );
3✔
104
    }
105

106
    public function getBasePromise(): BasePromise
107
    {
108
        if ($this->basePromise === null) {
6✔
109
            $this->basePromise = new BasePromise($this->promise_handler);
6✔
110
        }
111

112
        $this->basePromise->setPromiseId($this->id);
6✔
113
        $this->basePromise->setState($this->state);
6✔
114
        $this->basePromise->setConditions($this->conditions);
6✔
115
        $this->basePromise->setPromiseHandler($this->promise_handler);
6✔
116
        $this->basePromise->setCreatedAt($this->created_at);
6✔
117
        $this->basePromise->setUpdatedAt($this->updated_at);
6✔
118
        $this->basePromise->setWatchAt($this->watch_at);
6✔
119
        $this->basePromise->setTimeoutAt($this->timeout_at);
6✔
120
        $this->basePromise->setParentJobId($this->parent_job_id);
6✔
121
        $this->basePromise->setAttachedModel($this);
6✔
122

123
        return $this->basePromise;
6✔
124
    }
125

126
    public static function saveBasePromise(BasePromise $basePromise): self
127
    {
128
        $model = $basePromise->getAttachedModel();
3✔
129

130
        $model->setChangedState($model->state);
3✔
131
        $model->state = $basePromise->getState();
3✔
132
        $model->conditions = $basePromise->getConditions();
3✔
133
        $model->promise_handler = clone $basePromise->getPromiseHandler();
3✔
134
        $model->watch_at = $basePromise->getWatchAt();
3✔
135
        $model->timeout_at = $basePromise->getTimeoutAt();
3✔
136
        $model->parent_job_id = $basePromise->getParentJobId();
3✔
137

138
        $model->save();
3✔
139

140
        $basePromise->setPromiseId($model->id);
3✔
141
        $basePromise->setCreatedAt($model->created_at);
3✔
142
        $basePromise->setUpdatedAt($model->updated_at);
3✔
143
        $basePromise->setAttachedModel($model);
3✔
144

145
        return $model;
3✔
146
    }
147

148
    protected static function newFactory(): PromiseFactory
149
    {
150
        return new PromiseFactory();
9✔
151
    }
152

153
    public function getChangedState(): ?StateEnum
154
    {
155
        return $this->changedState;
3✔
156
    }
157

158
    public function setChangedState(?StateEnum $state): void
159
    {
160
        $this->changedState = $state;
6✔
161
    }
162
}
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