AbstractServiceAddEvent::beforeExecute()   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\Event\Service;
4
5
use Safe\Exceptions\StringsException;
6
use TheAentMachine\Aent\Context\Context;
7
use TheAentMachine\Aent\Event\AbstractEvent;
8
use TheAentMachine\Aent\Event\Service\Model\Environments;
9
use TheAentMachine\Aent\Event\Service\Model\ServiceState;
10
use TheAentMachine\Aenthill\Aenthill;
11
use TheAentMachine\Service\Service;
12
use function Safe\sprintf;
13
14
abstract class AbstractServiceAddEvent extends AbstractEvent
15
{
16
    /**
17
     * @param Environments $environments
18
     * @return ServiceState[]
19
     */
20
    abstract protected function createServices(Environments $environments): array;
21
22
    /**
23
     * @return string
24
     */
25
    protected function getEventName(): string
26
    {
27
        return 'ADD';
28
    }
29
30
    /**
31
     * @return bool
32
     */
33
    protected function shouldRegisterEvents(): bool
34
    {
35
        return false;
36
    }
37
38
    /**
39
     * @return void
40
     * @throws StringsException
41
     */
42
    protected function beforeExecute(): void
43
    {
44
        $this->output->writeln(sprintf(
45
            "šŸ‘‹ Hello! I'm the aent <info>%s</info> and I'm going to setup myself!",
46
            $this->getAentName()
47
        ));
48
    }
49
50
    /**
51
     * @param null|string $payload
52
     * @return null|string
53
     * @throws StringsException
54
     */
55
    protected function executeEvent(?string $payload): ?string
56
    {
57
        $environments = $this->fetchEnvironments();
58
        $this->prompt->printAltBlock(sprintf("%s: configuring service(s)...", $this->getAentName()));
59
        $servicesStates = $this->createServices($environments);
60
        foreach ($servicesStates as $serviceState) {
61
            $this->dispatch($environments->getDevelopmentEnvironments(), $serviceState->getDevelopmentVersion());
62
            $this->dispatch($environments->getTestEnvironments(), $serviceState->getTestVersion());
63
            $this->dispatch($environments->getProductionEnvironments(), $serviceState->getProductionVersion());
64
        }
65
        $this->prompt->printBlock("Dispatch done.");
66
        return null;
67
    }
68
69
    /**
70
     * @return void
71
     * @throws StringsException
72
     */
73
    protected function afterExecute(): void
74
    {
75
        $this->output->writeln(sprintf("\nšŸ‘‹ Hello again! This is the aent <info>%s</info> and we have finished my setup.", $this->getAentName()));
76
    }
77
78
    /**
79
     * @return Environments
80
     * @throws StringsException
81
     */
82
    private function fetchEnvironments(): Environments
83
    {
84
        $this->prompt->printAltBlock(sprintf("%s: fetching environments...", $this->getAentName()));
85
        $responses = Aenthill::dispatchJson('CHOOSE_ENVIRONMENT', []);
86
        if (empty($responses)) {
87
            $this->byebye();
88
        }
89
        $environments = new Environments();
90
        foreach ($responses as $response) {
91
            if (!empty($response)) {
92
                $context = Context::fromArray($response);
93
                $environments->add($context);
94
            }
95
        }
96
        if ($environments->isEmpty()) {
97
            $this->byebye();
98
        }
99
        return $environments;
100
    }
101
102
    /**
103
     * @return void
104
     * @throws StringsException
105
     */
106
    private function byebye(): void
107
    {
108
        $this->output->writeln(sprintf("\nšŸ‘‹ Hello again! This is the aent <info>%s</info> and you have not selected any environment! Bye!", $this->getAentName()));
109
        exit(0);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
110
    }
111
112
    /**
113
     * @param Context[] $environments
114
     * @param null|Service $service
115
     * @throws StringsException
116
     */
117
    private function dispatch(array $environments, ?Service $service): void
118
    {
119
        if (empty($environments || empty($service))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $environments of type TheAentMachine\Aent\Context\Context[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
120
            return;
121
        }
122
        foreach ($environments as $environment) {
123
            $this->prompt->printBlock(sprintf(
124
                "Dispatching service %s for %s environment %s.",
125
                $service->getServiceName(),
0 ignored issues
show
Bug introduced by
The method getServiceName() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
                $service->/** @scrutinizer ignore-call */ 
126
                          getServiceName(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
                $environment->getEnvironmentType(),
127
                $environment->getEnvironmentName()
128
            ));
129
            Aenthill::dispatchJson('NEW_SERVICE', $service, sprintf('"ENVIRONMENT_NAME" in Metadata and Metadata["ENVIRONMENT_NAME"] == "%s"', $environment->getEnvironmentName()));
130
        }
131
    }
132
}
133