JoliCiBuildStrategy::supportProject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
/*
3
 * This file is part of JoliCi.
4
*
5
* (c) Joel Wurtz <[email protected]>
6
*
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
9
*/
10
11
namespace Joli\JoliCi\BuildStrategy;
12
13
use Joli\JoliCi\Job;
14
use Joli\JoliCi\Filesystem\Filesystem;
15
use Joli\JoliCi\Naming;
16
use Symfony\Component\Finder\Finder;
17
18
/**
19
 * JoliCi implementation for build
20
 *
21
 * A project must have a .jolici directory, each directory inside this one will be a type of build and must contain a Dockerfile to be executable
22
 *
23
 * @author Joel Wurtz <[email protected]>
24
 */
25
class JoliCiBuildStrategy implements BuildStrategyInterface
26
{
27
    /**
28
     * @var string Base path for build
29
     */
30
    private $buildPath;
31
32
    /**
33
     * @var Filesystem Filesystem service
34
     */
35
    private $filesystem;
36
37
    /**
38
     * @var Naming Use to name the image created
39
     */
40
    private $naming;
41
42
    /**
43
     * @param string     $buildPath  Directory where build must be created
44
     * @param Naming     $naming     Naming service
45
     * @param Filesystem $filesystem Filesystem service
46
     */
47
    public function __construct($buildPath, Naming $naming, Filesystem $filesystem)
48
    {
49
        $this->buildPath  = $buildPath;
50
        $this->naming     = $naming;
51
        $this->filesystem = $filesystem;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getJobs($directory)
58
    {
59
        $builds = array();
60
        $finder = new Finder();
61
        $finder->directories();
62
63
        foreach ($finder->in($this->getJoliCiStrategyDirectory($directory)) as $dir) {
64
            $builds[] = new Job(
65
                $this->naming->getProjectName($directory),
66
                $this->getName(),
67
                $this->naming->getUniqueKey(array('build' => $dir->getFilename())),
68
                array(
69
                    'origin' => $directory,
70
                    'build'  => $dir->getRealPath(),
71
                ),
72
                "JoliCi Build: ".$dir->getFilename()
73
            );
74
        }
75
76
        return $builds;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function prepareJob(Job $job)
83
    {
84
        $origin = $job->getParameters()['origin'];
85
        $target = $this->buildPath.DIRECTORY_SEPARATOR. $job->getDirectory();
86
        $build  = $job->getParameters()['build'];
87
88
        // First mirroring target
89
        $this->filesystem->mirror($origin, $target, null, array(
90
            'delete'   => true,
91
            'override' => true,
92
        ));
93
94
        // Second override target with build dir
95
        $this->filesystem->mirror($build, $target, null, array(
96
            'delete'   => false,
97
            'override' => true,
98
        ));
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getName()
105
    {
106
        return "JoliCi";
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function supportProject($directory)
113
    {
114
        return file_exists($this->getJoliCiStrategyDirectory($directory)) && is_dir($this->getJoliCiStrategyDirectory($directory));
115
    }
116
117
    /**
118
     * Return the jolici strategy directory where there must be builds
119
     *
120
     * @param  string $projectPath
121
     * @return string
122
     */
123
    protected function getJoliCiStrategyDirectory($projectPath)
124
    {
125
        return $projectPath.DIRECTORY_SEPARATOR.'.jolici';
126
    }
127
}
128