• 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

95.35
/src/Models/PromiseJob.php
1
<?php
2

3
namespace Tochka\Promises\Models;
4

5
use Carbon\Carbon;
6
use Illuminate\Contracts\Queue\Job;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Factories\HasFactory;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Database\Eloquent\Relations\BelongsTo;
11
use Illuminate\Support\Facades\Config;
12
use Tochka\Promises\Contracts\MayPromised;
13
use Tochka\Promises\Core\BaseJob;
14
use Tochka\Promises\Core\Support\ConditionTransition;
15
use Tochka\Promises\Enums\StateEnum;
16
use Tochka\Promises\Models\Casts\ConditionsCast;
17
use Tochka\Promises\Models\Casts\SerializableClassCast;
18
use Tochka\Promises\Models\Factories\PromiseJobFactory;
19

20
/**
21
 * @api
22
 * @property int $id
23
 * @property int $promise_id
24
 * @property StateEnum $state
25
 * @property array<ConditionTransition> $conditions
26
 * @property MayPromised $initial_job
27
 * @property MayPromised $result_job
28
 * @property \Throwable|null $exception
29
 * @property Carbon $created_at
30
 * @property Carbon $updated_at
31
 * @property Promise|null $promise
32
 *
33
 * @psalm-suppress PropertyNotSetInConstructor
34
 */
35
class PromiseJob extends Model
36
{
37
    use HasFactory;
38

39
    protected $casts = [
40
        'promise_id' => 'int',
41
        'state' => StateEnum::class,
42
        'conditions' => ConditionsCast::class,
43
        'initial_job' => SerializableClassCast::class,
44
        'result_job' => SerializableClassCast::class,
45
        'exception' => SerializableClassCast::class,
46
    ];
47

48
    private ?BaseJob $baseJob = null;
49
    private ?StateEnum $changedState = null;
50
    private bool $nestedEvents = false;
51

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

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

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

68
    public function getTable(): string
69
    {
70
        return Config::get('promises.database.table_jobs', 'promise_jobs');
15✔
71
    }
72

73
    public function promise(): BelongsTo
74
    {
UNCOV
75
        return $this->belongsTo(Promise::class, 'promise_id', 'id');
×
76
    }
77

78
    public function scopeByPromise(Builder $query, int $promiseId): Builder
79
    {
80
        return $query->where('promise_id', $promiseId);
3✔
81
    }
82

83
    public function getBaseJob(): BaseJob
84
    {
85
        if ($this->baseJob === null) {
12✔
86
            $this->baseJob = new BaseJob($this->promise_id, $this->initial_job, $this->result_job);
12✔
87
        }
88

89
        $this->baseJob->setJobId($this->id);
12✔
90
        $this->baseJob->setConditions($this->conditions);
12✔
91
        $this->baseJob->setState($this->state);
12✔
92
        $this->baseJob->setException($this->exception);
12✔
93
        $this->baseJob->setCreatedAt($this->created_at);
12✔
94
        $this->baseJob->setUpdatedAt($this->updated_at);
12✔
95
        $this->baseJob->setInitial($this->initial_job);
12✔
96
        $this->baseJob->setResult($this->result_job);
12✔
97
        $this->baseJob->setAttachedModel($this);
12✔
98

99
        return $this->baseJob;
12✔
100
    }
101

102
    public static function saveBaseJob(BaseJob $baseJob): self
103
    {
104
        $model = $baseJob->getAttachedModel();
9✔
105

106
        $model->promise_id = $baseJob->getPromiseId();
9✔
107
        $model->setChangedState($model->state);
9✔
108
        $model->state = $baseJob->getState();
9✔
109
        $model->conditions = $baseJob->getConditions();
9✔
110
        $model->initial_job = $model->clearJobs(clone $baseJob->getInitialJob());
9✔
111
        $model->result_job = $model->clearJobs(clone $baseJob->getResultJob());
9✔
112
        $model->exception = $baseJob->getException();
9✔
113

114
        $model->save();
9✔
115

116
        $baseJob->setJobId($model->id);
9✔
117
        $baseJob->setCreatedAt($model->created_at);
9✔
118
        $baseJob->setUpdatedAt($model->updated_at);
9✔
119
        $baseJob->setAttachedModel($model);
9✔
120

121
        return $model;
9✔
122
    }
123

124
    private function clearJobs(MayPromised $job): MayPromised
125
    {
126
        try {
127
            $property = (new \ReflectionClass($job))->getProperty('job');
9✔
128
        } catch (\Exception) {
6✔
129
            return $job;
6✔
130
        }
131

132
        /** @psalm-suppress UnusedMethodCall */
133
        $property->setAccessible(true);
3✔
134
        $internalJob = $property->getValue($job);
3✔
135
        if ($internalJob instanceof Job) {
3✔
136
            /** @noinspection PhpRedundantOptionalArgumentInspection */
137
            $property->setValue($job, null);
3✔
138
        }
139

140
        return $job;
3✔
141
    }
142

143
    protected static function newFactory(): PromiseJobFactory
144
    {
145
        return new PromiseJobFactory();
6✔
146
    }
147

148
    public function getChangedState(): ?StateEnum
149
    {
150
        return $this->changedState;
3✔
151
    }
152

153
    public function setChangedState(?StateEnum $state): void
154
    {
155
        $this->changedState = $state;
12✔
156
    }
157
}
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