Completed
Push — master ( f0448f...4201d1 )
by Julien
14s
created

DockerCompose::createDockerComposeFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
namespace TheAentMachine\AentDockerCompose\DockerCompose;
3
4
use Nette\NotImplementedException;
5
use Symfony\Component\Finder\Finder;
6
use TheAentMachine\AentDockerCompose\Aenthill\Enum\PheromoneEnum;
7
use TheAentMachine\AentDockerCompose\Aenthill\Log;
8
use TheAentMachine\AentDockerCompose\Aenthill\Exception\ContainerProjectDirEnvVariableEmptyException;
9
10
class DockerCompose
11
{
12
    /** @var Log */
13
    private $log;
14
15
    /** @var DockerComposeFile[] */
16
    private $files;
17
18
    /**
19
     * DockerCompose constructor.
20
     * @param Log $log
21
     */
22
    public function __construct(Log $log)
23
    {
24
        $this->log = $log;
25
        $this->files = [];
26
    }
27
28
    /**
29
     * @throws ContainerProjectDirEnvVariableEmptyException
30
     */
31
    public function seekFiles(): void
32
    {
33
        $containerProjectDir = getenv(PheromoneEnum::PHEROMONE_CONTAINER_PROJECT_DIR);
34
        if (empty($containerProjectDir)) {
35
            throw new ContainerProjectDirEnvVariableEmptyException();
36
        }
37
38
        $finder = new Finder();
39
        $dockerComposeFileFilter = function (\SplFileInfo $file) {
40
            return $file->isFile() && preg_match('/^docker-compose(.)*\.(yaml|yml)$/', $file->getFilename());
41
        };
42
        $finder->files()->filter($dockerComposeFileFilter)->in($containerProjectDir)->depth('== 0');
43
44
        if (!$finder->hasResults()) {
45
            $this->log->infoln("no docker-compose file found, let's create it");
46
            $this->createDockerComposeFile($containerProjectDir . '/docker-compose.yml');
47
            return;
48
        }
49
50
        /** @var \SplFileInfo $file */
51
        foreach ($finder as $file) {
52
            $this->files[] = new DockerComposeFile($file);
53
        }
54
55
        if (count($this->files) === 1) {
56
            $this->log->infoln($this->files[0]->getFilename() . ' has been found');
57
            return;
58
        }
59
60
        throw new NotImplementedException("multiple docker-compose files handling is not yet implemented");
61
    }
62
63
    /**
64
     * @param string $path
65
     */
66
    private function createDockerComposeFile(string $path): void
67
    {
68
        // TODO ask questions about version and so on!
69
        $fp = fopen($path, 'wb');
70
        fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

70
        fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
71
72
        $file = new DockerComposeFile(new \SplFileInfo($path));
73
        $this->files[] = $file;
74
        $this->log->infoln($file->getFilename() . ' was successfully created!');
75
    }
76
}
77