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

daycry / jobs / 24514486770

05 Apr 2026 08:48AM UTC coverage: 53.982% (-2.2%) from 56.164%
24514486770

push

github

daycry
Optimize

50 of 192 new or added lines in 14 files covered. (26.04%)

3 existing lines in 3 files now uncovered.

1220 of 2260 relevant lines covered (53.98%)

4.43 hits per line

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

38.46
/src/Traits/StateTrait.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Daycry Queues.
7
 *
8
 * (c) Daycry <daycry9@proton.me>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Daycry\Jobs\Traits;
15

16
use Daycry\Jobs\Execution\ExecutionResult;
17
use Daycry\Jobs\Job;
18

19
/**
20
 * Unified trait for job state management and notifications.
21
 * Combines StatusTrait and NotificableTrait functionality.
22
 *
23
 * @mixin Job
24
 */
25
trait StateTrait
26
{
27
    protected bool $enabled         = true;
28
    protected bool $notifyOnFailure = false;
29
    protected bool $notifyOnSuccess = false;
30

31
    // ============================================================
32
    // Status & Single Instance (formerly StatusTrait)
33
    // ============================================================
34

35
    private function runningCacheKey(): string
36
    {
37
        return 'job_running_' . $this->getName();
1✔
38
    }
39

40
    public function saveRunningFlag(): bool
41
    {
42
        $cache = service('cache');
×
43

NEW
44
        return $cache->save($this->runningCacheKey(), true, 0);
×
45
    }
46

47
    public function isRunning(): bool
48
    {
49
        $cache = service('cache');
1✔
50

51
        return (bool) $cache->get($this->runningCacheKey());
1✔
52
    }
53

54
    public function clearRunningFlag(): bool
55
    {
56
        $cache = service('cache');
×
57

NEW
58
        return $cache->delete($this->runningCacheKey());
×
59
    }
60

61
    public function disable(): self
62
    {
63
        $this->enabled = false;
1✔
64

65
        return $this;
1✔
66
    }
67

68
    public function isEnabled(): bool
69
    {
70
        return $this->enabled;
3✔
71
    }
72

73
    // ============================================================
74
    // Notifications (formerly NotificableTrait)
75
    // ============================================================
76

77
    public function notifyOnFailure(bool $notify = true): self
78
    {
79
        $this->notifyOnFailure = $notify;
1✔
80

81
        return $this;
1✔
82
    }
83

84
    public function notifyOnSuccess(bool $notify = true): self
85
    {
86
        $this->notifyOnSuccess = $notify;
1✔
87

88
        return $this;
1✔
89
    }
90

91
    public function shouldNotifyOnFailure(): bool
92
    {
93
        return $this->notifyOnFailure;
14✔
94
    }
95

96
    public function shouldNotifyOnSuccess(): bool
97
    {
98
        return $this->notifyOnSuccess;
22✔
99
    }
100

101
    public function notifyOnCompletion(bool $notify = true): self
102
    {
103
        $this->notifyOnFailure = $notify;
1✔
104
        $this->notifyOnSuccess = $notify;
1✔
105

106
        return $this;
1✔
107
    }
108

109
    public function notify(ExecutionResult $result): bool
110
    {
111
        $email  = service('email');
×
112
        $parser = service('parser');
×
113

114
        $content          = $result->success ? $result->output : $result->error;
×
115
        $normalizedOutput = null;
×
116
        if (is_array($content) || is_object($content)) {
×
117
            $normalizedOutput = json_encode($content);
×
118
        } elseif ($content !== null) {
×
119
            $normalizedOutput = (string) $content;
×
120
        }
121

122
        $email->setMailType('html');
×
123
        $email->setFrom(config('Jobs')->from, config('Jobs')->fromName);
×
124
        $email->setTo(config('Jobs')->to);
×
125
        $email->setSubject($parser->setData(['job' => $this->getName()])->renderString('Job {job} just finished running.'));
×
126
        $email->setMessage($parser->setData([
×
127
            'name'     => $this->getName(),
×
128
            'runStart' => null,
×
129
            'duration' => null,
×
130
            'output'   => ($result->success ? $normalizedOutput : null),
×
131
            'error'    => ($result->success !== true) ? $normalizedOutput : null,
×
132
        ])->render(config('Jobs')->emailNotificationView));
×
133

134
        return $email->send();
×
135
    }
136
}
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