Completed
Push — master ( f105fc...3abe44 )
by Sergii
02:30
created

Interaction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 2
cbo 1
dl 0
loc 97
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A debug() 0 10 2
A consoleOutput() 0 16 2
A hasTag() 0 4 1
A getTag() 0 4 2
A collectTags() 0 13 3
1
<?php
2
/**
3
 * @author Sergey Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Utils;
6
7
// Helpers.
8
use Symfony\Component\Console\Output\ConsoleOutput;
9
10
trait Interaction
11
{
12
    /**
13
     * @var string[]
14
     */
15
    private static $tags = [];
16
    /**
17
     * @var ConsoleOutput
18
     */
19
    private static $consoleOutput;
20
21
    /**
22
     * @param array $strings
23
     */
24
    public static function debug(array $strings)
25
    {
26
        if (self::hasTag('debug')) {
27
            array_unshift($strings, '<question>DEBUG:</question>');
28
            call_user_func_array(['self', 'consoleOutput'], array_merge(
29
                ['comment', 4, $strings],
30
                array_slice(func_get_args(), 1))
31
            );
32
        }
33
    }
34
35
    /**
36
     * @param string $type
37
     *   Could be "comment", "info", "question" or "error".
38
     * @param int $indent
39
     *   Number of spaces.
40
     * @param array $strings
41
     *   Paragraphs.
42
     * @param string ...
43
     *   Any replacement argument for "sprintf()".
44
     *
45
     * @link http://symfony.com/doc/current/components/console/introduction.html#coloring-the-output
46
     */
47
    public static function consoleOutput($type, $indent, array $strings)
48
    {
49
        if (!isset(self::$consoleOutput)) {
50
            self::$consoleOutput = new ConsoleOutput;
51
        }
52
53
        $indent = implode(' ', array_fill_keys(range(0, $indent), ''));
54
        $arguments = func_get_args();
55
        // Remove the "indent" and "strings" parameters from an array with arguments.
56
        unset($arguments[1], $arguments[2]);
57
58
        // Replace the "type" argument by message that will be printed.
59
        $arguments[0] = "$indent<$type>" . implode(PHP_EOL . "</$type>$indent<$type>", $strings) . "</$type>";
60
61
        self::$consoleOutput->writeln(call_user_func_array('sprintf', $arguments));
62
    }
63
64
    /**
65
     * @param string $tag
66
     *   The name of tag.
67
     *
68
     * @return bool
69
     *   Indicates the state of tag existence in a feature and/or scenario.
70
     */
71
    public static function hasTag($tag)
72
    {
73
        return isset(self::$tags[$tag]);
74
    }
75
76
    /**
77
     * @param string $tag
78
     *   The name of tag.
79
     * @param string $default
80
     *   Default value, if tag does not exist or empty.
81
     *
82
     * @return string
83
     *   Tag value or an empty string.
84
     */
85
    public static function getTag($tag, $default = '')
86
    {
87
        return empty(self::$tags[$tag]) ? $default : self::$tags[$tag];
88
    }
89
90
    /**
91
     * @param string[] $tags
92
     */
93
    public static function collectTags(array $tags)
94
    {
95
        foreach ($tags as $tag) {
96
            $values = explode(':', $tag);
97
            $value = '';
98
99
            if (count($values) > 1) {
100
                list($tag, $value) = $values;
101
            }
102
103
            self::$tags[strtolower($tag)] = $value;
104
        }
105
    }
106
}
107