InitializeCommand::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 10
rs 9.4286
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of CoverallsKit.
5
 *
6
 * (c) Noritaka Horio <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace coverallskit;
13
14
use Aura\Cli\Stdio;
15
use Aura\Cli\Context;
16
use Aura\Cli\Status;
17
use Eloquent\Pathogen\Factory\PathFactory;
18
use Eloquent\Pathogen\RelativePath;
19
use Eloquent\Pathogen\AbstractPath;
20
21
/**
22
 * Class InitializeCommand
23
 * @package coverallskit
24
 */
25
class InitializeCommand
26
{
27
28
    /**
29
     * @var Context
30
     */
31
    private $context;
32
33
    /**
34
     * @var Stdio
35
     */
36
    private $stdio;
37
38
    /**
39
     * @var \Eloquent\Pathogen\AbsolutePath
40
     */
41
    private $destDirectoryPath;
42
43
    /**
44
     * @var \Eloquent\Pathogen\AbsolutePath
45
     */
46
    private $destDirectoryFilePath;
47
48
    /**
49
     * @param Context $context
50
     * @param Stdio $stdio
51
     */
52
    public function __construct(Context $context, Stdio $stdio)
53
    {
54
        $this->context = $context;
55
        $this->stdio = $stdio;
56
    }
57
58
    /**
59
     * @param string|null $projectDirectory
60
     * @return int
61
     */
62
    public function __invoke($projectDirectory = null)
63
    {
64
        try {
65
            $this->prepare($projectDirectory);
66
        } catch (DirectoryNotFoundException $exception) {
67
            return $this->failed($exception);
68
        }
69
70
        return $this->performAction();
71
    }
72
73
    /**
74
     * @param string|null $projectDirectory
75
     */
76
    private function prepare($projectDirectory = null)
77
    {
78
        $projectRelativeDirectory = $projectDirectory;
79
        $configFilePath = RelativePath::fromString('.coveralls.toml');
80
        $destDirectoryPath = PathFactory::instance()->create(getcwd());
81
82
        if (is_null($projectRelativeDirectory)) {
83
            $projectRelativeDirectory = AbstractPath::SELF_ATOM;
84
        }
85
86
        $projectDirectoryPath = RelativePath::fromString($projectRelativeDirectory);
87
        $absoluteDestDirectoryPath = $destDirectoryPath->resolve($projectDirectoryPath);
88
89
        $this->destDirectoryPath = $absoluteDestDirectoryPath;
90
        $this->destDirectoryFilePath = $absoluteDestDirectoryPath->resolve($configFilePath);
91
92
        if (file_exists((string) $this->destDirectoryPath)) {
93
            return;
94
        }
95
96
        throw new DirectoryNotFoundException("'$this->destDirectoryPath' does not exist.");
97
    }
98
99
    /**
100
     * @param PrintableException $exception
101
     * @return int
102
     */
103
    private function failed(PrintableException $exception)
104
    {
105
        $exception->printMessage($this->stdio);
106
        return Status::FAILURE;
107
    }
108
109
    /**
110
     * @return int
111
     */
112
    private function performAction()
113
    {
114
        try {
115
            $this->copyTemplateFile();
116
        } catch (TemplateCopyFailedException $exception) {
117
            return $this->failed($exception);
118
        }
119
120
        return Status::SUCCESS;
121
    }
122
123
    /**
124
     * @return int
125
     */
126
    private function copyTemplateFile()
127
    {
128
        $templateFile = realpath(__DIR__ . '/../template/.coveralls.toml');
129
130
        if (copy($templateFile, (string) $this->destDirectoryFilePath)) {
131
            return;
132
        }
133
134
        throw new TemplateCopyFailedException("Can not copy the files to the directory $this->destDirectoryPath.");
135
    }
136
137
}
138