Agent::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * For the full copyright and license information, please view the LICENSE.md
4
 * file that was distributed with this source code.
5
 */
6
7
namespace Notamedia\ConsoleJedi\Agent;
8
9
/**
10
 * Abstract class agent for development simple workers.
11
 *
12
 * Algorithm of agent execution:
13
 * 1. Bitrix launches method `Agent::agent()->run()`. Your agents should be registered in the same format:
14
 * `\Vendor\Packeage\ClassName::agent()->run();`. All arguments from this method will be duplicated to the
15
 * object constructor:
16
 * `agent($arg1, …, $arg2)` → `__construct($arg1, …, $arg2)`.
17
 * 2. Create an object of agent class.
18
 * 3. Call `init()` method. It is needed for some initial operations, for example: loading required modules.
19
 * 4. Call `execute()` method. This will execute main agent's logic.
20
 *
21
 * @author Nik Samokhvalov <[email protected]>
22
 */
23
abstract class Agent
24
{
25
    use AgentTrait;
26
27
    /**
28
     * Runs the Agent.
29
     *
30
     * Notice, that overriding agent's initialisation and body, should be done though `init` and `execute` methods,
31
     * not here.
32
     *
33
     * @see Agent::init()
34
     * @see Agent::execute()
35
     */
36
    public function run()
37
    {
38
        $this->init();
39
40
        return $this->execute();
41
    }
42
43
    /**
44
     * Initialization of the agent.
45
     */
46
    protected function init()
47
    {
48
    }
49
50
    /**
51
     * Agent execution.
52
     *
53
     * @return string Agent name if need again add his to queue. Use `$this->getAgentName()` for get name of agent.
54
     */
55
    protected function execute()
56
    {
57
    }
58
}
59