Passed
Pull Request — master (#41)
by Keoghan
03:50
created

Organiser::pullImages()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.128

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 4.128
1
<?php
2
3
namespace App\Support\Images\Organiser;
4
5
use App\Support\Contracts\Cli;
6
use App\Support\Contracts\ImageRepository;
7
use App\Support\Images\Image;
8
9
class Organiser
10
{
11
    /** @var ImageRepository */
12
    protected $repository;
13
14
    /** @var Cli */
15
    protected $cli;
16
17 9
    public function __construct(ImageRepository $repository, Cli $cli)
18
    {
19 9
        $this->repository = $repository;
20 9
        $this->cli = $cli;
21 9
    }
22
23
    /**
24
     * Build the current images.
25
     *
26
     * @param string|null $service
27
     *
28
     * @throws \Exception
29
     */
30 3
    public function buildImages($service = null)
31
    {
32 3
        foreach ($this->repository->findByServiceName($service, $firstPartyOnly = true) as $image) {
33
            /* @var Image $image */
34 2
            $this->cli->passthru("docker build -t {$image->getName()} --rm {$image->getLocalPath()} --");
35
        }
36 3
    }
37
38
    /**
39
     * Push the current images.
40
     *
41
     * @param string|null $service
42
     *
43
     * @throws \Exception
44
     */
45 3
    public function pushImages($service = null)
46
    {
47 3
        foreach ($this->repository->findByServiceName($service, $firstPartyOnly = true) as $image) {
48
            /* @var Image $image */
49 2
            $this->cli->passthru("docker push {$image->getName()}");
50
        }
51 3
    }
52
53
    /**
54
     * Pull our docker images.
55
     *
56
     * @param string|null $service
57
     *
58
     * @throws \Exception
59
     */
60 3
    public function pullImages($service = null)
61
    {
62 3
        foreach ($this->repository->findByServiceName($service) as $image) {
63
            /** @var Image $image */
64 3
            if (running_tests() && $this->hasImage($image)) {
65
                continue;
66
            }
67
68 3
            $this->cli->passthru("docker pull {$image->getName()}");
69
        }
70 3
    }
71
72
    /**
73
     * Check if we already have the image.
74
     *
75
     * @param Image $image
76
     *
77
     * @return bool
78
     */
79 3
    public function hasImage(Image $image)
80
    {
81 3
        $output = $this->cli->exec("docker image inspect {$image->getName()}");
82
83 3
        return strpos($output, "Error: No such image: {$image->getName()}") === false;
84
    }
85
}
86