NewImageEvent::createDockerfile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 13
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\AentDockerfile\Event;
4
5
use Safe\Exceptions\FilesystemException;
6
use Safe\Exceptions\StringsException;
7
use Symfony\Component\Filesystem\Filesystem;
8
use TheAentMachine\Aent\Context\Context;
9
use TheAentMachine\Aent\Event\Builder\AbstractNewImageEvent;
10
use TheAentMachine\Aent\Payload\Builder\NewImageReplyPayload;
11
use TheAentMachine\Aenthill\Pheromone;
12
use TheAentMachine\Exception\MissingEnvironmentVariableException;
13
use TheAentMachine\Service\Service;
14
use function \Safe\sprintf;
15
use function \Safe\chown;
16
use function \Safe\chgrp;
17
18
final class NewImageEvent extends AbstractNewImageEvent
19
{
20
    /**
21
     * @param Service $service
22
     * @return NewImageReplyPayload
23
     * @throws FilesystemException
24
     * @throws MissingEnvironmentVariableException
25
     * @throws StringsException
26
     */
27
    protected function createDockerfile(Service $service): NewImageReplyPayload
28
    {
29
        /** @var Context $context */
30
        $context = Context::fromMetadata();
31
        $commands = \implode(PHP_EOL, $service->getDockerfileCommands()) . PHP_EOL;
32
        $dockerfileName = sprintf("Dockerfile.%s.%s", $context->getEnvironmentName(), $service->getServiceName());
33
        $dockerfilePath = Pheromone::getContainerProjectDirectory() . "/$dockerfileName";
34
        $fileSystem = new Filesystem();
35
        $fileSystem->dumpFile($dockerfilePath, $commands);
36
        $dirInfo = new \SplFileInfo(\dirname($dockerfilePath));
37
        chown($dockerfilePath, $dirInfo->getOwner());
38
        chgrp($dockerfilePath, $dirInfo->getGroup());
39
        return new NewImageReplyPayload($dockerfileName);
40
    }
41
}
42