AbstractNewImageEvent   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A afterExecute() 0 9 1
A beforeExecute() 0 9 1
A getEventName() 0 3 1
A executeJsonEvent() 0 6 1
A shouldRegisterEvents() 0 3 1
1
<?php
2
3
namespace TheAentMachine\Aent\Event\Builder;
4
5
use Safe\Exceptions\FilesystemException;
6
use Safe\Exceptions\StringsException;
7
use TheAentMachine\Aent\Context\Context;
8
use TheAentMachine\Aent\Event\AbstractJsonEvent;
9
use TheAentMachine\Aent\Payload\Builder\NewImageReplyPayload;
10
use TheAentMachine\Service\Exception\ServiceException;
11
use TheAentMachine\Service\Service;
12
use function Safe\sprintf;
13
14
abstract class AbstractNewImageEvent extends AbstractJsonEvent
15
{
16
    /** @var string */
17
    private $dockerfileName;
18
19
    /**
20
     * @param Service $service
21
     * @return NewImageReplyPayload
22
     */
23
    abstract protected function createDockerfile(Service $service): NewImageReplyPayload;
24
25
    /**
26
     * @return string
27
     */
28
    protected function getEventName(): string
29
    {
30
        return 'NEW_IMAGE';
31
    }
32
33
    /**
34
     * @return bool
35
     */
36
    protected function shouldRegisterEvents(): bool
37
    {
38
        return false;
39
    }
40
41
    /**
42
     * @return void
43
     * @throws StringsException
44
     */
45
    protected function beforeExecute(): void
46
    {
47
        /** @var Context $context */
48
        $context = Context::fromMetadata();
49
        $this->output->writeln(sprintf(
50
            "\nšŸ‘‹ Hello! I'm the aent <info>%s</info> and I'm going to create a Dockerfile for your service for your <info>%s</info> environment <info>%s</info>.",
51
            $this->getAentName(),
52
            $context->getEnvironmentType(),
53
            $context->getEnvironmentName()
54
        ));
55
    }
56
57
    /**
58
     * @param array $payload
59
     * @return array|null
60
     * @throws ServiceException
61
     * @throws StringsException
62
     * @throws FilesystemException
63
     */
64
    protected function executeJsonEvent(array $payload): ?array
65
    {
66
        $service = Service::parsePayload($payload);
67
        $payload = $this->createDockerfile($service);
68
        $this->dockerfileName = $payload->getDockerfileName();
69
        return $payload->toArray();
70
    }
71
72
    /**
73
     * @return void
74
     * @throws StringsException
75
     */
76
    protected function afterExecute(): void
77
    {
78
        /** @var Context $context */
79
        $context = Context::fromMetadata();
80
        $this->output->writeln(sprintf(
81
            "\nI've successfully created <info>%s</info> for your <info>%s</info> environment <info>%s</info>!",
82
            $this->dockerfileName,
83
            $context->getEnvironmentType(),
84
            $context->getEnvironmentName()
85
        ));
86
    }
87
}
88