ServiceReference::getIterator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
namespace Raigu\XRoad\SoapEnvelope;
4
5
/**
6
 * I am a string representation of an X-Road service.
7
 *
8
 * I act like iterator over associative array.
9
 * Returned array keys are same as tag names in SOAP envelope header.
10
 */
11
final class ServiceReference implements \IteratorAggregate
12
{
13
    /**
14
     * @var string[]
15
     */
16
    private $parts;
17
18
    public function getIterator()
19
    {
20
        yield from array_combine(
21
            [
22
                'xRoadInstance',
23
                'memberClass',
24
                'memberCode',
25
                'subsystemCode',
26
                'serviceCode',
27
                'serviceVersion',
28
            ],
29
            $this->parts
30
        );
31
    }
32
33
    /**
34
     * @param string $reference reference to the serice.
35
     *                   Format: {xRoadInstance}/{memberClass/{memberCode}/{subsystemCode}/{serviceCode}/{serviceVerson}
36
     *                   Example: EE/GOV/70000310/DHX.Riigi-Teataja/sendDocument/v1
37
     */
38
    public function __construct(string $reference)
39
    {
40
        $parts = explode('/', $reference);
41
        if (count($parts) !== 6) {
42
            throw new \Exception('Could not extract service parameters. Invalid format.');
43
        }
44
45
        return $this->parts = $parts;
46
    }
47
}
48