Passed
Push — master ( 45927b...26b8fa )
by Marc
02:33
created

FbpDumper::examineProcess()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3.0021

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 15
cts 16
cp 0.9375
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 2
crap 3.0021
1
<?php
2
/*
3
 * This file is part of the phpflo\phpflo-fbp package.
4
 *
5
 * (c) Marc Aschmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PhpFlo\Fbp;
12
13
use PhpFlo\Common\FbpDefinitionsInterface;
14
use PhpFlo\Exception\DumperException;
15
use Symfony\Component\Yaml\Yaml;
16
17
/**
18
 * Class FbpDumper
19
 *
20
 * @package PhpFlo\Fbp
21
 * @author Marc Aschmann <[email protected]>
22
 */
23
final class FbpDumper implements FbpDefinitionsInterface
24
{
25
    /**
26
     * @var array
27
     */
28
    private static $processes;
29
30
    /**
31
     * @param array $definition
32
     * @return string json
33
     */
34 1
    public static function toJson(array $definition)
35
    {
36 1
        return json_encode($definition, JSON_PRETTY_PRINT);
37
    }
38
39
    /**
40
     * @param array $definition
41
     * @param int $inline level until inlining starts
42
     * @return string yaml
43
     */
44 1
    public static function toYaml(array $definition, $inline = 3)
45
    {
46 1
        return Yaml::dump($definition, $inline);
47
    }
48
49
    /**
50
     * @param array $definition
51
     * @return string
52
     */
53 1
    public static function toFbp(array $definition)
54
    {
55 1
        return self::createFbp($definition);
56
    }
57
58
    /**
59
     * @param array $definition
60
     * @return string
61
     */
62 1
    private static function createFbp(array $definition)
63
    {
64 1
        $fbp = [];
65
        // handle initializer
66 1
        if (!empty($definition[self::INITIALIZERS_LABEL])) {
67 View Code Duplication
            if (empty($definition[self::INITIALIZERS_LABEL][self::DATA_LABEL])) {
68
                throw new DumperException("Defintion has " .
69
                    self::INITIALIZERS_LABEL . " but no 
70
                    " . self::DATA_LABEL . " node"
71
                );
72
            }
73
74 View Code Duplication
            if (empty($definition[self::INITIALIZERS_LABEL][self::TARGET_LABEL])) {
75
                throw new DumperException("Defintion has " .
76
                    self::INITIALIZERS_LABEL . " but no 
77
                    " . self::TARGET_LABEL . " node"
78
                );
79
            }
80
81
            array_push(
82
                $fbp,
83
                self::connectPorts(
84
                    $definition[self::INITIALIZERS_LABEL][self::DATA_LABEL],
85
                    self::examineProcess(self::TARGET_LABEL, $definition[self::INITIALIZERS_LABEL][self::TARGET_LABEL])
86
                )
87
            );
88
        }
89
90 1
        if (self::hasElement(self::PROCESSES_LABEL, $definition)) {
91 1
            self::$processes = $definition[self::PROCESSES_LABEL];
92 1
        }
93
94 1
        foreach ($definition[self::CONNECTIONS_LABEL] as $connection) {
95 1
            array_push($fbp, self::examineConnectionTouple($connection));
96 1
        }
97
98 1
        return implode(self::FILE_LINEFEED, $fbp);
99
    }
100
101
    /**
102
     * Look for all needed fields and build a port -> port connection.
103
     *
104
     * @param array $connectionTouple
105
     * @return string
106
     */
107 2
    private static function examineConnectionTouple(array $connectionTouple)
108
    {
109 1
        self::hasElement(self::SOURCE_LABEL, $connectionTouple);
110 1
        self::hasElement(self::TARGET_LABEL, $connectionTouple);
111
112 1
        return self::connectPorts(
113 1
            self::examineProcess(self::SOURCE_LABEL, $connectionTouple[self::SOURCE_LABEL]),
114 2
            self::examineProcess(self::TARGET_LABEL, $connectionTouple[self::TARGET_LABEL])
115 1
        );
116
    }
117
118
    /**
119
     * @param string $type
120
     * @param array $processPart
121
     * @return string
122
     */
123 1
    private static function examineProcess($type, array $processPart)
124
    {
125 1
        self::hasElement(self::PROCESS_LABEL, $processPart);
126 1
        self::hasElement(self::PORT_LABEL, $processPart);
127
        
128 1
        $inport = '';
129 1
        $outport = '';
130 1
        $process = $processPart[self::PROCESS_LABEL];
131 1
        $port = $processPart[self::PORT_LABEL];
132
133 1
        if (self::hasElement($process, self::$processes, false)) {
134 1
            $meta = "(" . self::$processes[$process][self::COMPONENT_LABEL] . ")";
135 1
        } else {
136
            throw new DumperException("{$process} is not defined in " . self::PROCESSES_LABEL);
137
        }
138
139 1
        if (self::SOURCE_LABEL == $type) {
140 1
            $outport = " {$port}";
141 1
        } else {
142 1
            $inport = "{$port} ";
143
        }
144
145 1
        return "{$inport}{$process}{$meta}{$outport}";
146
    }
147
148
    /**
149
     * @param string $needle
150
     * @param array $haystack
151
     * @param bool $triggerException
152
     * @return bool
153
     */
154 1
    private static function hasElement($needle, array $haystack, $triggerException = true)
155
    {
156 1
        if (empty($haystack[$needle])) {
157
            if ($triggerException) {
158
                throw new DumperException("Element has no {$needle}");
159
            } else {
160
                return false;
161
            }
162
        }
163
164 1
        return true;
165
    }
166
167
    /**
168
     * @param string $sourcePort
169
     * @param string $targetPort
170
     * @return string
171
     */
172 1
    private static function connectPorts($sourcePort, $targetPort)
173
    {
174 1
        return implode(
175 1
            " " . self::SOURCE_TARGET_SEPARATOR . " ",
176
            [
177 1
                $sourcePort,
178
                $targetPort
179 1
            ]
180 1
        );
181
    }
182
}
183