ExampleComplexJob::foo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 9
Ratio 90 %

Importance

Changes 0
Metric Value
cc 1
dl 9
loc 10
rs 9.9332
c 0
b 0
f 0
nc 1
nop 1
1
<?php
2
namespace PHPDaemon\Examples;
3
4
/*
5
 * All listeners will be called after async calling "foo", "bar", "baz" jobs.
6
 *
7
 * @package Examples
8
 * @subpackage Base
9
 *
10
 * @author Vasily Zorin <[email protected]>
11
 */
12
class ExampleComplexJob extends \PHPDaemon\Core\AppInstance
13
{
14
15
    /**
16
     * Called when the worker is ready to go
17
     *
18
     * @return void
19
     */
20
    public function onReady()
21
    {
22
23
        // Adding listener
24
        // ComplexJob - STATE_WAITING
25
        $job = new \PHPDaemon\Core\ComplexJob(function ($job) {
26
            // ComplexJob - STATE_DONE
27
            /*array (
28
              'bar' =>
29
              array (
30
                    'job' => 'bar',
31
                    'success' => false,
32
                    'line' => 63,
33
              ),
34
              'foo' =>
35
              array (
36
                    'job' => 'foo',
37
                    'success' => true,
38
                    'line' => 84,
39
                    'arg' =>
40
                    array (
41
                      'param' => 'value',
42
                    ),
43
              ),
44
              'baz' =>
45
              array (
46
                    'job' => 'baz',
47
                    'success' => false,
48
                    'line' => 94,
49
              ),
50
            )*/
51
            \PHPDaemon\Core\Daemon::log($job->results);
52
        });
53
54
        // Adding listener
55
        // ComplexJob - STATE_WAITING
56
        $job->addListener(function ($job) {
0 ignored issues
show
Unused Code introduced by
The parameter $job is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
            // ComplexJob - STATE_DONE
58
        });
59
60
        // Adding async job foo
61
        $job('foo', $this->foo(['param' => 'value']));
62
63
        // Adding with 1 sec delay
64
        \PHPDaemon\Core\Timer::add(function ($event) use ($job) {
65
66
            // Adding async job bar
67 View Code Duplication
            $job('bar', function ($jobname, $job) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
                \PHPDaemon\Core\Timer::add(function ($event) use ($jobname, $job) {
69
                    // Job done
70
                    $job->setResult($jobname, ['job' => 'bar', 'success' => false, 'line' => __LINE__]);
71
                    $event->finish();
72
                }, 1e3 * 50);
73
            });
74
75
            // Adding async job baz. Equal $job('baz', $this->baz());
76
            $job->addJob('baz', $this->baz());
77
78
            // Run jobs. All listeners will be called when the jobs done
79
            // ComplexJob - STATE_RUNNING
80
            $job();
81
82
            $event->finish();
83
        }, 1e6 * 1);
84
    }
85
86 View Code Duplication
    final public function foo($arg)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        return function ($jobname, $job) use ($arg) {
89
            \PHPDaemon\Core\Timer::add(function ($event) use ($jobname, $job, $arg) {
90
                // Job done
91
                $job->setResult($jobname, ['job' => 'foo', 'success' => true, 'line' => __LINE__, 'arg' => $arg]);
92
                $event->finish();
93
            }, 1e3 * 100);
94
        };
95
    }
96
97 View Code Duplication
    final public function baz()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        return function ($jobname, $job) {
100
            \PHPDaemon\Core\Timer::add(function ($event) use ($jobname, $job) {
101
                // Job done
102
                $job->setResult($jobname, ['job' => 'baz', 'success' => false, 'line' => __LINE__]);
103
                $event->finish();
104
            }, 1e3 * 300);
105
        };
106
    }
107
}
108