AgentHelper::createName()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 3
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\ArgumentTypeException;
10
11
/**
12
 * Agent helpers.
13
 *
14
 * @author Nik Samokhvalov <[email protected]>
15
 */
16
class AgentHelper
17
{
18
    /**
19
     * Creates and returns agent name by class name and parameters.
20
     * Use to return this name from the executed method of agent.
21
     *
22
     * @param string $className Agent class name.
23
     * @param array $args Arguments for `__constructor` of agent class.
24
     * @param array $callChain
25
     *
26
     * @return string
27
     * @throws ArgumentTypeException
28
     */
29
    public static function createName($className, array $args = [], array $callChain = [])
30
    {
31
        $chain = '';
32
33
        if (!empty($callChain)) {
34
            foreach ($callChain as $method => $methodArgs) {
35
                if (!is_array($methodArgs)) {
36
                    throw new ArgumentTypeException('callChain', 'array');
37
                }
38
39
                $chain .= '->' . $method . '(' . static::convertArgsToString($methodArgs) . ')';
40
            }
41
        }
42
43
        return '\\' . $className . '::agent(' . static::convertArgsToString($args) . ')' . $chain . ';';
44
    }
45
46
    protected static function convertArgsToString(array $args)
47
    {
48
        $args = json_encode($args, JSON_UNESCAPED_SLASHES);
49
        $args = str_replace(',', ', ', $args);
50
        $args = substr($args, 1);
51
        $args = substr($args, 0, -1);
52
53
        return $args;
54
    }
55
}