Failed Conditions
Pull Request — master (#41)
by Keoghan
03:17
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
     * @throws \Exception
28
     */
29 3
    public function buildImages($service = null)
30
    {
31 3
        foreach ($this->repository->findByServiceName($service, $firstPartyOnly = true) as $image) {
32
            /* @var Image $image */
33 2
            $this->cli->passthru("docker build -t {$image->getName()} --rm {$image->getLocalPath()} --");
34
        }
35 3
    }
36
37
    /**
38
     * Push the current images.
39
     *
40
     * @param string|null $service
41
     * @throws \Exception
42
     */
43 3
    public function pushImages($service = null)
44
    {
45 3
        foreach ($this->repository->findByServiceName($service, $firstPartyOnly = true) as $image) {
46
            /* @var Image $image */
47 2
            $this->cli->passthru("docker push {$image->getName()}");
48
        }
49 3
    }
50
51
    /**
52
     * Pull our docker images.
53
     *
54
     * @param string|null $service
55
     * @throws \Exception
56
     */
57 3
    public function pullImages($service = null)
58
    {
59 3
        foreach ($this->repository->findByServiceName($service) as $image) {
60
            /** @var Image $image */
61 3
            if (running_tests() && $this->hasImage($image)) {
62
                continue;
63
            }
64
65 3
            $this->cli->passthru("docker pull {$image->getName()}");
66
        }
67 3
    }
68
69
    /**
70
     * Check if we already have the image.
71
     *
72
     * @param Image $image
73
     *
74
     * @return bool
75
     */
76 3
    public function hasImage(Image $image)
77
    {
78 3
        $output = $this->cli->exec("docker image inspect {$image->getName()}");
79
80 3
        return strpos($output, "Error: No such image: {$image->getName()}") === false;
81
    }
82
}
83