AgentTask::setSort()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
use Bitrix\Main\Type\DateTime;
10
11
/**
12
 * Builder for create new agents in the Bitrix queue.
13
 *
14
 * Usage:
15
 * ```php
16
 * use Notamedia\ConsoleJedi\Agent\AgentTask;
17
 * use Vendor\Module\TestAgent;
18
 *
19
 * AgentTask::builder()
20
 *      ->setClass(TestAgent::class)
21
 *      ->setConstructorArgs(['arg1', true])
22
 *      ->setCallChain([
23
 *          ['execute' => [100500]]
24
 *      ]),
25
 *      ->setModule('vendor.module')
26
 *      ->create();
27
 * ```
28
 * The result: will be the registered agent `\Vendor\Module\TestAgent::agent('arg1', true)->execute(100500);`.
29
 *
30
 * @author Nik Samokhvalov <[email protected]>
31
 */
32
class AgentTask
33
{
34
    protected $class;
35
    protected $constructorArgs = [];
36
    protected $callChain;
37
    protected $module;
38
    protected $interval;
39
    protected $periodically = false;
40
    protected $active = true;
41
    protected $executionTime;
42
    protected $sort;
43
    protected $userId;
44
45
    /**
46
     * Builder for create new task to the queue of agents.
47
     *
48
     * @return static
49
     */
50
    public static function build()
51
    {
52
        return new static;
53
    }
54
55
    /**
56
     * Sets agent class name.
57
     *
58
     * @param string $className
59
     *
60
     * @return $this
61
     */
62
    public function setClass($className)
63
    {
64
        $this->class = $className;
65
66
        return $this;
67
    }
68
69
    /**
70
     * Sets the arguments for `__constructor` of agent class.
71
     *
72
     * @param array $args
73
     *
74
     * @return $this
75
     */
76
    public function setConstructorArgs(array $args)
77
    {
78
        $this->constructorArgs = $args;
79
80
        return $this;
81
    }
82
83
    /**
84
     * Sets the chain methods with arguments for add them to agent name for execution.
85
     *
86
     * @param array $callChain
87
     *
88
     * @return $this
89
     */
90
    public function setCallChain(array $callChain)
91
    {
92
        $this->callChain = $callChain;
93
94
        return $this;
95
    }
96
97
    /**
98
     * Sets the name of the module to which the agent belongs.
99
     *
100
     * @param string $moduleName
101
     *
102
     * @return $this
103
     */
104
    public function setModule($moduleName)
105
    {
106
        $this->module = $moduleName;
107
108
        return $this;
109
    }
110
111
    /**
112
     * Sets the time interval between execution.
113
     *
114
     * @param int $seconds
115
     *
116
     * @return $this
117
     */
118
    public function setInterval($seconds)
119
    {
120
        $this->interval = (int)$seconds;
121
122
        return $this;
123
    }
124
125
    /**
126
     * Sets the periodically mode of agent.
127
     *
128
     * @param bool $periodically
129
     *
130
     * @return $this
131
     */
132
    public function setPeriodically($periodically)
133
    {
134
        $this->periodically = (bool)$periodically;
135
136
        return $this;
137
    }
138
139
    /**
140
     * Sets the activity of agent.
141
     *
142
     * @param bool $active
143
     *
144
     * @return $this
145
     */
146
    public function setActive($active)
147
    {
148
        $this->active = (bool)$active;
149
150
        return $this;
151
    }
152
153
    /**
154
     * Sets first execution time.
155
     *
156
     * @param DateTime $time
157
     *
158
     * @return $this
159
     */
160
    public function setExecutionTime(DateTime $time)
161
    {
162
        $this->executionTime = $time;
163
164
        return $this;
165
    }
166
167
    /**
168
     * Sets sorting.
169
     *
170
     * @param int $sort
171
     *
172
     * @return $this
173
     */
174
    public function setSort($sort)
175
    {
176
        $this->sort = (int)$sort;
177
178
        return $this;
179
    }
180
181
    /**
182
     * Sets user ID on whose behalf the agent is executed.
183
     *
184
     * @param int $userId User ID.
185
     *
186
     * @return $this
187
     */
188
    public function setUserId($userId)
189
    {
190
        $this->userId = (int)$userId;
191
192
        return $this;
193
    }
194
195
    /**
196
     * Convertation property for creation agent in queue through the old and dirty Bitrix API.
197
     */
198
    protected function convertation()
199
    {
200
        if ($this->executionTime instanceof DateTime) {
0 ignored issues
show
Bug introduced by
The class Bitrix\Main\Type\DateTime does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
201
            $this->executionTime = $this->executionTime->toString();
202
        } elseif ($this->executionTime === null) {
203
            $time = new DateTime();
204
            $this->executionTime = $time->toString();
205
        }
206
207
        foreach (['periodically', 'active'] as $property) {
208
            if ($this->$property === true) {
209
                $this->$property = 'Y';
210
            } else {
211
                $this->$property = 'N';
212
            }
213
        }
214
    }
215
216
    /**
217
     * Create agent in Bitrix queue.
218
     *
219
     * @param bool $checkExist Return false and set `CAdminException`, if agent already exist.
220
     *
221
     * @return bool|int ID of agent or false if `$checkExist` is true and agent already exist.
222
     */
223
    public function create($checkExist = false)
224
    {
225
        $this->convertation();
226
227
        $model = new \CAgent;
228
229
        return $model->AddAgent(
230
            AgentHelper::createName($this->class, $this->constructorArgs, $this->callChain),
231
            $this->module,
232
            $this->periodically,
233
            $this->interval,
234
            null,
235
            $this->active,
236
            $this->executionTime,
237
            $this->sort,
238
            $this->userId,
239
            $checkExist
240
        );
241
    }
242
}
243