Completed
Push — master ( 2386c7...c56f94 )
by Tomáš
02:42
created

pushDirectoryContentToRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
ccs 8
cts 8
cp 1
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\PHP7_Sculpin\Github;
11
12
use Symfony\Component\Process\Process;
13
14
final class GihubPublishingProcess
15
{
16 1
    public function setupTravisIdentityToGit()
17
    {
18 1
        if (getenv('TRAVIS')) {
19 1
            $this->runScript('git config --global user.email "[email protected]"');
20 1
            $this->runScript('git config --global user.name "Travis"');
21
        }
22 1
    }
23
24 2
    public function pushDirectoryContentToRepository(string $outputDirectory, string $githubRepository)
25
    {
26 2
        $this->ensureDirectoryExists($outputDirectory);
27
28 1
        $this->runScript(
29 1
            'git init && git add . && git commit -m "Regenerate output"',
30
            $outputDirectory
31
        );
32
33 1
        if (getenv('TRAVIS')) {
34 1
            $this->runScript(sprintf(
35 1
                'git push --force --quiet "${%s}" master:gh-pages > /dev/null 2>&1', $githubRepository
36
            ), $outputDirectory);
37
        }
38 1
    }
39
40 2
    private function runScript(string $script, string $workingDirectory = null)
41
    {
42 2
        $process = new Process($script);
43 2
        if ($workingDirectory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $workingDirectory of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
44 1
            $process->setWorkingDirectory($workingDirectory);
45
        }
46
47 2
        $process->run();
48 2
    }
49
50 2
    private function ensureDirectoryExists(string $directory)
51
    {
52 2
        if (!is_dir($directory)) {
53 1
            throw new \Exception(
54 1
                sprintf('Directory "%s" was not found.', $directory)
55
            );
56
        }
57 1
    }
58
}
59