ColonyRegistry::reverseProxyServiceRegistry()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\Aent\Registry;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\RequestException;
7
use TheAentMachine\Aent\Registry\Exception\ColonyRegistryException;
8
9
final class ColonyRegistry
10
{
11
    private const ORCHESTRATOR = 'orchestrator';
12
    private const CI = 'ci';
13
    private const BUILDER = 'builder';
14
    private const REVERSE_PROXY_SERVICE = 'reverseproxyservice';
15
16
    public const DOCKER_COMPOSE = 'Docker Compose';
17
    public const KUBERNETES = 'Kubernetes';
18
    public const DOCKERFILE = 'Dockerfile';
19
    public const TRAEFIK = 'Traefik';
20
21
    /** @var AentItemRegistry[] */
22
    private $aents;
23
24
    /**
25
     * @return ColonyRegistry
26
     * @throws ColonyRegistryException
27
     */
28
    public static function orchestratorRegistry(): self
29
    {
30
        $self = new ColonyRegistry();
31
        $self->fetch(self::ORCHESTRATOR);
32
        return $self;
33
    }
34
35
    /**
36
     * @return ColonyRegistry
37
     * @throws ColonyRegistryException
38
     */
39
    public static function CIRegistry(): self
40
    {
41
        $self = new ColonyRegistry();
42
        $self->fetch(self::CI);
43
        return $self;
44
    }
45
46
    /**
47
     * @return ColonyRegistry
48
     * @throws ColonyRegistryException
49
     */
50
    public static function builderRegistry(): self
51
    {
52
        $self = new ColonyRegistry();
53
        $self->fetch(self::BUILDER);
54
        return $self;
55
    }
56
57
    /**
58
     * @return ColonyRegistry
59
     * @throws ColonyRegistryException
60
     */
61
    public static function reverseProxyServiceRegistry(): self
62
    {
63
        $self = new ColonyRegistry();
64
        $self->fetch(self::REVERSE_PROXY_SERVICE);
65
        return $self;
66
    }
67
68
    /**
69
     * @param string $category
70
     * @return void
71
     * @throws ColonyRegistryException
72
     */
73
    private function fetch(string $category): void
74
    {
75
        try {
76
            $client = new Client();
77
            $response = $client->request('GET', "https://raw.githubusercontent.com/theaentmachine/colony-registry/master/$category.json");
78
            $body = \GuzzleHttp\json_decode($response->getBody(), true);
79
            $this->aents = [];
80
            foreach ($body as $item) {
81
                $this->aents[] = AentItemRegistry::fromArray($item);
82
            }
83
        } catch (\Exception $e) {
84
            throw new ColonyRegistryException($e->getMessage(), $e->getCode(), $e);
85
        } catch (\GuzzleHttp\Exception\GuzzleException $e) {
86
            throw new ColonyRegistryException($e->getMessage(), $e->getCode(), $e);
87
        }
88
    }
89
90
    /**
91
     * @param string $name
92
     * @return AentItemRegistry
93
     * @throws ColonyRegistryException
94
     */
95
    public function getAent(string $name): AentItemRegistry
96
    {
97
        foreach ($this->aents as $aent) {
98
            if ($aent->getName() === $name) {
99
                return $aent;
100
            }
101
        }
102
        throw ColonyRegistryException::aentNotFound($name);
103
    }
104
105
    /**
106
     * @return AentItemRegistry[]
107
     */
108
    public function getAents(): array
109
    {
110
        return $this->aents;
111
    }
112
}
113